Struct Cell

Source
pub struct Cell<T>(/* private fields */);
Expand description

A Cell type with methods for by-reference mutation and inspection.

Implementations§

Source§

impl<T> Cell<T>

Source

pub fn new(value: T) -> Self

Creates a new Cell with the given value.

Source§

impl<T: Copy> Cell<T>

Source

pub fn get(&self) -> T

Gets the value held by the cell.

Source

pub fn with<F, R>(&self, f: F) -> R
where F: FnOnce(&T) -> R,

Calls f with a reference to the contents of the cell.

Source

pub fn with_mut<F, R>(&self, f: F) -> R
where F: FnOnce(&mut T) -> R,

Calls f with a mutable reference to the contents of the cell.

Methods from Deref<Target = StdCell<T>>§

1.0.0 · Source

pub fn set(&self, val: T)

Sets the contained value.

§Examples
use std::cell::Cell;

let c = Cell::new(5);

c.set(10);
1.17.0 · Source

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 · Source

pub 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.0.0 · Source

pub fn get(&self) -> T

Returns a copy of the contained value.

§Examples
use std::cell::Cell;

let c = Cell::new(5);

let five = c.get();
Source

pub fn update(&self, f: impl FnOnce(T) -> T)

🔬This is a nightly-only experimental API. (cell_update)

Updates the contained value using a function.

§Examples
#![feature(cell_update)]

use std::cell::Cell;

let c = Cell::new(5);
c.update(|x| x + 1);
assert_eq!(c.get(), 6);
1.12.0 · Source

pub 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 · Source

pub 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.17.0 · Source

pub fn take(&self) -> T

Takes the value of the cell, leaving Default::default() in its place.

§Examples
use std::cell::Cell;

let c = Cell::new(5);
let five = c.take();

assert_eq!(five, 5);
assert_eq!(c.into_inner(), 0);

Trait Implementations§

Source§

impl<T> CellExt<T> for Cell<T>

Source§

fn get(&self) -> T
where T: Clone + Default,

Gets the value held by the cell.
Source§

fn with<F, R>(&self, f: F) -> R
where T: Default, F: FnOnce(&T) -> R,

Calls f with a reference to the contents of the cell.
Source§

fn with_mut<F, R>(&self, f: F) -> R
where T: Default, F: FnOnce(&mut T) -> R,

Calls f with a mutable reference to the contents of the cell.
Source§

impl<T: Copy> Clone for Cell<T>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Copy> Debug for Cell<T>

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for Cell<T>

Source§

fn default() -> Cell<T>

Returns the “default value” for a type. Read more
Source§

impl<T> Deref for Cell<T>

Source§

type Target = Cell<T>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T> DerefMut for Cell<T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T> From<Cell<T>> for Cell<T>

Source§

fn from(cell: StdCell<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Cell<T>> for Cell<T>

Source§

fn from(cell: Cell<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<T> for Cell<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T: Ord + Copy> Ord for Cell<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq + Copy> PartialEq for Cell<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd + Copy> PartialOrd for Cell<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Eq + Copy> Eq for Cell<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Cell<T>

§

impl<T> !RefUnwindSafe for Cell<T>

§

impl<T> Send for Cell<T>
where T: Send,

§

impl<T> !Sync for Cell<T>

§

impl<T> Unpin for Cell<T>
where T: Unpin,

§

impl<T> UnwindSafe for Cell<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.