pub struct Cell<T>where
T: ?Sized,{ /* private fields */ }Expand description
A mutable memory location.
§Memory layout
Cell<T> has the same memory layout and caveats as
UnsafeCell<T>. In particular, this means that
Cell<T> has the same in-memory representation as its inner type T.
§Examples
In this example, you can see that Cell<T> enables mutation inside an
immutable struct. In other words, it enables “interior mutability”.
use std::cell::Cell;
struct SomeStruct {
regular_field: u8,
special_field: Cell<u8>,
}
let my_struct = SomeStruct {
regular_field: 0,
special_field: Cell::new(1),
};
let new_value = 100;
// ERROR: `my_struct` is immutable
// my_struct.regular_field = new_value;
// WORKS: although `my_struct` is immutable, `special_field` is a `Cell`,
// which can always be mutated
my_struct.special_field.set(new_value);
assert_eq!(my_struct.special_field.get(), new_value);See the module-level documentation for more.
Implementations§
Source§impl<T> Cell<T>
impl<T> Cell<T>
1.17.0 · Sourcepub fn swap(&self, other: &Cell<T>)
Available on non-crate feature ferrocene_certified only.
pub fn swap(&self, other: &Cell<T>)
ferrocene_certified only.Swaps the values of two Cells.
The difference with std::mem::swap is that this function doesn’t
require a &mut reference.
§Panics
This function will panic if self and other are different Cells that partially overlap.
(Using just standard library methods, it is impossible to create such partially overlapping Cells.
However, unsafe code is allowed to e.g. create two &Cell<[i32; 2]> that partially overlap.)
§Examples
1.17.0 (const: 1.88.0) · Sourcepub const fn replace(&self, val: T) -> T
pub const fn replace(&self, val: T) -> T
Replaces the contained value with val, and returns the old contained value.
§Examples
1.17.0 (const: 1.83.0) · Sourcepub const fn into_inner(self) -> T
Available on non-crate feature ferrocene_certified only.
pub const fn into_inner(self) -> T
ferrocene_certified only.Unwraps the value, consuming the cell.
§Examples
Source§impl<T> Cell<T>where
T: Copy,
impl<T> Cell<T>where
T: Copy,
Source§impl<T> Cell<T>where
T: ?Sized,
impl<T> Cell<T>where
T: ?Sized,
1.12.0 (const: 1.32.0) · Sourcepub const fn as_ptr(&self) -> *mut T
Available on non-crate feature ferrocene_certified only.
pub const fn as_ptr(&self) -> *mut T
ferrocene_certified only.Returns a raw pointer to the underlying data in this cell.
§Examples
1.11.0 (const: 1.88.0) · Sourcepub const fn get_mut(&mut self) -> &mut T
Available on non-crate feature ferrocene_certified only.
pub const fn get_mut(&mut self) -> &mut T
ferrocene_certified only.Returns a mutable reference to the underlying data.
This call borrows Cell mutably (at compile-time) which guarantees
that we possess the only reference.
However be cautious: this method expects self to be mutable, which is
generally not the case when using a Cell. If you require interior
mutability by reference, consider using RefCell which provides
run-time checked mutable borrows through its borrow_mut method.
§Examples
Source§impl<T> Cell<T>where
T: Default,
impl<T> Cell<T>where
T: Default,
Source§impl<T> Cell<[T]>
impl<T> Cell<[T]>
1.37.0 (const: 1.88.0) · Sourcepub const fn as_slice_of_cells(&self) -> &[Cell<T>]
Available on non-crate feature ferrocene_certified only.
pub const fn as_slice_of_cells(&self) -> &[Cell<T>]
ferrocene_certified only.Returns a &[Cell<T>] from a &Cell<[T]>
§Examples
Source§impl<T, const N: usize> Cell<[T; N]>
impl<T, const N: usize> Cell<[T; N]>
1.91.0 (const: 1.91.0) · Sourcepub const fn as_array_of_cells(&self) -> &[Cell<T>; N]
Available on non-crate feature ferrocene_certified only.
pub const fn as_array_of_cells(&self) -> &[Cell<T>; N]
ferrocene_certified only.Returns a &[Cell<T>; N] from a &Cell<[T; N]>
§Examples
Source§impl<T> Cell<T>where
T: CloneFromCell,
impl<T> Cell<T>where
T: CloneFromCell,
Sourcepub fn get_cloned(&self) -> Cell<T>
🔬This is a nightly-only experimental API. (cell_get_cloned #145329)Available on non-crate feature ferrocene_certified only.
pub fn get_cloned(&self) -> Cell<T>
cell_get_cloned #145329)ferrocene_certified only.Get a clone of the Cell that contains a copy of the original value.
This allows a cheaply Clone-able type like an Rc to be stored in a Cell, exposing the
cheaper clone() method.
§Examples
Trait Implementations§
1.0.0 · Source§impl<T> Clone for Cell<T>where
T: Copy,
Available on non-crate feature ferrocene_certified only.
impl<T> Clone for Cell<T>where
T: Copy,
ferrocene_certified only.1.0.0 (const: unstable) · Source§impl<T> Default for Cell<T>where
T: Default,
Available on non-crate feature ferrocene_certified only.
impl<T> Default for Cell<T>where
T: Default,
ferrocene_certified only.1.12.0 (const: unstable) · Source§impl<T> From<T> for Cell<T>
Available on non-crate feature ferrocene_certified only.
impl<T> From<T> for Cell<T>
ferrocene_certified only.1.10.0 · Source§impl<T> Ord for Cell<T>
Available on non-crate feature ferrocene_certified only.
impl<T> Ord for Cell<T>
ferrocene_certified only.1.0.0 · Source§impl<T> PartialEq for Cell<T>
Available on non-crate feature ferrocene_certified only.
impl<T> PartialEq for Cell<T>
ferrocene_certified only.1.10.0 · Source§impl<T> PartialOrd for Cell<T>where
T: PartialOrd + Copy,
Available on non-crate feature ferrocene_certified only.
impl<T> PartialOrd for Cell<T>where
T: PartialOrd + Copy,
ferrocene_certified only.impl<T, U> CoerceUnsized<Cell<U>> for Cell<T>where
T: CoerceUnsized<U>,
ferrocene_certified only.impl<T, U> DispatchFromDyn<Cell<U>> for Cell<T>where
T: DispatchFromDyn<U>,
ferrocene_certified only.impl<T> Eq for Cell<T>
ferrocene_certified only.impl<T> PinCoerceUnsized for Cell<T>where
T: ?Sized,
ferrocene_certified only.