Skip to content

Commit 8a7ca93

Browse files
committed
Auto merge of #105587 - tgross35:once-cell-min, r=m-ou-se
Partial stabilization of `once_cell` This PR aims to stabilize a portion of the `once_cell` feature: - `core::cell::OnceCell` - `std::cell::OnceCell` (re-export of the above) - `std::sync::OnceLock` This will leave `LazyCell` and `LazyLock` unstabilized, which have been moved to the `lazy_cell` feature flag. Tracking issue: #74465 (does not fully close, but it may make sense to move to a new issue) Future steps for separate PRs: - ~~Add `#[inline]` to many methods~~ #105651 - Update cranelift usage of the `once_cell` crate - Update rust-analyzer usage of the `once_cell` crate - Update error messages discussing once_cell ## To be stabilized API summary ```rust // core::cell (in core/cell/once.rs) pub struct OnceCell<T> { .. } impl<T> OnceCell<T> { pub const fn new() -> OnceCell<T>; pub fn get(&self) -> Option<&T>; pub fn get_mut(&mut self) -> Option<&mut T>; pub fn set(&self, value: T) -> Result<(), T>; pub fn get_or_init<F>(&self, f: F) -> &T where F: FnOnce() -> T; pub fn into_inner(self) -> Option<T>; pub fn take(&mut self) -> Option<T>; } impl<T: Clone> Clone for OnceCell<T>; impl<T: Debug> Debug for OnceCell<T> impl<T> Default for OnceCell<T>; impl<T> From<T> for OnceCell<T>; impl<T: PartialEq> PartialEq for OnceCell<T>; impl<T: Eq> Eq for OnceCell<T>; ``` ```rust // std::sync (in std/sync/once_lock.rs) impl<T> OnceLock<T> { pub const fn new() -> OnceLock<T>; pub fn get(&self) -> Option<&T>; pub fn get_mut(&mut self) -> Option<&mut T>; pub fn set(&self, value: T) -> Result<(), T>; pub fn get_or_init<F>(&self, f: F) -> &T where F: FnOnce() -> T; pub fn into_inner(self) -> Option<T>; pub fn take(&mut self) -> Option<T>; } impl<T: Clone> Clone for OnceLock<T>; impl<T: Debug> Debug for OnceLock<T>; impl<T> Default for OnceLock<T>; impl<#[may_dangle] T> Drop for OnceLock<T>; impl<T> From<T> for OnceLock<T>; impl<T: PartialEq> PartialEq for OnceLock<T> impl<T: Eq> Eq for OnceLock<T>; impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for OnceLock<T>; unsafe impl<T: Send> Send for OnceLock<T>; unsafe impl<T: Sync + Send> Sync for OnceLock<T>; impl<T: UnwindSafe> UnwindSafe for OnceLock<T>; ``` No longer planned as part of this PR, and moved to the `rust_cell_try` feature gate: ```rust impl<T> OnceCell<T> { pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E> where F: FnOnce() -> Result<T, E>; } impl<T> OnceLock<T> { pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E> where F: FnOnce() -> Result<T, E>; } ``` I am new to this process so would appreciate mentorship wherever needed.
2 parents f2d9a3d + 9b51229 commit 8a7ca93

File tree

40 files changed

+163
-156
lines changed

40 files changed

+163
-156
lines changed

compiler/rustc_borrowck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![feature(let_chains)]
66
#![feature(min_specialization)]
77
#![feature(never_type)]
8-
#![feature(once_cell)]
8+
#![feature(lazy_cell)]
99
#![feature(rustc_attrs)]
1010
#![feature(stmt_expr_attributes)]
1111
#![feature(trusted_step)]

compiler/rustc_codegen_llvm/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#![feature(iter_intersperse)]
1111
#![feature(let_chains)]
1212
#![feature(never_type)]
13-
#![feature(once_cell)]
1413
#![recursion_limit = "256"]
1514
#![allow(rustc::potential_query_instability)]
1615
#![deny(rustc::untranslatable_diagnostic)]

compiler/rustc_codegen_ssa/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#![feature(int_roundings)]
66
#![feature(let_chains)]
77
#![feature(never_type)]
8-
#![feature(once_cell)]
98
#![feature(strict_provenance)]
109
#![feature(try_blocks)]
1110
#![recursion_limit = "256"]

compiler/rustc_data_structures/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#![feature(never_type)]
2121
#![feature(type_alias_impl_trait)]
2222
#![feature(new_uninit)]
23-
#![feature(once_cell)]
23+
#![feature(lazy_cell)]
2424
#![feature(rustc_attrs)]
2525
#![feature(negative_impls)]
2626
#![feature(test)]

compiler/rustc_driver_impl/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
88
#![feature(is_terminal)]
9-
#![feature(once_cell)]
9+
#![feature(lazy_cell)]
1010
#![feature(decl_macro)]
1111
#![recursion_limit = "256"]
1212
#![allow(rustc::potential_query_instability)]

compiler/rustc_error_messages/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![feature(let_chains)]
2-
#![feature(once_cell)]
2+
#![feature(lazy_cell)]
33
#![feature(rustc_attrs)]
44
#![feature(type_alias_impl_trait)]
55
#![deny(rustc::untranslatable_diagnostic)]

compiler/rustc_feature/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! even if it is stabilized or removed, *do not remove it*. Instead, move the
1212
//! symbol to the `accepted` or `removed` modules respectively.
1313
14-
#![feature(once_cell)]
14+
#![feature(lazy_cell)]
1515
#![deny(rustc::untranslatable_diagnostic)]
1616
#![deny(rustc::diagnostic_outside_of_impl)]
1717

compiler/rustc_hir_analysis/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ This API is completely unstable and subject to change.
6767
#![feature(let_chains)]
6868
#![feature(min_specialization)]
6969
#![feature(never_type)]
70-
#![feature(once_cell)]
70+
#![feature(lazy_cell)]
7171
#![feature(slice_partition_dedup)]
7272
#![feature(try_blocks)]
7373
#![feature(is_some_and)]

compiler/rustc_interface/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![feature(decl_macro)]
33
#![feature(internal_output_capture)]
44
#![feature(thread_spawn_unchecked)]
5-
#![feature(once_cell)]
5+
#![feature(lazy_cell)]
66
#![feature(try_blocks)]
77
#![recursion_limit = "256"]
88
#![allow(rustc::potential_query_instability)]

compiler/rustc_metadata/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#![feature(generators)]
55
#![feature(iter_from_generator)]
66
#![feature(let_chains)]
7-
#![feature(once_cell)]
87
#![feature(proc_macro_internals)]
98
#![feature(macro_metavar_expr)]
109
#![feature(min_specialization)]

compiler/rustc_middle/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
#![feature(never_type)]
4040
#![feature(extern_types)]
4141
#![feature(new_uninit)]
42-
#![feature(once_cell)]
4342
#![feature(let_chains)]
4443
#![feature(min_specialization)]
4544
#![feature(trusted_len)]

compiler/rustc_mir_build/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#![feature(if_let_guard)]
99
#![feature(let_chains)]
1010
#![feature(min_specialization)]
11-
#![feature(once_cell)]
1211
#![feature(try_blocks)]
1312
#![recursion_limit = "256"]
1413

compiler/rustc_mir_dataflow/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#![feature(exact_size_is_empty)]
44
#![feature(let_chains)]
55
#![feature(min_specialization)]
6-
#![feature(once_cell)]
76
#![feature(stmt_expr_attributes)]
87
#![feature(trusted_step)]
98
#![recursion_limit = "256"]

compiler/rustc_mir_transform/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#![feature(map_try_insert)]
66
#![feature(min_specialization)]
77
#![feature(never_type)]
8-
#![feature(once_cell)]
98
#![feature(option_get_or_insert_default)]
109
#![feature(trusted_step)]
1110
#![feature(try_blocks)]

compiler/rustc_query_impl/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#![feature(const_mut_refs)]
66
#![feature(min_specialization)]
77
#![feature(never_type)]
8-
#![feature(once_cell)]
98
#![feature(rustc_attrs)]
109
#![recursion_limit = "256"]
1110
#![allow(rustc::potential_query_instability)]

compiler/rustc_session/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![feature(let_chains)]
33
#![feature(min_specialization)]
44
#![feature(never_type)]
5-
#![feature(once_cell)]
5+
#![feature(lazy_cell)]
66
#![feature(option_get_or_insert_default)]
77
#![feature(rustc_attrs)]
88
#![feature(map_many_mut)]

library/alloc/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
#![feature(slice_flatten)]
4343
#![feature(thin_box)]
4444
#![feature(strict_provenance)]
45-
#![feature(once_cell)]
4645
#![feature(drain_keep_rest)]
4746
#![deny(fuzzy_provenance_casts)]
4847
#![deny(unsafe_op_in_unsafe_fn)]

library/core/src/cell.rs

+70-27
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,77 @@
1111
//! mutate it.
1212
//!
1313
//! Shareable mutable containers exist to permit mutability in a controlled manner, even in the
14-
//! presence of aliasing. Both [`Cell<T>`] and [`RefCell<T>`] allow doing this in a single-threaded
15-
//! way. However, neither `Cell<T>` nor `RefCell<T>` are thread safe (they do not implement
16-
//! [`Sync`]). If you need to do aliasing and mutation between multiple threads it is possible to
17-
//! use [`Mutex<T>`], [`RwLock<T>`] or [`atomic`] types.
14+
//! presence of aliasing. [`Cell<T>`], [`RefCell<T>`], and [`OnceCell<T>`] allow doing this in
15+
//! a single-threaded way—they do not implement [`Sync`]. (If you need to do aliasing and
16+
//! mutation among multiple threads, [`Mutex<T>`], [`RwLock<T>`], [`OnceLock<T>`] or [`atomic`]
17+
//! types are the correct data structures to do so).
1818
//!
19-
//! Values of the `Cell<T>` and `RefCell<T>` types may be mutated through shared references (i.e.
20-
//! the common `&T` type), whereas most Rust types can only be mutated through unique (`&mut T`)
21-
//! references. We say that `Cell<T>` and `RefCell<T>` provide 'interior mutability', in contrast
22-
//! with typical Rust types that exhibit 'inherited mutability'.
19+
//! Values of the `Cell<T>`, `RefCell<T>`, and `OnceCell<T>` types may be mutated through shared
20+
//! references (i.e. the common `&T` type), whereas most Rust types can only be mutated through
21+
//! unique (`&mut T`) references. We say these cell types provide 'interior mutability'
22+
//! (mutable via `&T`), in contrast with typical Rust types that exhibit 'inherited mutability'
23+
//! (mutable only via `&mut T`).
2324
//!
24-
//! Cell types come in two flavors: `Cell<T>` and `RefCell<T>`. `Cell<T>` implements interior
25-
//! mutability by moving values in and out of the `Cell<T>`. To use references instead of values,
26-
//! one must use the `RefCell<T>` type, acquiring a write lock before mutating. `Cell<T>` provides
27-
//! methods to retrieve and change the current interior value:
25+
//! Cell types come in three flavors: `Cell<T>`, `RefCell<T>`, and `OnceCell<T>`. Each provides
26+
//! a different way of providing safe interior mutability.
27+
//!
28+
//! ## `Cell<T>`
29+
//!
30+
//! [`Cell<T>`] implements interior mutability by moving values in and out of the cell. That is, an
31+
//! `&mut T` to the inner value can never be obtained, and the value itself cannot be directly
32+
//! obtained without replacing it with something else. Both of these rules ensure that there is
33+
//! never more than one reference pointing to the inner value. This type provides the following
34+
//! methods:
2835
//!
2936
//! - For types that implement [`Copy`], the [`get`](Cell::get) method retrieves the current
30-
//! interior value.
37+
//! interior value by duplicating it.
3138
//! - For types that implement [`Default`], the [`take`](Cell::take) method replaces the current
3239
//! interior value with [`Default::default()`] and returns the replaced value.
33-
//! - For all types, the [`replace`](Cell::replace) method replaces the current interior value and
34-
//! returns the replaced value and the [`into_inner`](Cell::into_inner) method consumes the
35-
//! `Cell<T>` and returns the interior value. Additionally, the [`set`](Cell::set) method
36-
//! replaces the interior value, dropping the replaced value.
40+
//! - All types have:
41+
//! - [`replace`](Cell::replace): replaces the current interior value and returns the replaced
42+
//! value.
43+
//! - [`into_inner`](Cell::into_inner): this method consumes the `Cell<T>` and returns the
44+
//! interior value.
45+
//! - [`set`](Cell::set): this method replaces the interior value, dropping the replaced value.
46+
//!
47+
//! `Cell<T>` is typically used for more simple types where copying or moving values isn't too
48+
//! resource intensive (e.g. numbers), and should usually be preferred over other cell types when
49+
//! possible. For larger and non-copy types, `RefCell` provides some advantages.
3750
//!
38-
//! `RefCell<T>` uses Rust's lifetimes to implement 'dynamic borrowing', a process whereby one can
51+
//! ## `RefCell<T>`
52+
//!
53+
//! [`RefCell<T>`] uses Rust's lifetimes to implement "dynamic borrowing", a process whereby one can
3954
//! claim temporary, exclusive, mutable access to the inner value. Borrows for `RefCell<T>`s are
40-
//! tracked 'at runtime', unlike Rust's native reference types which are entirely tracked
41-
//! statically, at compile time. Because `RefCell<T>` borrows are dynamic it is possible to attempt
42-
//! to borrow a value that is already mutably borrowed; when this happens it results in thread
43-
//! panic.
55+
//! tracked at _runtime_, unlike Rust's native reference types which are entirely tracked
56+
//! statically, at compile time.
57+
//!
58+
//! An immutable reference to a `RefCell`'s inner value (`&T`) can be obtained with
59+
//! [`borrow`](`RefCell::borrow`), and a mutable borrow (`&mut T`) can be obtained with
60+
//! [`borrow_mut`](`RefCell::borrow_mut`). When these functions are called, they first verify that
61+
//! Rust's borrow rules will be satisfied: any number of immutable borrows are allowed or a
62+
//! single immutable borrow is allowed, but never both. If a borrow is attempted that would violate
63+
//! these rules, the thread will panic.
64+
//!
65+
//! The corresponding [`Sync`] version of `RefCell<T>` is [`RwLock<T>`].
66+
//!
67+
//! ## `OnceCell<T>`
68+
//!
69+
//! [`OnceCell<T>`] is somewhat of a hybrid of `Cell` and `RefCell` that works for values that
70+
//! typically only need to be set once. This means that a reference `&T` can be obtained without
71+
//! moving or copying the inner value (unlike `Cell`) but also without runtime checks (unlike
72+
//! `RefCell`). However, its value can also not be updated once set unless you have a mutable
73+
//! reference to the `OnceCell`.
74+
//!
75+
//! `OnceCell` provides the following methods:
76+
//!
77+
//! - [`get`](OnceCell::get): obtain a reference to the inner value
78+
//! - [`set`](OnceCell::set): set the inner value if it is unset (returns a `Result`)
79+
//! - [`get_or_init`](OnceCell::get_or_init): return the inner value, initializing it if needed
80+
//! - [`get_mut`](OnceCell::get_mut): provide a mutable reference to the inner value, only available
81+
//! if you have a mutable reference to the cell itself.
82+
//!
83+
//! The corresponding [`Sync`] version of `OnceCell<T>` is [`OnceLock<T>`].
84+
//!
4485
//!
4586
//! # When to choose interior mutability
4687
//!
@@ -188,6 +229,8 @@
188229
//! [`Rc<T>`]: ../../std/rc/struct.Rc.html
189230
//! [`RwLock<T>`]: ../../std/sync/struct.RwLock.html
190231
//! [`Mutex<T>`]: ../../std/sync/struct.Mutex.html
232+
//! [`OnceLock<T>`]: ../../std/sync/struct.OnceLock.html
233+
//! [`Sync`]: ../../std/marker/trait.Sync.html
191234
//! [`atomic`]: crate::sync::atomic
192235
193236
#![stable(feature = "rust1", since = "1.0.0")]
@@ -202,9 +245,9 @@ use crate::ptr::{self, NonNull};
202245
mod lazy;
203246
mod once;
204247

205-
#[unstable(feature = "once_cell", issue = "74465")]
248+
#[unstable(feature = "lazy_cell", issue = "109736")]
206249
pub use lazy::LazyCell;
207-
#[unstable(feature = "once_cell", issue = "74465")]
250+
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
208251
pub use once::OnceCell;
209252

210253
/// A mutable memory location.
@@ -419,7 +462,7 @@ impl<T> Cell<T> {
419462
mem::replace(unsafe { &mut *self.value.get() }, val)
420463
}
421464

422-
/// Unwraps the value.
465+
/// Unwraps the value, consuming the cell.
423466
///
424467
/// # Examples
425468
///
@@ -1969,7 +2012,7 @@ impl<T> UnsafeCell<T> {
19692012
UnsafeCell { value }
19702013
}
19712014

1972-
/// Unwraps the value.
2015+
/// Unwraps the value, consuming the cell.
19732016
///
19742017
/// # Examples
19752018
///
@@ -2133,7 +2176,7 @@ impl<T> SyncUnsafeCell<T> {
21332176
Self { value: UnsafeCell { value } }
21342177
}
21352178

2136-
/// Unwraps the value.
2179+
/// Unwraps the value, consuming the cell.
21372180
#[inline]
21382181
pub const fn into_inner(self) -> T {
21392182
self.value.into_inner()

library/core/src/cell/lazy.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::ops::Deref;
1111
/// # Examples
1212
///
1313
/// ```
14-
/// #![feature(once_cell)]
14+
/// #![feature(lazy_cell)]
1515
///
1616
/// use std::cell::LazyCell;
1717
///
@@ -29,7 +29,7 @@ use crate::ops::Deref;
2929
/// // 92
3030
/// // 92
3131
/// ```
32-
#[unstable(feature = "once_cell", issue = "74465")]
32+
#[unstable(feature = "lazy_cell", issue = "109736")]
3333
pub struct LazyCell<T, F = fn() -> T> {
3434
cell: OnceCell<T>,
3535
init: Cell<Option<F>>,
@@ -41,7 +41,7 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
4141
/// # Examples
4242
///
4343
/// ```
44-
/// #![feature(once_cell)]
44+
/// #![feature(lazy_cell)]
4545
///
4646
/// use std::cell::LazyCell;
4747
///
@@ -52,7 +52,7 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
5252
/// assert_eq!(&*lazy, "HELLO, WORLD!");
5353
/// ```
5454
#[inline]
55-
#[unstable(feature = "once_cell", issue = "74465")]
55+
#[unstable(feature = "lazy_cell", issue = "109736")]
5656
pub const fn new(init: F) -> LazyCell<T, F> {
5757
LazyCell { cell: OnceCell::new(), init: Cell::new(Some(init)) }
5858
}
@@ -65,7 +65,7 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
6565
/// # Examples
6666
///
6767
/// ```
68-
/// #![feature(once_cell)]
68+
/// #![feature(lazy_cell)]
6969
///
7070
/// use std::cell::LazyCell;
7171
///
@@ -75,7 +75,7 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
7575
/// assert_eq!(&*lazy, &92);
7676
/// ```
7777
#[inline]
78-
#[unstable(feature = "once_cell", issue = "74465")]
78+
#[unstable(feature = "lazy_cell", issue = "109736")]
7979
pub fn force(this: &LazyCell<T, F>) -> &T {
8080
this.cell.get_or_init(|| match this.init.take() {
8181
Some(f) => f(),
@@ -84,7 +84,7 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
8484
}
8585
}
8686

87-
#[unstable(feature = "once_cell", issue = "74465")]
87+
#[unstable(feature = "lazy_cell", issue = "109736")]
8888
impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
8989
type Target = T;
9090
#[inline]
@@ -93,7 +93,7 @@ impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
9393
}
9494
}
9595

96-
#[unstable(feature = "once_cell", issue = "74465")]
96+
#[unstable(feature = "lazy_cell", issue = "109736")]
9797
impl<T: Default> Default for LazyCell<T> {
9898
/// Creates a new lazy value using `Default` as the initializing function.
9999
#[inline]
@@ -102,7 +102,7 @@ impl<T: Default> Default for LazyCell<T> {
102102
}
103103
}
104104

105-
#[unstable(feature = "once_cell", issue = "74465")]
105+
#[unstable(feature = "lazy_cell", issue = "109736")]
106106
impl<T: fmt::Debug, F> fmt::Debug for LazyCell<T, F> {
107107
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108108
f.debug_struct("Lazy").field("cell", &self.cell).field("init", &"..").finish()

0 commit comments

Comments
 (0)