Key

Struct Key 

Source
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>

Source

pub fn id(&self) -> CallId

Returns the topo::CallId at which the state variable is bound.

Source

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");
Source§

impl<State> Key<State>
where State: PartialEq,

Source

pub fn set(&self, new: State)

Commits a new state value if it is unequal to the current value and the state variable is still live. Has the same properties as update regarding waking the runtime.

See state and cache_state for examples.

Source§

impl<State> Key<State>
where State: Clone + PartialEq,

Source

pub fn mutate(&self, op: impl FnOnce(&mut State))

Mutates a copy of the current state, committing the update if it results in a change. Has the same properties as update See state and cache_state for examples.

Trait Implementations§

Source§

impl<State> Clone for Key<State>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<State> Debug for Key<State>
where State: Debug,

Source§

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

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

impl<State> Deref for Key<State>

Source§

type Target = State

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<State> Display for Key<State>
where State: Display,

Source§

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

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

impl<State> Hash for Key<State>

Source§

fn hash<H: Hasher>(&self, hasher: &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<State> PartialEq for Key<State>

Source§

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

Keys are considered equal if they point to the same state variable. Importantly, they will compare as equal even if they contain different snapshots of the state variable due to having been initialized in different revisions.

1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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>
where State: Sync + Send,

§

impl<State> Sync for Key<State>
where State: Sync + Send,

§

impl<State> Unpin for Key<State>

§

impl<State> !UnwindSafe for Key<State>

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> AsContext for T
where T: Debug + 'static,

Source§

fn offer<R>(self, op: impl FnOnce() -> R) -> R

Call op within the context of a Layer containing self.
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> CallHasher for T
where T: Hash + ?Sized,

Source§

default fn get_hash<H, B>(value: &H, build_hasher: &B) -> u64
where H: Hash + ?Sized, B: BuildHasher,

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<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> Erased for T