pub struct Event { /* private fields */ }Expand description
An event on its way into the store.
The payload is opaque: the contract layer never parses it. That keeps adapters free of domain knowledge and lets replication forward events byte-for-byte without deserialising them. Encoding and decoding are the typed layer’s job.
§Examples
use happenstance_core::{Event, Tags};
let event = Event::new("StudentSubscribed", &b"{\"student\":\"s1\"}"[..])?
.with_tags(Tags::from_pairs([("course", "c1"), ("student", "s1")])?);
assert_eq!(event.event_type().as_str(), "StudentSubscribed");
assert_eq!(event.tags().len(), 2);
assert!(event.metadata().is_none());Implementations§
Source§impl Event
impl Event
Sourcepub fn new<T>(
event_type: T,
data: impl Into<Bytes>,
) -> Result<Self, InvalidEventType>
pub fn new<T>( event_type: T, data: impl Into<Bytes>, ) -> Result<Self, InvalidEventType>
Creates an untagged event.
§Errors
Returns InvalidEventType if event_type fails
EventType::new’s validation.
§Examples
The bound accepts a &str, which is validated here, and an
already-built EventType, which is not validated twice:
use happenstance_core::{Event, EventType};
const COURSE_DEFINED: EventType = EventType::from_static("CourseDefined");
let from_str = Event::new("CourseDefined", &b"{}"[..])?;
let from_const = Event::new(COURSE_DEFINED, &b"{}"[..])?;
assert_eq!(from_str.event_type(), from_const.event_type());Sourcepub fn with_metadata(self, metadata: impl Into<Bytes>) -> Self
pub fn with_metadata(self, metadata: impl Into<Bytes>) -> Self
Attaches opaque client metadata, replacing any already set.
Sourcepub fn event_type(&self) -> &EventType
pub fn event_type(&self) -> &EventType
The event’s type.
Examples found in repository?
103fn decode<'a>(
104 query: &Query,
105 events: impl IntoIterator<Item = &'a ReplicatedEvent>,
106) -> Result<Vec<BoxOffice>, RuleError> {
107 events
108 .into_iter()
109 .map(|replicated| &replicated.event)
110 .filter(|event| query.matches(event.event_type(), event.tags()))
111 .map(|event| {
112 BoxOffice::decode(&Json, event.event_type(), event.data()).map_err(RuleError::Decode)
113 })
114 .collect()
115}Sourcepub fn data(&self) -> &Bytes
pub fn data(&self) -> &Bytes
The opaque payload.
Examples found in repository?
171async fn one_log_two_encodings(events: &SqliteEventStore) -> Result<()> {
172 println!("\n== one log, two encodings ==");
173
174 let log = collect(events.read(&Query::all(), ReadOptions::new())).await?;
175 let mut seen = Vec::new();
176
177 for event in &log {
178 let codec = framed_codec(event).unwrap_or_else(|| "unframed".to_owned());
179 println!(
180 " {:>3} {:<22} {:<8} {} bytes",
181 event.position.get(),
182 event.event_type().as_str(),
183 codec,
184 event.event.data().len()
185 );
186 if !seen.contains(&codec) {
187 seen.push(codec);
188 }
189 }
190
191 if seen.len() < 2 {
192 bail!("the log holds only {seen:?}, so there are no two encodings to read across");
193 }
194 println!(" two encodings and two temperature types, in one store, side by side");
195
196 Ok(())
197}More examples
103fn decode<'a>(
104 query: &Query,
105 events: impl IntoIterator<Item = &'a ReplicatedEvent>,
106) -> Result<Vec<BoxOffice>, RuleError> {
107 events
108 .into_iter()
109 .map(|replicated| &replicated.event)
110 .filter(|event| query.matches(event.event_type(), event.tags()))
111 .map(|event| {
112 BoxOffice::decode(&Json, event.event_type(), event.data()).map_err(RuleError::Decode)
113 })
114 .collect()
115}The event’s tags, in canonical order.
Examples found in repository?
103fn decode<'a>(
104 query: &Query,
105 events: impl IntoIterator<Item = &'a ReplicatedEvent>,
106) -> Result<Vec<BoxOffice>, RuleError> {
107 events
108 .into_iter()
109 .map(|replicated| &replicated.event)
110 .filter(|event| query.matches(event.event_type(), event.tags()))
111 .map(|event| {
112 BoxOffice::decode(&Json, event.event_type(), event.data()).map_err(RuleError::Decode)
113 })
114 .collect()
115}Sourcepub fn into_parts(self) -> EventParts
pub fn into_parts(self) -> EventParts
Decomposes the event into its owned parts.
Not a clone-avoidance route for adapters, which is what this line
used to claim. EventStore::append takes
&[Event], so no store implementation ever owns an Event and none can
reach this method at all; an owning adapter clones instead. That clone
bumps the payload’s refcount rather than copying it, then allocates once
for the EventType, once for the boxed tag slice and once per Tag —
t + 2 heap operations, so 66 at VT-22’s 64-tag floor, measured at
experiments/event-clone-allocations. It exists for code that owns one.
Returns a struct rather than a tuple so that the number of an event’s
parts is not public API. Every let (ty, data, tags, meta) = … would
break the day a fifth part existed; reading fields by name, or
destructuring with .., survives it.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Event
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Event
serde only.