Struct clone_cell::cell::Cell

source ·
#[repr(transparent)]
pub struct Cell<T>where
    T: ?Sized,
{ /* private fields */ }
Expand description

A mutable memory location with a get method that works with PureClone types.

Examples

Cell works with some non-Copy types such as Rc:

use std::rc::Rc;
use clone_cell::cell::Cell;

let x = Cell::new(Rc::new(0));
x.set(Rc::new(42));
assert_eq!(*x.get(), 42);

Implementations§

Creates a new Cell containing the given value.

Examples
use clone_cell::cell::Cell;

let c = Cell::new(42);

Sets the contained value.

Examples
use clone_cell::cell::Cell;

let c = Cell::new(42);
c.set(0);

Swaps the values of two Cells. Unlike std::mem::swap, this does not require a &mut reference.

Examples
use std::rc::Rc;
use clone_cell::cell::Cell;

let c1 = Cell::new(Rc::new(21));
let c2 = Cell::new(Rc::new(42));
c1.swap(&c2);
assert_eq!(42, *c1.get());
assert_eq!(21, *c2.get());

Replaces the contained value with value and returns the old value.

Examples
use std::rc::Rc;
use clone_cell::cell::Cell;

let c = Cell::new(Rc::new(42));
assert_eq!(*c.get(), 42);
assert_eq!(*c.replace(Rc::new(2)), 42);
assert_eq!(*c.get(), 2);

Unwraps the value.

Examples
use clone_cell::cell::Cell;

let c = Cell::new(42);
assert_eq!(c.into_inner(), 42);

Returns a copy of the contained value.

Examples
use std::rc::Rc;
use clone_cell::cell::Cell;

let p = Rc::new(42);
let c = Cell::new(Rc::downgrade(&p));
let p2 = c.get().upgrade().unwrap();
assert_eq!(*p, *p2);
assert_eq!(p, p2);
assert_eq!(Rc::strong_count(&p), 2);
assert_eq!(Rc::strong_count(&p2), 2);

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

Examples
use clone_cell::cell::Cell;

let c = Cell::new(42);
let i = c.take();
assert_eq!(i, 42);
assert_eq!(c.into_inner(), 0);

Returns a raw pointer to the underlying data in this Cell.

Examples
use clone_cell::cell::Cell;

let c = Cell::new(42);
let p = c.as_ptr();

Returns a mutable reference to the underlying data. This method requires &mut self, ensuring the caller has the only reference to it.

Examples
use clone_cell::cell::Cell;

let mut c = Cell::new(42);
*c.get_mut() += 1;
assert_eq!(c.get(), 43);

Returns a &Cell<T> from a &mut T.

Examples
use std::rc::Rc;
use clone_cell::cell::Cell;

let p = &mut Rc::new(42);
let c = Cell::from_mut(p);
assert_eq!(*c.get(), 42);

Returns a &[Cell<T>] from a &Cell<[T]>.

Examples
use std::rc::Rc;
use clone_cell::cell::Cell;

let s: &mut [Rc<i32>] = &mut [Rc::new(0), Rc::new(1), Rc::new(2)];
let cs: &Cell<[Rc<i32>]> = Cell::from_mut(s);
let sc: &[Cell<Rc<i32>>] = cs.as_slice_of_cells();
assert_eq!(sc.len(), 3);
assert_eq!(*sc[0].get(), 0);
assert_eq!(*sc[1].get(), 1);
assert_eq!(*sc[2].get(), 2);

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Converts to this type from the input type.
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.