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>
impl<T: 'static> RuntimeCell<T>
Sourcepub fn with_runtime(value: T, runtime: RuntimeHandle) -> Self
pub fn with_runtime(value: T, runtime: RuntimeHandle) -> Self
Keeps value in runtime.
Sourcepub fn is_alive(&self) -> bool
pub fn is_alive(&self) -> bool
Whether the value still lives: its owner and its runtime keep it.
Sourcepub fn try_get(&self) -> Option<Rc<T>>
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.
Trait Implementations§
Source§impl<T: 'static> Clone for RuntimeCell<T>
impl<T: 'static> Clone for RuntimeCell<T>
impl<T: 'static> Copy for RuntimeCell<T>
Source§impl<T: 'static> Debug for RuntimeCell<T>
impl<T: 'static> Debug for RuntimeCell<T>
impl<T: 'static> Eq for RuntimeCell<T>
Source§impl<T: 'static> Hash for RuntimeCell<T>
impl<T: 'static> Hash for RuntimeCell<T>
Auto Trait Implementations§
impl<T> Freeze for RuntimeCell<T>
impl<T> RefUnwindSafe for RuntimeCell<T>
impl<T> Send for RuntimeCell<T>
impl<T> Sync for RuntimeCell<T>
impl<T> Unpin for RuntimeCell<T>
impl<T> UnsafeUnpin for RuntimeCell<T>
impl<T> UnwindSafe for RuntimeCell<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more