Stabilize LazyCell and LazyLock (lazy_cell) · model-checking/verify-rust-std@511fe47 (original) (raw)

`@@ -31,8 +31,6 @@ union Data<T, F> {

`

31

31

`` /// Initialize static variables with LazyLock.

``

32

32

`///

`

33

33

```` /// ```


`34`

``

`-

/// #![feature(lazy_cell)]

`

`35`

``

`-

///

`

`36`

`34`

`/// use std::collections::HashMap;

`

`37`

`35`

`///

`

`38`

`36`

`/// use std::sync::LazyLock;

`

`@@ -61,8 +59,6 @@ union Data<T, F> {

`

`61`

`59`

```` /// ```

62

60

`` /// Initialize fields with LazyLock.

``

63

61

```` /// ```


`64`

``

`-

/// #![feature(lazy_cell)]

`

`65`

``

`-

///

`

`66`

`62`

`/// use std::sync::LazyLock;

`

`67`

`63`

`///

`

`68`

`64`

`/// #[derive(Debug)]

`

`@@ -76,17 +72,29 @@ union Data<T, F> {

`

`76`

`72`

`/// println!("{}", *data.number);

`

`77`

`73`

`/// }

`

`78`

`74`

```` /// ```

79

``

-

80

``

`-

#[unstable(feature = "lazy_cell", issue = "109736")]

`

``

75

`+

#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]

`

81

76

`pub struct LazyLock<T, F = fn() -> T> {

`

82

77

`once: Once,

`

83

78

`data: UnsafeCell<Data<T, F>>,

`

84

79

`}

`

85

80

``

86

81

`impl<T, F: FnOnce() -> T> LazyLock<T, F> {

`

87

82

`/// Creates a new lazy value with the given initializing function.

`

``

83

`+

///

`

``

84

`+

/// # Examples

`

``

85

`+

///

`

``

86


/// ```

``

87

`+

/// use std::sync::LazyLock;

`

``

88

`+

///

`

``

89

`+

/// let hello = "Hello, World!".to_string();

`

``

90

`+

///

`

``

91

`+

/// let lazy = LazyLock::new(|| hello.to_uppercase());

`

``

92

`+

///

`

``

93

`+

/// assert_eq!(&*lazy, "HELLO, WORLD!");

`

``

94


/// ```

88

95

`#[inline]

`

89

``

`-

#[unstable(feature = "lazy_cell", issue = "109736")]

`

``

96

`+

#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]

`

``

97

`+

#[rustc_const_stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]

`

90

98

`pub const fn new(f: F) -> LazyLock<T, F> {

`

91

99

`LazyLock { once: Once::new(), data: UnsafeCell::new(Data { f: ManuallyDrop::new(f) }) }

`

92

100

`}

`

`@@ -107,7 +115,6 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {

`

107

115

`/// # Examples

`

108

116

`///

`

109

117

```` /// ```


`110`

``

`-

/// #![feature(lazy_cell)]

`

`111`

`118`

`/// #![feature(lazy_cell_consume)]

`

`112`

`119`

`///

`

`113`

`120`

`/// use std::sync::LazyLock;

`

`@@ -145,8 +152,6 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {

`

`145`

`152`

`/// # Examples

`

`146`

`153`

`///

`

`147`

`154`

```` /// ```

148

``

`-

/// #![feature(lazy_cell)]

`

149

``

`-

///

`

150

155

`/// use std::sync::LazyLock;

`

151

156

`///

`

152

157

`/// let lazy = LazyLock::new(|| 92);

`

`@@ -155,7 +160,7 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {

`

155

160

`/// assert_eq!(&*lazy, &92);

`

156

161

```` /// ```

````

157

162

`#[inline]

`

158

``

`-

#[unstable(feature = "lazy_cell", issue = "109736")]

`

``

163

`+

#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]

`

159

164

`pub fn force(this: &LazyLock<T, F>) -> &T {

`

160

165

` this.once.call_once(|| {

`

161

166

`` // SAFETY: call_once only runs this closure once, ever.

``

`@@ -191,7 +196,7 @@ impl<T, F> LazyLock<T, F> {

`

191

196

`}

`

192

197

`}

`

193

198

``

194

``

`-

#[unstable(feature = "lazy_cell", issue = "109736")]

`

``

199

`+

#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]

`

195

200

`impl<T, F> Drop for LazyLock<T, F> {

`

196

201

`fn drop(&mut self) {

`

197

202

`match self.once.state() {

`

`@@ -204,7 +209,7 @@ impl<T, F> Drop for LazyLock<T, F> {

`

204

209

`}

`

205

210

`}

`

206

211

``

207

``

`-

#[unstable(feature = "lazy_cell", issue = "109736")]

`

``

212

`+

#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]

`

208

213

`impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F> {

`

209

214

`type Target = T;

`

210

215

``

`@@ -219,7 +224,7 @@ impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F> {

`

219

224

`}

`

220

225

`}

`

221

226

``

222

``

`-

#[unstable(feature = "lazy_cell", issue = "109736")]

`

``

227

`+

#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]

`

223

228

`impl<T: Default> Default for LazyLock {

`

224

229

`` /// Creates a new lazy value using Default as the initializing function.

``

225

230

`#[inline]

`

`@@ -228,7 +233,7 @@ impl<T: Default> Default for LazyLock {

`

228

233

`}

`

229

234

`}

`

230

235

``

231

``

`-

#[unstable(feature = "lazy_cell", issue = "109736")]

`

``

236

`+

#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]

`

232

237

`impl<T: fmt::Debug, F> fmt::Debug for LazyLock<T, F> {

`

233

238

`fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

`

234

239

`let mut d = f.debug_tuple("LazyLock");

`

`@@ -242,13 +247,13 @@ impl<T: fmt::Debug, F> fmt::Debug for LazyLock<T, F> {

`

242

247

``

243

248

`` // We never create a &F from a &LazyLock<T, F> so it is fine

``

244

249

`` // to not impl Sync for F.

``

245

``

`-

#[unstable(feature = "lazy_cell", issue = "109736")]

`

``

250

`+

#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]

`

246

251

`unsafe impl<T: Sync + Send, F: Send> Sync for LazyLock<T, F> {}

`

247

252

`` // auto-derived Send impl is OK.

``

248

253

``

249

``

`-

#[unstable(feature = "lazy_cell", issue = "109736")]

`

``

254

`+

#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]

`

250

255

`impl<T: RefUnwindSafe + UnwindSafe, F: UnwindSafe> RefUnwindSafe for LazyLock<T, F> {}

`

251

``

`-

#[unstable(feature = "lazy_cell", issue = "109736")]

`

``

256

`+

#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]

`

252

257

`impl<T: UnwindSafe, F: UnwindSafe> UnwindSafe for LazyLock<T, F> {}

`

253

258

``

254

259

`#[cfg(test)]

`