pub struct Cell<T>where T: ?Sized,{/* private fields */ }Expand description
A mutable memory location.
§Memory layout
Cell<T> has the samememory layout and caveats asUnsafeCell<T>. In particular, this means thatCell<T> has the same in-memory representation as its inner typeT.
§Examples
In this example, you can see thatCell<T> enables mutation inside animmutable struct. In other words, it enables “interior mutability”.
usestd::cell::Cell;structSomeStruct { regular_field: u8, special_field: Cell<u8>,}letmy_struct = SomeStruct { regular_field:0, special_field: Cell::new(1),};letnew_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 mutatedmy_struct.special_field.set(new_value);assert_eq!(my_struct.special_field.get(), new_value);See themodule-level documentation for more.
Implementations§
Source§impl<T>Cell<T>
impl<T>Cell<T>
1.17.0 ·Sourcepub fnswap(&self, other: &Cell<T>)
pub fnswap(&self, other: &Cell<T>)
Swaps the values of twoCells.
The difference withstd::mem::swap is that this function doesn’trequire a&mut reference.
§Panics
This function will panic ifself andother are differentCells that partially overlap.(Using just standard library methods, it is impossible to create such partially overlappingCells.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 fnreplace(&self, val: T) -> T
pub const fnreplace(&self, val: T) -> T
Replaces the contained value withval, and returns the old contained value.
§Examples
1.17.0 (const: 1.83.0) ·Sourcepub const fninto_inner(self) -> T
pub const fninto_inner(self) -> T
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 fnas_ptr(&self) ->*mut T
pub const fnas_ptr(&self) ->*mut T
Returns a raw pointer to the underlying data in this cell.
§Examples
1.11.0 (const: 1.88.0) ·Sourcepub const fnget_mut(&mut self) ->&mut T
pub const fnget_mut(&mut self) ->&mut T
Returns a mutable reference to the underlying data.
This call borrowsCell mutably (at compile-time) which guaranteesthat we possess the only reference.
However be cautious: this method expectsself to be mutable, which isgenerally not the case when using aCell. If you require interiormutability by reference, consider usingRefCell which providesrun-time checked mutable borrows through itsborrow_mut method.
§Examples
Source§impl<T>Cell<[T]>
impl<T>Cell<[T]>
1.37.0 (const: 1.88.0) ·Sourcepub const fnas_slice_of_cells(&self) -> &[Cell<T>]
pub const fnas_slice_of_cells(&self) -> &[Cell<T>]
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 fnas_array_of_cells(&self) -> &[Cell<T>;N]
pub const fnas_array_of_cells(&self) -> &[Cell<T>;N]
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 fnget_cloned(&self) ->Cell<T>
🔬This is a nightly-only experimental API. (cell_get_cloned #145329)
pub fnget_cloned(&self) ->Cell<T>
cell_get_cloned #145329)Get a clone of theCell that contains a copy of the original value.
This allows a cheaplyClone-able type like anRc to be stored in aCell, exposing thecheaperclone() method.