Struct clone_cell::cell::Cell
source · #[repr(transparent)]pub struct Cell<T>where
T: ?Sized,{ /* private fields */ }
Expand description
Implementations§
source§impl<T> Cell<T>
impl<T> Cell<T>
sourcepub const fn new(value: T) -> Self
pub const fn new(value: T) -> Self
Creates a new Cell
containing the given value.
Examples
use clone_cell::cell::Cell;
let c = Cell::new(42);
sourcepub fn swap(&self, other: &Self)
pub fn swap(&self, other: &Self)
Swaps the values of two Cell
s. 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());
sourcepub fn replace(&self, value: T) -> T
pub fn replace(&self, value: T) -> T
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);
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Unwraps the value.
Examples
use clone_cell::cell::Cell;
let c = Cell::new(42);
assert_eq!(c.into_inner(), 42);
sourcepub fn get(&self) -> Twhere
T: PureClone,
pub fn get(&self) -> Twhere
T: PureClone,
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);
source§impl<T> Cell<T>where
T: ?Sized,
impl<T> Cell<T>where
T: ?Sized,
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 clone_cell::cell::Cell;
let c = Cell::new(42);
let p = c.as_ptr();
source§impl<T> Cell<[T]>
impl<T> Cell<[T]>
sourcepub fn as_slice_of_cells(&self) -> &[Cell<T>]
pub fn as_slice_of_cells(&self) -> &[Cell<T>]
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);