pub struct Key<State> { /* private fields */ }Expand description
A Key offers access to a state variable. The key allows reads of the state
variable through a snapshot taken when the Key was created. Writes are
supported with Key::update and Key::set.
They are created with the cache_state and state functions.
See state and cache_state for examples.
Implementations§
Source§impl<State> Key<State>
impl<State> Key<State>
Sourcepub fn update(&self, updater: impl FnOnce(&State) -> Option<State>)
pub fn update(&self, updater: impl FnOnce(&State) -> Option<State>)
Runs updater with a reference to the state variable’s latest value,
and enqueues a commit to the variable if updater returns Some.
Returns the Revision at which the state variable was last rooted
if the variable is live, otherwise returns None.
Enqueuing the commit invokes the state change waker registered with the Runtime (if any) to ensure that the code embedding the runtime schedules another call of run_once.
This should be called during event handlers or other code which executes
outside of a Revision’s execution, otherwise unpredictable waker
behavior may be obtained.
§Example
use futures::task::waker;
use moxie::{runtime::RunLoop, state, testing::BoolWaker};
// this runtime holds a single state variable
let mut rt = RunLoop::new(|| state(|| 0u64));
let track_wakes = BoolWaker::new();
rt.set_state_change_waker(waker(track_wakes.clone()));
let (first_commit, first_key) = rt.run_once();
assert_eq!(*first_commit, 0, "no updates yet");
assert!(!track_wakes.is_woken(), "no updates yet");
first_key.update(|_| None); // this is a no-op
assert_eq!(*first_key, 0, "no updates yet");
assert!(!track_wakes.is_woken(), "no updates yet");
first_key.update(|prev| Some(prev + 1));
assert_eq!(*first_key, 0, "update only enqueued, not yet committed");
assert!(track_wakes.is_woken());
let (second_commit, second_key) = rt.run_once(); // this commits the pending update
assert_eq!(*second_key, 1);
assert_eq!(*second_commit, 1);
assert_eq!(*first_commit, 0, "previous value still held by previous pointer");
assert!(!track_wakes.is_woken(), "wakes only come from updating state vars");
assert_eq!(first_key, second_key, "same state variable");Trait Implementations§
Source§impl<State> PartialEq for Key<State>
impl<State> PartialEq for Key<State>
impl<State> Eq for Key<State>
Auto Trait Implementations§
impl<State> Freeze for Key<State>
impl<State> !RefUnwindSafe for Key<State>
impl<State> Send for Key<State>
impl<State> Sync for Key<State>
impl<State> Unpin for Key<State>
impl<State> !UnwindSafe for Key<State>
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
Source§impl<T> CallHasher for T
impl<T> CallHasher for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.