macro_rules! cb {
($owner:expr, |$arg_name:ident| async $method:ident ( $($args:tt)* )) => { ... };
($owner:expr, async $method:ident ( $($args:tt)* )) => { ... };
($owner:expr, async $method:ident) => { ... };
($owner:expr, |$arg_name:ident| $method:ident ( $($args:tt)* )) => { ... };
($owner:expr, $method:ident ( $($args:tt)* )) => { ... };
($owner:expr, $method:ident) => { ... };
}Expand description
A convenience macro to create synchronized or asynchronous callbacks
linked to an owner (typically Rc or Arc).
This macro automates the process of cloning the owner into the closure and wrapping the execution in an async block when needed.
§Variations
-
With UI Argument:
cb!(owner, |v| method(v))Use this when you need to access the data passed by the callback (e.g., a button click value). The identifier between| |becomes the name of the argument within the method call. -
With Manual Arguments:
cb!(owner, method("static", 42))Use this when you want to call a method with specific, predefined arguments, ignoring the data provided by the callback itself. -
No Arguments:
cb!(owner, method)A shorthand for calling a method that takes no parameters (other than&self). -
Async Support: Prefix the method name with the
asynckeyword to usenew_async_rc. The macro will automatically wrap the call in anasync moveblock.
§Examples
// 1. Using the callback's value
let c1 = cb!(self, |msg| handle_message(msg));
// 2. Passing static data (ignoring callback value)
let c2 = cb!(self, async save_to_disk("/tmp/log.txt"));
// 3. Simple notification
let c3 = cb!(self, refresh_ui);