Callback

Struct Callback 

Source
pub struct Callback { /* private fields */ }
Expand description

A generic callback handler for executing functions with stored arguments. Callback allows you to associate a function with an optional argument and invoke it later.

§Usage

Callback is use for creating a Option component. The callback will be trigger when the Option component is selected.

Implementations§

Source§

impl Callback

Source

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

Constructs a new Callback with an associated argument.

§Parameters
  • func: A callback function created using the cbk_new_callback_func! macro.
  • arg: The argument value to associate with the Callback (T: 'static).
§Example
// Define a callback function using the macro.
cbk_new_callback_func!(callback_function, arg, {
    ...
});

// Create a `Callback` with an associated `u32` value.
let _ = Callback::new(callback_function, 5u32);
Source

pub fn no_arg(func: fn(&Option<Box<dyn Any>>) -> FtuiResult<()>) -> Self

Constructs a new Callback without an associated argument.

§Parameters
  • func: A callback function created using the cbk_new_callback_func! macro.
§Example
// Define a callback function using the macro.
cbk_new_callback_func!(callback_function, arg, {
    ...
});

// Create a `Callback` without a associated argument.
let _ = Callback::no_arg(callback_function);
Source

pub fn call(&self) -> FtuiResult<()>

Invoke the Callback. Typically used for testing purposes.

§Returns
  • Ok(()): Returns nothing.
  • Err(FtuiError): Returns an error.
§Example
// Define a callback function that accepts a `u32` and prints it.
cbk_new_callback_func!(print_num, arg, {
    println!("{}", tui::cbk::cast_arg::<u32>(arg)?);
    Ok(())
});
 
// Create a `Callback` with an argument of 5 and invoke it.
Callback::new(print_num, 5u32).call()?; // Prints: 5
Source

pub fn update_arg<T>(&mut self, arg: T)
where T: 'static,

Updates the argument associated with this Callback.

§Parameters
  • arg: The new argument value to associate with the Callback (T' static).
§Example
// Define a callback function that accepts a `u32` and prints it.
cbk_new_callback_func!(print_num, arg, {
    println!("{}", tui::cbk::cast_arg::<u32>(arg)?);
    Ok(())
});
 
// Create a `Callback` with an initial argument.
let mut callback = Callback::new(print_num, 5u32); 

callback.call()?; // Prints: 5

// Update the argument to a new value.
callback.update_arg(6u32);

callback.call()?; // Prints: 6
Source

pub fn remove_arg(&mut self)

Remove the argument associated with the Callback.

Auto Trait Implementations§

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.