Macro zng::event::event_args
source · macro_rules! event_args { ($( $(#[$outer:meta])* $vis:vis struct $Args:ident { $($(#[$arg_outer:meta])* $arg_vis:vis $arg:ident : $arg_ty:ty,)* .. $(#[$delivery_list_outer:meta])* fn delivery_list(&$self:ident, $delivery_list_ident:ident: &mut UpdateDeliveryList) { $($delivery_list:tt)* } $( $(#[$validate_outer:meta])* fn validate(&$self_v:ident) -> Result<(), $ValidationError:path> { $($validate:tt)+ } )? } )+) => { ... }; }
Expand description
Declares new EventArgs
types.
The macro syntax is similar to struct
declaration, but after the args struct members you must add ..
and then
the fn delivery_list(&self, list: &mut UpdateDeliveryList) {}
method that inserts the widget targets.
After the delivery_list
method you can also optionally add a fn validate(&self) -> Result<(), Txt> { }
method
that validates the arguments.
The macro expansion implements the EventArgs
and AnyEventArgs
traits for the new structs, it generates a public timestamp
member and a new
and now
associated functions. The new
function instantiates args with custom timestamp and propagation handle,
the now
function provides the timestamp and propagation handle and is the primary way to instantiate args.
§Examples
event_args! {
/// My event arguments.
pub struct MyEventArgs {
/// My argument.
pub arg: String,
/// My event target.
pub target: WidgetPath,
..
fn delivery_list(&self, list: &mut UpdateDeliveryList) {
list.insert_wgt(&self.target);
}
/// Optional validation, if defined the generated `new` and `now` functions call it and unwrap the result.
///
/// The error type can be any type that implement `Debug`.
fn validate(&self) -> Result<(), Txt> {
if self.arg.contains("error") {
return Err(formatx!("invalid arg `{}`", self.arg));
}
Ok(())
}
}
// multiple structs can be declared in the same call.
// pub struct MyOtherEventArgs { /**/ }
}