Struct AsyncEvent

Source
pub struct AsyncEvent<'a, T>
where T: EventSource,
{ pub plugin_id: u32, pub name: &'a CStr, pub data: T, }
Expand description

The event type that can be emitted from async event plugins Asynchronous event

This is the event type generated by async event plugins. This differs from the autogenerated falco_event_schema::types::PPME_ASYNCEVENT_E type in the following ways:

  • none of the fields are Options
  • the name is just a &CStr
  • serde serialization/deserialization is not supported
  • does not depend on the whole falco_event_schema crate

It handles encoding and decoding the payload automatically, so you can use any type that can be converted from/to a byte buffer (including a raw &[u8]) as the payload.

To store an arbitrary type inside the payload, make sure the data implements FromBytes, ToBytes and EventSource, for example:

use std::io::Write;
use falco_event::events::{AnyEventPayload, RawEvent};
use falco_event::fields::{FromBytes, FromBytesError, ToBytes};
use falco_plugin::event::{EventSource, AsyncEvent};
use falco_plugin::event::events::Event;

struct MyEvent {
    param1: u32,
    param2: u32,
}

impl EventSource for MyEvent {
    const SOURCE: Option<&'static str> = Some("my_plugin");
}

impl FromBytes<'_> for MyEvent {
    fn from_bytes(buf: &mut &[u8]) -> Result<Self, FromBytesError> {
        if buf.len() < 2 * size_of::<u32>() {
            return Err(FromBytesError::InvalidLength);
        }

        let param1 = u32::from_le_bytes(buf[..size_of::<u32>()].try_into().unwrap());
        let param2 = u32::from_le_bytes(buf[size_of::<u32>()..].try_into().unwrap());
        *buf = &buf[2 * size_of::<u32>()..];
        Ok(MyEvent { param1, param2 })
    }
}

impl ToBytes for MyEvent {
    fn binary_size(&self) -> usize {
        2 * size_of::<u32>()
    }

   fn write<W: Write>(&self, mut writer: W) -> std::io::Result<()> {
        writer.write_all(&self.param1.to_le_bytes())?;
        writer.write_all(&self.param2.to_le_bytes())?;
        Ok(())
    }

    fn default_repr() -> impl ToBytes {
        MyEvent { param1: 0, param2: 0 }
    }
}

// in a plugin trait implementation:
type Event<'a> = Event<AsyncEvent<'a, MyEvent>>;

Note: this SDK also provides support for JSON-encoded payloads since it’s already necessary to talk to the Falco Plugin API. JSON is not a good choice for high-volume events, as it takes a lot of space and is pretty slow, compared to binary formats. See the source of plugin::event:json for what is needed to support a different serialization format and consider using e.g. bincode instead.

Fields§

§plugin_id: u32

ID of the plugin that generated this event

§name: &'a CStr

Name of the plugin that generated this event

§data: T

Payload, as a custom type

Trait Implementations§

Source§

impl<'a, T> Debug for AsyncEvent<'a, T>
where T: EventSource + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T> Default for AsyncEvent<'a, T>
where T: EventSource + Default,

Source§

fn default() -> AsyncEvent<'a, T>

Returns the “default value” for a type. Read more
Source§

impl<'a, T> EventPayload for AsyncEvent<'a, T>
where T: EventSource,

Source§

const ID: u16 = 402u16

Source§

const SOURCE: Option<&'static str> = <T as EventSource>::SOURCE

Source§

impl<'raw_event, 'a, T> FromRawEvent<'raw_event> for AsyncEvent<'a, T>
where T: EventSource + FromBytes<'raw_event>, 'raw_event: 'a,

Source§

fn parse(raw: &RawEvent<'raw_event>) -> Result<Self, PayloadFromBytesError>

Parse a raw event into the type implementing this trait
Source§

impl<'a, T> PayloadToBytes for AsyncEvent<'a, T>
where T: EventSource + ToBytes,

Source§

fn binary_size(&self) -> usize

Get the binary size of the payload Read more
Source§

fn write<W: Write>(&self, metadata: &EventMetadata, writer: W) -> Result<()>

Write the payload to a writer implementing [std::io::Write].

Auto Trait Implementations§

§

impl<'a, T> Freeze for AsyncEvent<'a, T>
where T: Freeze,

§

impl<'a, T> RefUnwindSafe for AsyncEvent<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for AsyncEvent<'a, T>
where T: Send,

§

impl<'a, T> Sync for AsyncEvent<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for AsyncEvent<'a, T>
where T: Unpin,

§

impl<'a, T> UnwindSafe for AsyncEvent<'a, T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> AnyEventPayload for T
where T: EventPayload,

§

const SOURCES: &'static [Option<&'static str>]

The sources of the events that this payload type can represent.
§

const EVENT_TYPES: &'static [u16]

The event types that this payload type can represent.
§

fn event_sources() -> Vec<&'static str>

Get all the event sources for this payload type Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.