Struct zng_app::event::CommandMeta

source ·
pub struct CommandMeta<'a> { /* private fields */ }
Expand description

Access to metadata of a command.

The metadata storage can be accessed using the Command::with_meta method, implementers must declare and extension trait that adds methods that return CommandMetaVar or ReadOnlyCommandMetaVar that are stored in the CommandMeta. An initialization builder method for each value also must be provided to integrate with the command! macro.

§Examples

The command! initialization transforms foo: true, to command.init_foo(true);, to support that, the command extension trait must have a foo and init_foo methods.

use zng_app::{event::*, var::*, static_id};

static_id! {
    static ref COMMAND_FOO_ID: CommandMetaVarId<bool>;
    static ref COMMAND_BAR_ID: CommandMetaVarId<bool>;
}

/// FooBar command values.
pub trait CommandFooBarExt {
    /// Gets read/write *foo*.
    fn foo(self) -> CommandMetaVar<bool>;

    /// Gets read-only *bar*.
    fn bar(self) -> ReadOnlyCommandMetaVar<bool>;

    /// Gets a read-only var derived from other metadata.
    fn foo_and_bar(self) -> BoxedVar<bool>;

    /// Init *foo*.
    fn init_foo(self, foo: bool) -> Self;

    /// Init *bar*.
    fn init_bar(self, bar: bool) -> Self;
}

impl CommandFooBarExt for Command {
    fn foo(self) -> CommandMetaVar<bool> {
        self.with_meta(|m| m.get_var_or_default(*COMMAND_FOO_ID))
    }

    fn bar(self) -> ReadOnlyCommandMetaVar<bool> {
        self.with_meta(|m| m.get_var_or_insert(*COMMAND_BAR_ID, ||true)).read_only()
    }

    fn foo_and_bar(self) -> BoxedVar<bool> {
        merge_var!(self.foo(), self.bar(), |f, b| *f && *b).boxed()
    }

    fn init_foo(self, foo: bool) -> Self {
        self.with_meta(|m| m.init_var(*COMMAND_FOO_ID, foo));
        self
    }

    fn init_bar(self, bar: bool) -> Self {
        self.with_meta(|m| m.init_var(*COMMAND_BAR_ID, bar));
        self
    }
}

Implementations§

source§

impl<'a> CommandMeta<'a>

source

pub fn get_or_insert<T, F>(&mut self, id: impl Into<StateId<T>>, init: F) -> T
where T: StateValue + Clone, F: FnOnce() -> T,

Clone a meta value identified by a StateId.

If the key is not set in the app, insert it using init to produce a value.

source

pub fn get_or_default<T>(&mut self, id: impl Into<StateId<T>>) -> T
where T: StateValue + Clone + Default,

Clone a meta value identified by a StateId.

If the key is not set, insert the default value and returns a clone of it.

source

pub fn get<T>(&self, id: impl Into<StateId<T>>) -> Option<T>
where T: StateValue + Clone,

Clone a meta value identified by a StateId if it is set.

source

pub fn set<T>(&mut self, id: impl Into<StateId<T>>, value: impl Into<T>)
where T: StateValue + Clone,

Set the meta value associated with the StateId.

source

pub fn init<T>(&mut self, id: impl Into<StateId<T>>, value: impl Into<T>)
where T: StateValue + Clone,

Set the metadata value only if it is not set.

This does not set the scoped override, only the command type metadata.

source

pub fn get_var_or_insert<T, F>( &mut self, id: impl Into<CommandMetaVarId<T>>, init: F, ) -> CommandMetaVar<T>
where T: StateValue + VarValue, F: FnOnce() -> T,

Clone a meta variable identified by a CommandMetaVarId.

The variable is read-write and is clone-on-write if the command is scoped.

source

pub fn get_var<T>( &self, id: impl Into<CommandMetaVarId<T>>, ) -> Option<CommandMetaVar<T>>
where T: StateValue + VarValue,

Clone a meta variable identified by a CommandMetaVarId, if it is set.

source

pub fn get_var_or_default<T>( &mut self, id: impl Into<CommandMetaVarId<T>>, ) -> CommandMetaVar<T>

Clone a meta variable identified by a CommandMetaVarId.

Inserts a variable with the default value if no variable is in the metadata.

source

pub fn init_var<T>( &mut self, id: impl Into<CommandMetaVarId<T>>, value: impl Into<T>, )
where T: StateValue + VarValue,

Set the metadata variable if it was not set.

This does not set the scoped override, only the command type metadata.

Auto Trait Implementations§

§

impl<'a> Freeze for CommandMeta<'a>

§

impl<'a> !RefUnwindSafe for CommandMeta<'a>

§

impl<'a> Send for CommandMeta<'a>

§

impl<'a> Sync for CommandMeta<'a>

§

impl<'a> Unpin for CommandMeta<'a>

§

impl<'a> !UnwindSafe for CommandMeta<'a>

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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
source§

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