WithCell

Struct WithCell 

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

Cell-like container for making shared structures with mutable methods more convenient to use.

Unlike Cell, this wrapper does not require Copy in the general case. Instead, it relies on Default.

Internally, it uses Cell.

Implementations§

Source§

impl<T> WithCell<T>

Source

pub const fn new(value: T) -> Self

Create a new WithCell containing the given value.

Source§

impl<T> WithCell<T>
where T: Default,

Source

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

Perform an operation on the contained value.

This takes the value out of the cell and replaces it with its Default variant. This value is then passed by reference to f. When f returns, the value is put back in the cell, discarding the stub variant.

Source

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

Perform an operation on the contained value.

This takes the value out of the cell and replaces it with its Default variant. This value is then passed by reference to f. When f returns, the value is put back in the cell, discarding the stub variant.

This method is almost identical to with, except it returns a reference to self.

Source

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

Perform an operation on the contained value.

Like with, it replaces the value with its Default variant. This value is then passed by value to f. The value returned from f is put in the cell, discarding the stub variant.

This function can be chained:

let abcd = with_cell::WithCell::new(String::new());

abcd.map(|x| x + "a")
    .map(|x| x + "b")
    .map(|x| x + "cd")
    .map(|x| dbg!(x))
    .with(|x| x.clear());

Methods from Deref<Target = Cell<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();
1.88.0 · Source

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

Updates the contained value using a function.

§Examples
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
where T: Default,

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);
Source

pub fn get_cloned(&self) -> Cell<T>

🔬This is a nightly-only experimental API. (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§

Source§

impl<T> Clone for WithCell<T>
where T: Default + Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate 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 for WithCell<T>
where T: Default + Debug,

Source§

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

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

impl<T: Default> Default for WithCell<T>

Source§

fn default() -> WithCell<T>

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

impl<T> Deref for WithCell<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 WithCell<T>

Source§

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

Mutably dereferences the value.
Source§

impl<T> From<T> for WithCell<T>

Source§

fn from(x: T) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> !Freeze for WithCell<T>

§

impl<T> !RefUnwindSafe for WithCell<T>

§

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

§

impl<T> !Sync for WithCell<T>

§

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

§

impl<T> UnwindSafe for WithCell<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.