Documentation updates to better share the purpose of OnceCell/OnceLock

This commit is contained in:
Trevor Gross 2022-12-12 04:16:18 -05:00 committed by Trevor Gross
parent dc4ba57566
commit d1b28b75d2
3 changed files with 76 additions and 28 deletions

View File

@ -11,36 +11,77 @@
//! mutate it. //! mutate it.
//! //!
//! Shareable mutable containers exist to permit mutability in a controlled manner, even in the //! Shareable mutable containers exist to permit mutability in a controlled manner, even in the
//! presence of aliasing. Both [`Cell<T>`] and [`RefCell<T>`] allow doing this in a single-threaded //! presence of aliasing. [`Cell<T>`], [`RefCell<T>`], and [`OnceCell<T>`] allow doing this in
//! way. However, neither `Cell<T>` nor `RefCell<T>` are thread safe (they do not implement //! a single-threaded way - they do not implement [`Sync`]. (If you need to do aliasing and
//! [`Sync`]). If you need to do aliasing and mutation between multiple threads it is possible to //! mutation among multiple threads, [`Mutex<T>`], [`RwLock<T>`], [`OnceLock<T>`] or [`atomic`]
//! use [`Mutex<T>`], [`RwLock<T>`] or [`atomic`] types. //! types are the correct data structures to do so).
//! //!
//! Values of the `Cell<T>` and `RefCell<T>` types may be mutated through shared references (i.e. //! Values of the `Cell<T>`, `RefCell<T>`, and `OnceCell<T>` types may be mutated through shared
//! the common `&T` type), whereas most Rust types can only be mutated through unique (`&mut T`) //! references (i.e. the common `&T` type), whereas most Rust types can only be mutated through
//! references. We say that `Cell<T>` and `RefCell<T>` provide 'interior mutability', in contrast //! unique (`&mut T`) references. We say these cell types provide 'interior mutability'
//! with typical Rust types that exhibit 'inherited mutability'. //! (mutable via `&T`), in contrast with typical Rust types that exhibit 'inherited mutability'
//! (mutable only via `&mut T`).
//! //!
//! Cell types come in two flavors: `Cell<T>` and `RefCell<T>`. `Cell<T>` implements interior //! Cell types come in three flavors: `Cell<T>`, `RefCell<T>`, and `OnceCell<T>`. Each provides
//! mutability by moving values in and out of the `Cell<T>`. To use references instead of values, //! a different way of providing safe interior mutability.
//! one must use the `RefCell<T>` type, acquiring a write lock before mutating. `Cell<T>` provides //!
//! methods to retrieve and change the current interior value: //! ## `Cell<T>`
//!
//! [`Cell<T>`] implements interior mutability by moving values in and out of the cell. That is, an
//! `&mut T` to the inner value can never be obtained, and the value itself cannot be directly
//! obtained without replacing it with something else. Both of these rules ensure that there is
//! never more than one reference pointing to the inner value. This type provides the following
//! methods:
//! //!
//! - For types that implement [`Copy`], the [`get`](Cell::get) method retrieves the current //! - For types that implement [`Copy`], the [`get`](Cell::get) method retrieves the current
//! interior value. //! interior value by duplicating it.
//! - For types that implement [`Default`], the [`take`](Cell::take) method replaces the current //! - For types that implement [`Default`], the [`take`](Cell::take) method replaces the current
//! interior value with [`Default::default()`] and returns the replaced value. //! interior value with [`Default::default()`] and returns the replaced value.
//! - For all types, the [`replace`](Cell::replace) method replaces the current interior value and //! - All types have:
//! returns the replaced value and the [`into_inner`](Cell::into_inner) method consumes the //! - [`replace`](Cell::replace): replaces the current interior value and returns the replaced
//! `Cell<T>` and returns the interior value. Additionally, the [`set`](Cell::set) method //! value.
//! replaces the interior value, dropping the replaced value. //! - [`into_inner`](Cell::into_inner): this method consumes the `Cell<T>` and returns the
//! interior value.
//! - [`set`](Cell::set): this method replaces the interior value, dropping the replaced value.
//! //!
//! `RefCell<T>` uses Rust's lifetimes to implement 'dynamic borrowing', a process whereby one can //! `Cell<T>` is typically used for more simple types where copying or moving values isn't too
//! resource intensive (e.g. numbers), and should usually be preferred over other cell types when
//! possible. For larger and non-copy types, `RefCell` provides some advantages.
//!
//! ## `RefCell<T>`
//!
//! [`RefCell<T>`] uses Rust's lifetimes to implement "dynamic borrowing", a process whereby one can
//! claim temporary, exclusive, mutable access to the inner value. Borrows for `RefCell<T>`s are //! claim temporary, exclusive, mutable access to the inner value. Borrows for `RefCell<T>`s are
//! tracked 'at runtime', unlike Rust's native reference types which are entirely tracked //! tracked at _runtime_, unlike Rust's native reference types which are entirely tracked
//! statically, at compile time. Because `RefCell<T>` borrows are dynamic it is possible to attempt //! statically, at compile time.
//! to borrow a value that is already mutably borrowed; when this happens it results in thread //!
//! panic. //! An immutable reference to a `RefCell`'s inner value (`&T`) can be obtained with
//! [`borrow`](`RefCell::borrow`), and a mutable borrow (`&mut T`) can be obtained with
//! [`borrow_mut`](`RefCell::borrow_mut`). When these functions are called, they first verify that
//! Rust's borrow rules will be satisfied: any number of immutable borrows are allowed or a
//! single immutable borrow is allowed, but never both. If a borrow is attempted that would violate
//! these rules, the thread will panic.
//!
//! The corresponding [`Sync`] version of `RefCell<T>` is [`RwLock<T>`].
//!
//! ## `OnceCell<T>`
//!
//! [`OnceCell<T>`] is somewhat of a hybrid of `Cell` and `RefCell` that works for values that
//! typically only need to be set once. This means that a reference `&T` can be obtained without
//! moving or copying the inner value (unlike `Cell`) but also without runtime checks (unlike
//! `RefCell`). However, its value can also not be updated once set unless you have a mutable
//! reference to the `OnceCell`.
//!
//! `OnceCell` provides the following methods:
//!
//! - [`get`](OnceCell::get): obtain a reference to the inner value
//! - [`set`](OnceCell::set): set the inner value if it is unset (returns a `Result`)
//! - [`get_or_init`](OnceCell::get_or_init): return the inner value, initializing it if needed
//! - [`get_mut`](OnceCell::get_mut): provide a mutable reference to the inner value, only available
//! if you have a mutable reference to the cell itself.
//!
//! The corresponding [`Sync`] version of `OnceCell<T>` is [`OnceLock<T>`].
//!
//! //!
//! # When to choose interior mutability //! # When to choose interior mutability
//! //!
@ -188,6 +229,8 @@
//! [`Rc<T>`]: ../../std/rc/struct.Rc.html //! [`Rc<T>`]: ../../std/rc/struct.Rc.html
//! [`RwLock<T>`]: ../../std/sync/struct.RwLock.html //! [`RwLock<T>`]: ../../std/sync/struct.RwLock.html
//! [`Mutex<T>`]: ../../std/sync/struct.Mutex.html //! [`Mutex<T>`]: ../../std/sync/struct.Mutex.html
//! [`OnceLock<T>`]: ../../std/sync/struct.OnceLock.html
//! [`Sync`]: ../../std/marker/trait.Sync.html
//! [`atomic`]: crate::sync::atomic //! [`atomic`]: crate::sync::atomic
#![stable(feature = "rust1", since = "1.0.0")] #![stable(feature = "rust1", since = "1.0.0")]
@ -419,7 +462,7 @@ impl<T> Cell<T> {
mem::replace(unsafe { &mut *self.value.get() }, val) mem::replace(unsafe { &mut *self.value.get() }, val)
} }
/// Unwraps the value. /// Unwraps the value, consuming the cell.
/// ///
/// # Examples /// # Examples
/// ///
@ -1969,7 +2012,7 @@ impl<T> UnsafeCell<T> {
UnsafeCell { value } UnsafeCell { value }
} }
/// Unwraps the value. /// Unwraps the value, consuming the cell.
/// ///
/// # Examples /// # Examples
/// ///
@ -2133,7 +2176,7 @@ impl<T> SyncUnsafeCell<T> {
Self { value: UnsafeCell { value } } Self { value: UnsafeCell { value } }
} }
/// Unwraps the value. /// Unwraps the value, consuming the cell.
#[inline] #[inline]
pub const fn into_inner(self) -> T { pub const fn into_inner(self) -> T {
self.value.into_inner() self.value.into_inner()

View File

@ -4,8 +4,10 @@ use crate::mem;
/// A cell which can be written to only once. /// A cell which can be written to only once.
/// ///
/// Unlike [`RefCell`], a `OnceCell` only provides shared `&T` references to its value. /// This allows obtaining a shared `&T` reference to its inner value without copying or replacing
/// Unlike [`Cell`], a `OnceCell` doesn't require copying or replacing the value to access it. /// it (unlike [`Cell`]), and without runtime borrow checks (unlike [`RefCell`]). However,
/// only immutable references can be obtained unless one has a mutable reference to the cell
/// itself.
/// ///
/// For a thread-safe version of this struct, see [`std::sync::OnceLock`]. /// For a thread-safe version of this struct, see [`std::sync::OnceLock`].
/// ///

View File

@ -133,7 +133,9 @@
//! - [`Mutex`]: Mutual Exclusion mechanism, which ensures that at //! - [`Mutex`]: Mutual Exclusion mechanism, which ensures that at
//! most one thread at a time is able to access some data. //! most one thread at a time is able to access some data.
//! //!
//! - [`Once`]: Used for thread-safe, one-time initialization of a //! - [`Once`]: Used for a thread-safe, one-time global initialization routine
//!
//! - [`OnceLock`]: Used for thread-safe, one-time initialization of a
//! global variable. //! global variable.
//! //!
//! - [`RwLock`]: Provides a mutual exclusion mechanism which allows //! - [`RwLock`]: Provides a mutual exclusion mechanism which allows
@ -147,6 +149,7 @@
//! [`mpsc`]: crate::sync::mpsc //! [`mpsc`]: crate::sync::mpsc
//! [`Mutex`]: crate::sync::Mutex //! [`Mutex`]: crate::sync::Mutex
//! [`Once`]: crate::sync::Once //! [`Once`]: crate::sync::Once
//! [`OnceLock`]: crate::sync::OnceLock
//! [`RwLock`]: crate::sync::RwLock //! [`RwLock`]: crate::sync::RwLock
#![stable(feature = "rust1", since = "1.0.0")] #![stable(feature = "rust1", since = "1.0.0")]