|
11 | 11 | //! mutate it. |
12 | 12 | //! |
13 | 13 | //! 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). |
18 | 18 | //! |
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`). |
23 | 24 | //! |
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: |
28 | 35 | //! |
29 | 36 | //! - For types that implement [`Copy`], the [`get`](Cell::get) method retrieves the current |
30 | | -//! interior value. |
| 37 | +//! interior value by duplicating it. |
31 | 38 | //! - For types that implement [`Default`], the [`take`](Cell::take) method replaces the current |
32 | 39 | //! 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. |
37 | 50 | //! |
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 |
39 | 54 | //! 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 | +//! |
44 | 85 | //! |
45 | 86 | //! # When to choose interior mutability |
46 | 87 | //! |
|
188 | 229 | //! [`Rc<T>`]: ../../std/rc/struct.Rc.html |
189 | 230 | //! [`RwLock<T>`]: ../../std/sync/struct.RwLock.html |
190 | 231 | //! [`Mutex<T>`]: ../../std/sync/struct.Mutex.html |
| 232 | +//! [`OnceLock<T>`]: ../../std/sync/struct.OnceLock.html |
| 233 | +//! [`Sync`]: ../../std/marker/trait.Sync.html |
191 | 234 | //! [`atomic`]: crate::sync::atomic |
192 | 235 |
|
193 | 236 | #![stable(feature = "rust1", since = "1.0.0")] |
@@ -419,7 +462,7 @@ impl<T> Cell<T> { |
419 | 462 | mem::replace(unsafe { &mut *self.value.get() }, val) |
420 | 463 | } |
421 | 464 |
|
422 | | - /// Unwraps the value. |
| 465 | + /// Unwraps the value, consuming the cell. |
423 | 466 | /// |
424 | 467 | /// # Examples |
425 | 468 | /// |
@@ -1969,7 +2012,7 @@ impl<T> UnsafeCell<T> { |
1969 | 2012 | UnsafeCell { value } |
1970 | 2013 | } |
1971 | 2014 |
|
1972 | | - /// Unwraps the value. |
| 2015 | + /// Unwraps the value, consuming the cell. |
1973 | 2016 | /// |
1974 | 2017 | /// # Examples |
1975 | 2018 | /// |
@@ -2133,7 +2176,7 @@ impl<T> SyncUnsafeCell<T> { |
2133 | 2176 | Self { value: UnsafeCell { value } } |
2134 | 2177 | } |
2135 | 2178 |
|
2136 | | - /// Unwraps the value. |
| 2179 | + /// Unwraps the value, consuming the cell. |
2137 | 2180 | #[inline] |
2138 | 2181 | pub const fn into_inner(self) -> T { |
2139 | 2182 | self.value.into_inner() |
|
0 commit comments