Skip to main content

Event

Trait Event 

Source
pub trait Event: WriteXDR {
    type Payload<'a>;

    // Required methods
    fn num_topics() -> u32
       where Self: Sized;
    fn signature() -> Option<&'static Topic>
       where Self: Sized;
    fn decode_payload<'a>(
        bytes: &'a [u8],
    ) -> Result<Self::Payload<'a>, ReadError>;

    // Provided method
    fn topic<'a>(&'a self, index: u32) -> Option<TopicSrc<'a>> { ... }
}
Expand description

Trait implemented by event types that can be emitted by contracts.

Provides the number of topics and their values. Topic 0 is conventionally the event-type discriminant (e.g. a hash of the event name). The XDR-serialized payload is everything after the topics in the on-chain representation.

Required Associated Types§

Source

type Payload<'a>

The decoded payload type. Uses a GAT so it can borrow from the input bytes (e.g. &'a [u8] fields stay borrowed).

Required Methods§

Source

fn num_topics() -> u32
where Self: Sized,

Number of topics this event type carries (0..=3), including the signature topic when present.

This is a static property of the type: every emission carries the same count, and typed-event matching (TypedEvent::try_new / TypedEvent::new) rejects a raw event whose num_topics differs — so a matching topic 0 plus a coincidentally decodable payload cannot pass for an E emitted with a different topic count.

Source

fn signature() -> Option<&'static Topic>
where Self: Sized,

The static topic-0 discriminant for this event type, if it has one.

Topic 0 is conventionally the event-type discriminant (see the trait docs). Return Some(sig) when topic 0 is a fixed per-type constant: TypedEvent::new then rejects a raw event whose topic 0 belongs to a different type before trusting the decoded payload. Return None only when topic 0 is data-dependent (computed from payload fields, or absent), which opts out of that check.

This is deliberately not defaulted: a silent None would let an impl with a fixed discriminant skip the check by omission. #[derive(Event)] implements it as Some(Self::SIGNATURE).

Source

fn decode_payload<'a>(bytes: &'a [u8]) -> Result<Self::Payload<'a>, ReadError>

Decode the payload portion of a serialized event.

Provided Methods§

Source

fn topic<'a>(&'a self, index: u32) -> Option<TopicSrc<'a>>

Retrieve the topic at the given index.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§