Trigger

Struct Trigger 

Source
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

Source

pub fn new<T>( func: fn(&Option<Box<dyn Any>>) -> FtuiResult<bool>, arg: T, ) -> Self
where T: 'static,

Constructs a new Trigger with an associated argument.

§Parameters
  • func: A trigger function created using the trg_new_trigger_func! macro.
  • arg: The argument value to associate with the Trigger (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);
Source

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 the trg_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);
Source

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

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 the Trigger (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.
Source

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

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, 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.