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::*, static_id, var::*};
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) -> Var<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) -> Var<bool> {
merge_var!(self.foo(), self.bar(), |f, b| *f && *b)
}
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 CommandMeta<'_>
impl CommandMeta<'_>
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