#[repr(transparent)]pub struct Cell<T: ?Sized> {
value: UnsafeCell<T>,
}
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.
Fields§
§value: UnsafeCell<T>
Implementations§
Source§impl<T> Cell<T>
impl<T> Cell<T>
1.17.0 · Sourcepub fn swap(&self, other: &Self)
pub fn swap(&self, other: &Self)
Swaps the values of two Cell
s.
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 Cell
s that partially overlap.
(Using just standard library methods, it is impossible to create such partially overlapping Cell
s.
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
pub const fn into_inner(self) -> T
Unwraps the value, consuming the cell.
§Examples
Source§impl<T: Copy> Cell<T>
impl<T: Copy> Cell<T>
Source§impl<T: ?Sized> Cell<T>
impl<T: ?Sized> Cell<T>
1.12.0 (const: 1.32.0) · Sourcepub const fn as_ptr(&self) -> *mut T
pub const fn as_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 fn get_mut(&mut self) -> &mut T
pub const fn get_mut(&mut self) -> &mut T
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]>
impl<T> Cell<[T]>
1.37.0 (const: 1.88.0) · Sourcepub const fn as_slice_of_cells(&self) -> &[Cell<T>]
pub const fn as_slice_of_cells(&self) -> &[Cell<T>]
Returns a &[Cell<T>]
from a &Cell<[T]>
§Examples
Trait Implementations§
1.2.0 · Source§impl<T: Eq + Copy> Eq for Cell<T>
impl<T: Eq + Copy> Eq for Cell<T>
#[doc(hidden)] fn assert_receiver_is_total_eq(&self)
1.10.0 · Source§impl<T: Ord + Copy> Ord for Cell<T>
impl<T: Ord + Copy> Ord for Cell<T>
1.10.0 · Source§impl<T: PartialOrd + Copy> PartialOrd for Cell<T>
impl<T: PartialOrd + Copy> PartialOrd for Cell<T>
Source§#[doc(hidden)] fn __chaining_lt(&self, other: &Rhs) -> ControlFlow<bool>
#[doc(hidden)] fn __chaining_lt(&self, other: &Rhs) -> ControlFlow<bool>
partial_ord_chaining_methods
)self == other
, returns ControlFlow::Continue(())
.
Otherwise, returns ControlFlow::Break(self < other)
. Read moreSource§#[doc(hidden)] fn __chaining_le(&self, other: &Rhs) -> ControlFlow<bool>
#[doc(hidden)] fn __chaining_le(&self, other: &Rhs) -> ControlFlow<bool>
partial_ord_chaining_methods
)__chaining_lt
, but for <=
instead of <
.Source§#[doc(hidden)] fn __chaining_gt(&self, other: &Rhs) -> ControlFlow<bool>
#[doc(hidden)] fn __chaining_gt(&self, other: &Rhs) -> ControlFlow<bool>
partial_ord_chaining_methods
)__chaining_lt
, but for >
instead of <
.Source§#[doc(hidden)] fn __chaining_ge(&self, other: &Rhs) -> ControlFlow<bool>
#[doc(hidden)] fn __chaining_ge(&self, other: &Rhs) -> ControlFlow<bool>
partial_ord_chaining_methods
)__chaining_lt
, but for >=
instead of <
.impl<T: CoerceUnsized<U>, U> CoerceUnsized<Cell<U>> for Cell<T>
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<Cell<U>> for Cell<T>
impl<T: ?Sized> PinCoerceUnsized for Cell<T>
impl<T: PointerLike> PointerLike for Cell<T>
impl<T> Send for Cell<T>
impl<T: ?Sized> !Sync for Cell<T>
Auto Trait Implementations§
impl<T> !Freeze for Cell<T>
impl<T> !RefUnwindSafe for Cell<T>
impl<T> Unpin for Cell<T>
impl<T> UnsafeUnpin for Cell<T>where
T: UnsafeUnpin + ?Sized,
impl<T> UnwindSafe for Cell<T>where
T: UnwindSafe + ?Sized,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)] const IS_ZST: bool = _
#[doc(hidden)] const IS_ZST: bool = _
sized_type_properties
)Source§#[doc(hidden)] const LAYOUT: Layout = _
#[doc(hidden)] const LAYOUT: Layout = _
sized_type_properties
)Source§#[doc(hidden)] const MAX_SLICE_LEN: usize = _
#[doc(hidden)] const MAX_SLICE_LEN: usize = _
sized_type_properties
)[Self]
. Read more