1use std::convert::TryInto;
16
17use crate::{async_trait, Error, Result, ResultStream};
18
19#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
21pub struct Timestamp(u64);
22
23impl Timestamp {
24 pub fn serialize(&self) -> Vec<u8> {
25 self.0.to_be_bytes().to_vec()
26 }
27
28 pub fn deserialize(bytes: Vec<u8>) -> Result<Self> {
29 let bytes: [u8; 8] = bytes
30 .try_into()
31 .map_err(|v| Error::Unknown(format!("malformed bytes: {:?}", v)))?;
32 Ok(Self(u64::from_be_bytes(bytes)))
33 }
34}
35
36impl From<u64> for Timestamp {
37 fn from(v: u64) -> Self {
38 Self(v)
39 }
40}
41
42#[derive(Clone, Debug, PartialEq)]
43pub struct Event {
44 pub ts: Timestamp,
45 pub data: Vec<u8>,
46}
47
48#[async_trait]
50pub trait Stream: Clone + Send + Sync + 'static {
51 async fn read_events(&self, ts: Timestamp) -> ResultStream<Vec<Event>>;
53
54 async fn append_event(&self, event: Event) -> Result<()>;
56
57 async fn release_events(&self, ts: Timestamp) -> Result<()>;
59}