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>
impl<'a> CommandMeta<'a>
sourcepub fn get_or_insert<T, F>(&mut self, id: impl Into<StateId<T>>, init: F) -> T
pub fn get_or_insert<T, F>(&mut self, id: impl Into<StateId<T>>, init: F) -> 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.
sourcepub fn get_or_default<T>(&mut self, id: impl Into<StateId<T>>) -> T
pub fn get_or_default<T>(&mut self, id: impl Into<StateId<T>>) -> T
Clone a meta value identified by a StateId
.
If the key is not set, insert the default value and returns a clone of it.
sourcepub fn get<T>(&self, id: impl Into<StateId<T>>) -> Option<T>where
T: StateValue + Clone,
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.
sourcepub fn set<T>(&mut self, id: impl Into<StateId<T>>, value: impl Into<T>)where
T: StateValue + Clone,
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
.
sourcepub fn init<T>(&mut self, id: impl Into<StateId<T>>, value: impl Into<T>)where
T: StateValue + Clone,
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.
sourcepub fn get_var_or_insert<T, F>(
&mut self,
id: impl Into<CommandMetaVarId<T>>,
init: F,
) -> CommandMetaVar<T>
pub fn get_var_or_insert<T, F>( &mut self, id: impl Into<CommandMetaVarId<T>>, init: F, ) -> CommandMetaVar<T>
Clone a meta variable identified by a CommandMetaVarId
.
The variable is read-write and is clone-on-write if the command is scoped.
sourcepub fn get_var<T>(
&self,
id: impl Into<CommandMetaVarId<T>>,
) -> Option<CommandMetaVar<T>>where
T: StateValue + VarValue,
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.
sourcepub fn get_var_or_default<T>(
&mut self,
id: impl Into<CommandMetaVarId<T>>,
) -> CommandMetaVar<T>
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.
sourcepub fn init_var<T>(
&mut self,
id: impl Into<CommandMetaVarId<T>>,
value: impl Into<T>,
)where
T: StateValue + VarValue,
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> 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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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