Skip to content

Commit 0b13a6c

Browse files
authored
Rollup merge of rust-lang#121377 - pitaj:lazy_cell_fn_pointer, r=dtolnay
Stabilize `LazyCell` and `LazyLock` Closes rust-lang#109736 This stabilizes the [`LazyLock`](https://doc.rust-lang.org/stable/std/sync/struct.LazyLock.html) and [`LazyCell`](https://doc.rust-lang.org/stable/std/cell/struct.LazyCell.html) types: ```rust static HASHMAP: LazyLock<HashMap<i32, String>> = LazyLock::new(|| { println!("initializing"); let mut m = HashMap::new(); m.insert(13, "Spica".to_string()); m.insert(74, "Hoyten".to_string()); m }); let lazy: LazyCell<i32> = LazyCell::new(|| { println!("initializing"); 92 }); ``` r? libs-api
2 parents 23bb5bc + 511fe47 commit 0b13a6c

File tree

6 files changed

+32
-35
lines changed

6 files changed

+32
-35
lines changed

core/src/cell.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ use crate::ptr::{self, NonNull};
245245
mod lazy;
246246
mod once;
247247

248-
#[unstable(feature = "lazy_cell", issue = "109736")]
248+
#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
249249
pub use lazy::LazyCell;
250250
#[stable(feature = "once_cell", since = "1.70.0")]
251251
pub use once::OnceCell;

core/src/cell/lazy.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ enum State<T, F> {
1818
/// # Examples
1919
///
2020
/// ```
21-
/// #![feature(lazy_cell)]
22-
///
2321
/// use std::cell::LazyCell;
2422
///
2523
/// let lazy: LazyCell<i32> = LazyCell::new(|| {
@@ -36,7 +34,7 @@ enum State<T, F> {
3634
/// // 92
3735
/// // 92
3836
/// ```
39-
#[unstable(feature = "lazy_cell", issue = "109736")]
37+
#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
4038
pub struct LazyCell<T, F = fn() -> T> {
4139
state: UnsafeCell<State<T, F>>,
4240
}
@@ -47,8 +45,6 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
4745
/// # Examples
4846
///
4947
/// ```
50-
/// #![feature(lazy_cell)]
51-
///
5248
/// use std::cell::LazyCell;
5349
///
5450
/// let hello = "Hello, World!".to_string();
@@ -58,7 +54,8 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
5854
/// assert_eq!(&*lazy, "HELLO, WORLD!");
5955
/// ```
6056
#[inline]
61-
#[unstable(feature = "lazy_cell", issue = "109736")]
57+
#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
58+
#[rustc_const_stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
6259
pub const fn new(f: F) -> LazyCell<T, F> {
6360
LazyCell { state: UnsafeCell::new(State::Uninit(f)) }
6461
}
@@ -70,7 +67,6 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
7067
/// # Examples
7168
///
7269
/// ```
73-
/// #![feature(lazy_cell)]
7470
/// #![feature(lazy_cell_consume)]
7571
///
7672
/// use std::cell::LazyCell;
@@ -99,8 +95,6 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
9995
/// # Examples
10096
///
10197
/// ```
102-
/// #![feature(lazy_cell)]
103-
///
10498
/// use std::cell::LazyCell;
10599
///
106100
/// let lazy = LazyCell::new(|| 92);
@@ -109,7 +103,7 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
109103
/// assert_eq!(&*lazy, &92);
110104
/// ```
111105
#[inline]
112-
#[unstable(feature = "lazy_cell", issue = "109736")]
106+
#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
113107
pub fn force(this: &LazyCell<T, F>) -> &T {
114108
// SAFETY:
115109
// This invalidates any mutable references to the data. The resulting
@@ -173,7 +167,7 @@ impl<T, F> LazyCell<T, F> {
173167
}
174168
}
175169

176-
#[unstable(feature = "lazy_cell", issue = "109736")]
170+
#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
177171
impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
178172
type Target = T;
179173
#[inline]
@@ -182,7 +176,7 @@ impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
182176
}
183177
}
184178

185-
#[unstable(feature = "lazy_cell", issue = "109736")]
179+
#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
186180
impl<T: Default> Default for LazyCell<T> {
187181
/// Creates a new lazy value using `Default` as the initializing function.
188182
#[inline]
@@ -191,7 +185,7 @@ impl<T: Default> Default for LazyCell<T> {
191185
}
192186
}
193187

194-
#[unstable(feature = "lazy_cell", issue = "109736")]
188+
#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
195189
impl<T: fmt::Debug, F> fmt::Debug for LazyCell<T, F> {
196190
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
197191
let mut d = f.debug_tuple("LazyCell");

core/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@
9696
#![feature(pointer_is_aligned_to)]
9797
#![feature(portable_simd)]
9898
#![feature(ptr_metadata)]
99-
#![feature(lazy_cell)]
10099
#![feature(unsized_tuple_coercion)]
101100
#![feature(const_option)]
102101
#![feature(const_option_ext)]

std/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,6 @@
395395
#![feature(edition_panic)]
396396
#![feature(format_args_nl)]
397397
#![feature(get_many_mut)]
398-
#![feature(lazy_cell)]
399398
#![feature(log_syntax)]
400399
#![feature(test)]
401400
#![feature(trace_macros)]

std/src/sync/lazy_lock.rs

+23-18
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ union Data<T, F> {
3131
/// Initialize static variables with `LazyLock`.
3232
///
3333
/// ```
34-
/// #![feature(lazy_cell)]
35-
///
3634
/// use std::collections::HashMap;
3735
///
3836
/// use std::sync::LazyLock;
@@ -61,8 +59,6 @@ union Data<T, F> {
6159
/// ```
6260
/// Initialize fields with `LazyLock`.
6361
/// ```
64-
/// #![feature(lazy_cell)]
65-
///
6662
/// use std::sync::LazyLock;
6763
///
6864
/// #[derive(Debug)]
@@ -76,17 +72,29 @@ union Data<T, F> {
7672
/// println!("{}", *data.number);
7773
/// }
7874
/// ```
79-
80-
#[unstable(feature = "lazy_cell", issue = "109736")]
75+
#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
8176
pub struct LazyLock<T, F = fn() -> T> {
8277
once: Once,
8378
data: UnsafeCell<Data<T, F>>,
8479
}
8580

8681
impl<T, F: FnOnce() -> T> LazyLock<T, F> {
8782
/// 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+
/// ```
8895
#[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")]
9098
pub const fn new(f: F) -> LazyLock<T, F> {
9199
LazyLock { once: Once::new(), data: UnsafeCell::new(Data { f: ManuallyDrop::new(f) }) }
92100
}
@@ -107,7 +115,6 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
107115
/// # Examples
108116
///
109117
/// ```
110-
/// #![feature(lazy_cell)]
111118
/// #![feature(lazy_cell_consume)]
112119
///
113120
/// use std::sync::LazyLock;
@@ -145,8 +152,6 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
145152
/// # Examples
146153
///
147154
/// ```
148-
/// #![feature(lazy_cell)]
149-
///
150155
/// use std::sync::LazyLock;
151156
///
152157
/// let lazy = LazyLock::new(|| 92);
@@ -155,7 +160,7 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
155160
/// assert_eq!(&*lazy, &92);
156161
/// ```
157162
#[inline]
158-
#[unstable(feature = "lazy_cell", issue = "109736")]
163+
#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
159164
pub fn force(this: &LazyLock<T, F>) -> &T {
160165
this.once.call_once(|| {
161166
// SAFETY: `call_once` only runs this closure once, ever.
@@ -191,7 +196,7 @@ impl<T, F> LazyLock<T, F> {
191196
}
192197
}
193198

194-
#[unstable(feature = "lazy_cell", issue = "109736")]
199+
#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
195200
impl<T, F> Drop for LazyLock<T, F> {
196201
fn drop(&mut self) {
197202
match self.once.state() {
@@ -204,7 +209,7 @@ impl<T, F> Drop for LazyLock<T, F> {
204209
}
205210
}
206211

207-
#[unstable(feature = "lazy_cell", issue = "109736")]
212+
#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
208213
impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F> {
209214
type Target = T;
210215

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

222-
#[unstable(feature = "lazy_cell", issue = "109736")]
227+
#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
223228
impl<T: Default> Default for LazyLock<T> {
224229
/// Creates a new lazy value using `Default` as the initializing function.
225230
#[inline]
@@ -228,7 +233,7 @@ impl<T: Default> Default for LazyLock<T> {
228233
}
229234
}
230235

231-
#[unstable(feature = "lazy_cell", issue = "109736")]
236+
#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
232237
impl<T: fmt::Debug, F> fmt::Debug for LazyLock<T, F> {
233238
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234239
let mut d = f.debug_tuple("LazyLock");
@@ -242,13 +247,13 @@ impl<T: fmt::Debug, F> fmt::Debug for LazyLock<T, F> {
242247

243248
// We never create a `&F` from a `&LazyLock<T, F>` so it is fine
244249
// to not impl `Sync` for `F`.
245-
#[unstable(feature = "lazy_cell", issue = "109736")]
250+
#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
246251
unsafe impl<T: Sync + Send, F: Send> Sync for LazyLock<T, F> {}
247252
// auto-derived `Send` impl is OK.
248253

249-
#[unstable(feature = "lazy_cell", issue = "109736")]
254+
#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
250255
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")]
252257
impl<T: UnwindSafe, F: UnwindSafe> UnwindSafe for LazyLock<T, F> {}
253258

254259
#[cfg(test)]

std/src/sync/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pub use self::rwlock::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
179179
#[stable(feature = "rust1", since = "1.0.0")]
180180
pub use self::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
181181

182-
#[unstable(feature = "lazy_cell", issue = "109736")]
182+
#[stable(feature = "lazy_cell", since = "CURRENT_RUSTC_VERSION")]
183183
pub use self::lazy_lock::LazyLock;
184184
#[stable(feature = "once_cell", since = "1.70.0")]
185185
pub use self::once_lock::OnceLock;

0 commit comments

Comments
 (0)