Skip to main content

RuntimeCell

Struct RuntimeCell 

Source
pub struct RuntimeCell<T: 'static> { /* private fields */ }
Expand description

A value kept in a runtime, reached through a handle that copies like a MutableState.

A state holder that is Copy keeps its shared, interior-mutable parts here: a read finds the value in the runtime’s cells, with no snapshot records to walk and no scope subscribed. Nothing recomposes when the value changes, so what a composition reads goes in a MutableState instead.

The value is owned as a state is: made while a remember builds its value, it belongs to that slot and goes with it; made anywhere else, it lives as long as the runtime.

use std::cell::Cell;

use cranpose_core::{DefaultScheduler, Runtime, RuntimeCell, scheduler_ref};

let runtime = Runtime::new(scheduler_ref(DefaultScheduler));
let count = RuntimeCell::with_runtime(Cell::new(0), runtime.handle());
let copy = count;
copy.with(|count| count.set(count.get() + 1));
assert_eq!(count.with(Cell::get), 1);

Implementations§

Source§

impl<T: 'static> RuntimeCell<T>

Source

pub fn new(value: T) -> Self

Keeps value in the current runtime. Panics without one.

Source

pub fn with_runtime(value: T, runtime: RuntimeHandle) -> Self

Keeps value in runtime.

Source

pub fn is_alive(&self) -> bool

Whether the value still lives: its owner and its runtime keep it.

Source

pub fn try_get(&self) -> Option<Rc<T>>

A handle to the value, or None once it is gone. The handle keeps the value alive.

Source

pub fn get(&self) -> Rc<T>

A handle to the value. Panics once it is gone.

Source

pub fn try_with<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R>

Runs f on the value, or returns None once it is gone.

Source

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

Runs f on the value. Panics once it is gone.

Trait Implementations§

Source§

impl<T: 'static> Clone for RuntimeCell<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: 'static> Copy for RuntimeCell<T>

Source§

impl<T: 'static> Debug for RuntimeCell<T>

Source§

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

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

impl<T: 'static> Eq for RuntimeCell<T>

Source§

impl<T: 'static> Hash for RuntimeCell<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: 'static> PartialEq for RuntimeCell<T>

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<T> Freeze for RuntimeCell<T>
where PhantomData<fn() -> T>: Freeze,

§

impl<T> RefUnwindSafe for RuntimeCell<T>
where PhantomData<fn() -> T>: RefUnwindSafe,

§

impl<T> Send for RuntimeCell<T>
where PhantomData<fn() -> T>: Send,

§

impl<T> Sync for RuntimeCell<T>
where PhantomData<fn() -> T>: Sync,

§

impl<T> Unpin for RuntimeCell<T>
where PhantomData<fn() -> T>: Unpin,

§

impl<T> UnsafeUnpin for RuntimeCell<T>
where PhantomData<fn() -> T>: UnsafeUnpin,

§

impl<T> UnwindSafe for RuntimeCell<T>
where PhantomData<fn() -> 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
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<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.