Derive Macro Event

Source
#[derive(Event)]
{
    // Attributes available to this derive:
    #[auto_naming]
    #[mod_name]
}
Available on crate feature event only.
Expand description

Conditionally adds Listen or Emit to a struct.

The field values inside the struct require to be self owned. That means references aren’t allowed inside the event struct.

Depending on the targeted architecture the macro generates different results. When compiling to wasm the Listen trait is derived. Otherwise, Emit is derived.

Both traits generate a new mod in which the related field-structs are generated in. The mod can be automatically renamed with #[auto_naming(EnumLike)] to behave enum-like (for example a struct Tests mod would usually be named test, ‘EnumLike’ names it TestField instead) and #[mod_name(...)] is a direct possibility to rename the mod to any given name.

The generated field-structs represent a field of the struct and are used for the derived trait functions. The fields are used to emit, update or listen_to a given field. For detail usages see the individual traits defined in tauri-interop.

§Example

use tauri_interop_macro::Event;
use serde::{Serialize, Deserialize};

#[derive(Default, Clone, Serialize, Deserialize)]
pub struct Bar {
    value: bool
}

#[derive(Event)]
struct EventModel {
    foo: String,
    pub bar: Bar
}

#[cfg(feature = "initial_value")]
impl tauri_interop::event::ManagedEmit for EventModel {}

// has to be defined in this example, otherwise the
// macro expansion panics because of missing super
fn main() {}