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>)
pub fn swap(&self, other: &Cell<T>)
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
use std::cell::Cell;
let c1 = Cell::new(5i32);
let c2 = Cell::new(10i32);
c1.swap(&c2);
assert_eq!(10, c1.get());
assert_eq!(5, c2.get());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
use std::cell::Cell;
let cell = Cell::new(5);
assert_eq!(cell.get(), 5);
assert_eq!(cell.replace(10), 5);
assert_eq!(cell.get(), 10);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
use std::cell::Cell;
let c = Cell::new(5);
let five = c.into_inner();
assert_eq!(five, 5);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
pub const fn as_ptr(&self) -> *mut T
Returns a raw pointer to the underlying data in this cell.
§Examples
use std::cell::Cell;
let c = Cell::new(5);
let ptr = c.as_ptr();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
use std::cell::Cell;
let mut c = Cell::new(5);
*c.get_mut() += 1;
assert_eq!(c.get(), 6);1.37.0 (const: 1.88.0) · Sourcepub const fn from_mut(t: &mut T) -> &Cell<T>
pub const fn from_mut(t: &mut T) -> &Cell<T>
Returns a &Cell<T> from a &mut T
§Examples
use std::cell::Cell;
let slice: &mut [i32] = &mut [1, 2, 3];
let cell_slice: &Cell<[i32]> = Cell::from_mut(slice);
let slice_cell: &[Cell<i32>] = cell_slice.as_slice_of_cells();
assert_eq!(slice_cell.len(), 3);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
use std::cell::Cell;
let slice: &mut [i32] = &mut [1, 2, 3];
let cell_slice: &Cell<[i32]> = Cell::from_mut(slice);
let slice_cell: &[Cell<i32>] = cell_slice.as_slice_of_cells();
assert_eq!(slice_cell.len(), 3);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]
pub const fn as_array_of_cells(&self) -> &[Cell<T>; N]
Returns a &[Cell<T>; N] from a &Cell<[T; N]>
§Examples
use std::cell::Cell;
let mut array: [i32; 3] = [1, 2, 3];
let cell_array: &Cell<[i32; 3]> = Cell::from_mut(&mut array);
let array_cell: &[Cell<i32>; 3] = cell_array.as_array_of_cells();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)
pub fn get_cloned(&self) -> Cell<T>
cell_get_cloned)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
#![feature(cell_get_cloned)]
use core::cell::Cell;
use std::rc::Rc;
let rc = Rc::new(1usize);
let c1 = Cell::new(rc);
let c2 = c1.get_cloned();
assert_eq!(*c2.into_inner(), 1);Trait Implementations§
1.2.0 · Source§impl<T> Eq for Cell<T>
impl<T> Eq for Cell<T>
#[doc(hidden)] fn assert_receiver_is_total_eq(&self)
Source§impl<T: ?Sized + FromBytes> FromBytes for Cell<T>
impl<T: ?Sized + FromBytes> FromBytes for Cell<T>
fn only_derive_is_allowed_to_implement_this_trait()
Source§fn mut_from_bytes(
source: &mut [u8],
) -> Result<&mut Self, CastError<&mut [u8], Self>>where
Self: IntoBytes + KnownLayout,
fn mut_from_bytes(
source: &mut [u8],
) -> Result<&mut Self, CastError<&mut [u8], Self>>where
Self: IntoBytes + KnownLayout,
Source§fn mut_from_prefix(
source: &mut [u8],
) -> Result<(&mut Self, &mut [u8]), CastError<&mut [u8], Self>>where
Self: IntoBytes + KnownLayout,
fn mut_from_prefix(
source: &mut [u8],
) -> Result<(&mut Self, &mut [u8]), CastError<&mut [u8], Self>>where
Self: IntoBytes + KnownLayout,
Source§fn mut_from_suffix(
source: &mut [u8],
) -> Result<(&mut [u8], &mut Self), CastError<&mut [u8], Self>>where
Self: IntoBytes + KnownLayout,
fn mut_from_suffix(
source: &mut [u8],
) -> Result<(&mut [u8], &mut Self), CastError<&mut [u8], Self>>where
Self: IntoBytes + KnownLayout,
Source§fn mut_from_prefix_with_elems(
source: &mut [u8],
count: usize,
) -> Result<(&mut Self, &mut [u8]), CastError<&mut [u8], Self>>
fn mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), CastError<&mut [u8], Self>>
Source§fn mut_from_suffix_with_elems(
source: &mut [u8],
count: usize,
) -> Result<(&mut [u8], &mut Self), CastError<&mut [u8], Self>>
fn mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), CastError<&mut [u8], Self>>
Source§fn read_from_prefix(
source: &[u8],
) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>where
Self: Sized,
fn read_from_prefix(
source: &[u8],
) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>where
Self: Sized,
Source§fn read_from_suffix(
source: &[u8],
) -> Result<(&[u8], Self), SizeError<&[u8], Self>>where
Self: Sized,
fn read_from_suffix(
source: &[u8],
) -> Result<(&[u8], Self), SizeError<&[u8], Self>>where
Self: Sized,
Source§#[doc(hidden)] fn mut_from(source: &mut [u8]) -> Option<&mut Self>where
Self: KnownLayout + IntoBytes,
#[doc(hidden)] fn mut_from(source: &mut [u8]) -> Option<&mut Self>where
Self: KnownLayout + IntoBytes,
FromBytes::mut_from_bytesSource§#[doc(hidden)] fn mut_slice_from_prefix(
source: &mut [u8],
count: usize,
) -> Option<(&mut [Self], &mut [u8])>
#[doc(hidden)] fn mut_slice_from_prefix( source: &mut [u8], count: usize, ) -> Option<(&mut [Self], &mut [u8])>
FromBytes::mut_from_prefix_with_elemsSource§impl<T: ?Sized + IntoBytes> IntoBytes for Cell<T>
impl<T: ?Sized + IntoBytes> IntoBytes for Cell<T>
fn only_derive_is_allowed_to_implement_this_trait()
Source§fn as_mut_bytes(&mut self) -> &mut [u8]where
Self: FromBytes,
fn as_mut_bytes(&mut self) -> &mut [u8]where
Self: FromBytes,
Source§#[doc(hidden)] fn as_bytes_mut(&mut self) -> &mut [u8]where
Self: FromBytes,
#[doc(hidden)] fn as_bytes_mut(&mut self) -> &mut [u8]where
Self: FromBytes,
IntoBytes::as_bytes_mut was renamed to as_mut_bytesSource§impl<T: ?Sized + KnownLayout> KnownLayout for Cell<T>
impl<T: ?Sized + KnownLayout> KnownLayout for Cell<T>
Source§type PointerMetadata = <T as KnownLayout>::PointerMetadata
type PointerMetadata = <T as KnownLayout>::PointerMetadata
Self. Read moreSource§type MaybeUninit = <T as KnownLayout>::MaybeUninit
type MaybeUninit = <T as KnownLayout>::MaybeUninit
Self Read morefn only_derive_is_allowed_to_implement_this_trait()
Source§fn raw_from_ptr_len(
bytes: NonNull<u8>,
meta: <T as KnownLayout>::PointerMetadata,
) -> NonNull<Self>
fn raw_from_ptr_len( bytes: NonNull<u8>, meta: <T as KnownLayout>::PointerMetadata, ) -> NonNull<Self>
bytes. If Self is a DST, the returned pointer’s referent has elems
elements in its trailing slice.Source§fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata
fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata
Self. Read moreSource§#[doc(hidden)] fn size_of_val_raw(ptr: NonNull<Self>) -> Option<usize>
#[doc(hidden)] fn size_of_val_raw(ptr: NonNull<Self>) -> Option<usize>
ptr. Read more#[doc(hidden)] fn raw_dangling() -> NonNull<Self>
Source§fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>
fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>
Self with the given pointer
metadata. Read more1.10.0 · Source§impl<T> Ord for Cell<T>
impl<T> Ord for Cell<T>
1.10.0 · Source§impl<T> PartialOrd for Cell<T>where
T: PartialOrd + Copy,
impl<T> PartialOrd for Cell<T>where
T: PartialOrd + Copy,
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 <.Source§impl<T: ?Sized + TryFromBytes> TryFromBytes for Cell<T>
impl<T: ?Sized + TryFromBytes> TryFromBytes for Cell<T>
fn only_derive_is_allowed_to_implement_this_trait()
Source§fn is_bit_valid<A: Reference>(candidate: Maybe<'_, Self, A>) -> bool
fn is_bit_valid<A: Reference>(candidate: Maybe<'_, Self, A>) -> bool
Self? Read moreSource§fn try_mut_from_bytes(
bytes: &mut [u8],
) -> Result<&mut Self, TryCastError<&mut [u8], Self>>where
Self: KnownLayout + IntoBytes,
fn try_mut_from_bytes(
bytes: &mut [u8],
) -> Result<&mut Self, TryCastError<&mut [u8], Self>>where
Self: KnownLayout + IntoBytes,
Source§fn try_mut_from_prefix(
source: &mut [u8],
) -> Result<(&mut Self, &mut [u8]), TryCastError<&mut [u8], Self>>where
Self: KnownLayout + IntoBytes,
fn try_mut_from_prefix(
source: &mut [u8],
) -> Result<(&mut Self, &mut [u8]), TryCastError<&mut [u8], Self>>where
Self: KnownLayout + IntoBytes,
Source§fn try_mut_from_suffix(
source: &mut [u8],
) -> Result<(&mut [u8], &mut Self), TryCastError<&mut [u8], Self>>where
Self: KnownLayout + IntoBytes,
fn try_mut_from_suffix(
source: &mut [u8],
) -> Result<(&mut [u8], &mut Self), TryCastError<&mut [u8], Self>>where
Self: KnownLayout + IntoBytes,
Source§fn try_mut_from_bytes_with_elems(
source: &mut [u8],
count: usize,
) -> Result<&mut Self, TryCastError<&mut [u8], Self>>
fn try_mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, TryCastError<&mut [u8], Self>>
Source§fn try_mut_from_prefix_with_elems(
source: &mut [u8],
count: usize,
) -> Result<(&mut Self, &mut [u8]), TryCastError<&mut [u8], Self>>
fn try_mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), TryCastError<&mut [u8], Self>>
source as a &mut Self
with a DST length equal to count. Read moreSource§fn try_mut_from_suffix_with_elems(
source: &mut [u8],
count: usize,
) -> Result<(&mut [u8], &mut Self), TryCastError<&mut [u8], Self>>
fn try_mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), TryCastError<&mut [u8], Self>>
source as a &mut Self
with a DST length equal to count. Read moreSource§fn try_read_from_bytes(source: &[u8]) -> Result<Self, TryReadError<&[u8], Self>>where
Self: Sized,
fn try_read_from_bytes(source: &[u8]) -> Result<Self, TryReadError<&[u8], Self>>where
Self: Sized,
Source§fn try_read_from_prefix(
source: &[u8],
) -> Result<(Self, &[u8]), TryReadError<&[u8], Self>>where
Self: Sized,
fn try_read_from_prefix(
source: &[u8],
) -> Result<(Self, &[u8]), TryReadError<&[u8], Self>>where
Self: Sized,
Source§fn try_read_from_suffix(
source: &[u8],
) -> Result<(&[u8], Self), TryReadError<&[u8], Self>>where
Self: Sized,
fn try_read_from_suffix(
source: &[u8],
) -> Result<(&[u8], Self), TryReadError<&[u8], Self>>where
Self: Sized,
impl<T, U> CoerceUnsized<Cell<U>> for Cell<T>where
T: CoerceUnsized<U>,
impl<T, U> DispatchFromDyn<Cell<U>> for Cell<T>where
T: DispatchFromDyn<U>,
impl<T> PinCoerceUnsized for Cell<T>where
T: ?Sized,
impl<T> Send for Cell<T>
impl<T> !Sync for Cell<T>where
T: ?Sized,
Auto Trait Implementations§
impl<T> !Freeze for Cell<T>
impl<T> !RefUnwindSafe for Cell<T>
impl<T> Unpin for Cell<T>
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 SIZE: usize = _
#[doc(hidden)] const SIZE: usize = _
sized_type_properties)Source§#[doc(hidden)] const ALIGN: usize = _
#[doc(hidden)] const ALIGN: usize = _
sized_type_properties)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