pub struct Trigger { /* private fields */ }Expand description
A generic trigger handler for evaluating conditions. Trigger allows you
to define a condition as a function, associate it with an optional argument,
and check whether the condition is met.
§Usage
Trigger is use for creating a Selector object.
Implementations§
Source§impl Trigger
impl Trigger
Sourcepub fn new<T>(
func: fn(&Option<Box<dyn Any>>) -> FtuiResult<bool>,
arg: T,
) -> Selfwhere
T: 'static,
pub fn new<T>(
func: fn(&Option<Box<dyn Any>>) -> FtuiResult<bool>,
arg: T,
) -> Selfwhere
T: 'static,
Constructs a new Trigger with an associated argument.
§Parameters
func: A trigger function created using thetrg_new_trigger_func!macro.arg: The argument value to associate with theTrigger(T: 'static).
§Example
// Define a trigger function using the macro.
trg_new_trigger_func!(trigger_function, arg, {
...
});
// Create a `Trigger` with an associated `u32` value.
let _ = Trigger::new(trigger_function, 5u32);Sourcepub fn no_arg(func: fn(&Option<Box<dyn Any>>) -> FtuiResult<bool>) -> Self
pub fn no_arg(func: fn(&Option<Box<dyn Any>>) -> FtuiResult<bool>) -> Self
Constructs a new Trigger without an associated argument.
§Parameters
func: A trigger function created using thetrg_new_trigger_func!macro.
§Example
// Define a trigger function using the macro.
trg_new_trigger_func!(trigger_function, arg, {
...
});
// Create a `Trigger` without an associated argument.
let _ = Trigger::no_arg(trigger_function);Sourcepub fn check(&self) -> FtuiResult<bool>
pub fn check(&self) -> FtuiResult<bool>
Check whether the Trigger evaluate to true or false. Typically used
for testing purposes.
§Returns
Ok(bool): Whether the trigger condition was met.Err(FtuiError): Returns an error.
§Example
// A trigger function that take in a u32 an evaluate whether it is five.
trg_new_trigger_func!(is_five, arg, {
Ok(*trg::cast_arg::<u32>(arg)? == 5)
});
assert_eq!(Trigger::new(is_five, 5u32).check()?, true); // Evaluates to true
assert_eq!(Trigger::new(is_five, 6u32).check()?, false); // Evaluates to falseSourcepub fn update_arg<T>(&mut self, arg: T)where
T: 'static,
pub fn update_arg<T>(&mut self, arg: T)where
T: 'static,
Updates the argument associated with the Trigger.
§Parameters
arg: The new argument value to associate with theTrigger(T' static).
§Example
// Define a trigger function that checks if the argument is 5.
trg_new_trigger_func!(is_five, arg, {
Ok(*trg::cast_arg::<u32>(arg)? == 5)
});
// Create a `Trigger` with an initial argument value of 5.
let mut trigger = Trigger::new(is_five, 5u32);
assert_eq!(trigger.check()?, true); // Evaluates to true.
// Update the argument to a different value.
trigger.update_arg(6u32);
assert_eq!(trigger.check()?, false); // Now evaluates to false.Sourcepub fn remove_arg(&mut self)
pub fn remove_arg(&mut self)
Remove the argument associated with the Trigger.
Auto Trait Implementations§
impl Freeze for Trigger
impl !RefUnwindSafe for Trigger
impl !Send for Trigger
impl !Sync for Trigger
impl Unpin for Trigger
impl !UnwindSafe for Trigger
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
Mutably borrows from an owned value. Read more