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
impl Callback
Sourcepub fn new<T>(func: fn(&Option<Box<dyn Any>>) -> FtuiResult<()>, arg: T) -> Selfwhere
T: 'static,
pub fn new<T>(func: fn(&Option<Box<dyn Any>>) -> FtuiResult<()>, arg: T) -> Selfwhere
T: 'static,
Constructs a new Callback with an associated argument.
§Parameters
func: A callback function created using thecbk_new_callback_func!macro.arg: The argument value to associate with theCallback(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);Sourcepub fn no_arg(func: fn(&Option<Box<dyn Any>>) -> FtuiResult<()>) -> Self
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 thecbk_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);Sourcepub fn call(&self) -> FtuiResult<()>
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: 5Sourcepub 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 this Callback.
§Parameters
arg: The new argument value to associate with theCallback(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: 6Sourcepub fn remove_arg(&mut self)
pub fn remove_arg(&mut self)
Remove the argument associated with the Callback.
Auto Trait Implementations§
impl Freeze for Callback
impl !RefUnwindSafe for Callback
impl !Send for Callback
impl !Sync for Callback
impl Unpin for Callback
impl !UnwindSafe for Callback
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