#[derive(AnyEvent)]
{
// Attributes available to this derive:
#[falco_event_crate]
}
Expand description
§Derive event-related traits for an enum
Use this macro to define an enum like falco_event::events::types::AnyEvent
, that is usable
wherever another event type is. For example,
use std::ffi::CStr;
use anyhow::{anyhow, Context};
use falco_event::events::PayloadFromBytesError;
#[derive(Default, Debug, falco_event::EventPayload)]
#[event_payload(source = Some("syscall"), code = 322, length_type = u32)]
pub struct MyPluginEvent<'a> {
pub plugin_id: u32,
pub data: &'a [u8],
}
#[derive(Default, Debug, falco_event::EventPayload)]
#[event_payload(source = Some("syscall"), code = 322, length_type = u32)]
pub struct MyAsyncEvent<'a> {
pub plugin_id: u32,
pub name: &'a [u8],
pub data: &'a [u8],
}
#[derive(falco_event_derive::AnyEvent)]
pub enum AnyPluginEvent<'a> {
AsyncEvent(MyAsyncEvent<'a>),
PluginEvent(MyPluginEvent<'a>),
}
If the falco_event
crate is available under a different path, provide its name
in the falco_event_crate
attribute:
#[derive(falco_event_alt::AnyEvent)]
#[falco_event_crate(falco_event_alt)]
pub enum AnyPluginEvent<'a> {
AsyncEvent(MyAsyncEvent<'a>),
PluginEvent(MyPluginEvent<'a>),
}
§Requirements
To use this macro on an enum, all its variants need to have exactly one unnamed field, which implements the following traits:
std::fmt::Debug
, for a string representationevents::EventPayload
, which indicates the type id (and source) of the variantevents::FromRawEvent
, which handles deserialization of the variantevents::PayloadToBytes
, which handles serialization of the variant
One way to fullfil these requirements is to use the EventPayload
macro on the variant field’s type.
§Derived traits
This macro implements the following traits on the enum type:
std::fmt::Debug
, by delegating to each variant (without additional wrapping)events::AnyEventPayload
, which describes a whole set of type ids and sources supported by the enum (one for each variant)events::FromRawEvent
, for deserializationevents::PayloadToBytes
, for serialization