Skip to main content

s2_lite/
stream_id.rs

1use s2_common::{
2    bash::Bash,
3    types::{basin::BasinName, stream::StreamName},
4};
5
6/// Unique identifier for a stream scoped by its basin.
7#[derive(Clone, Copy, PartialEq, Eq, Hash)]
8pub struct StreamId(Bash);
9
10impl std::fmt::Display for StreamId {
11    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12        self.0.fmt(f)
13    }
14}
15
16impl std::fmt::Debug for StreamId {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        write!(f, "StreamId({})", self.0)
19    }
20}
21
22impl StreamId {
23    pub const LEN: usize = 32;
24    const SEPARATOR: u8 = 0;
25
26    pub fn new(basin: &BasinName, stream: &StreamName) -> Self {
27        Self(Bash::delimited(
28            &[basin.as_bytes(), stream.as_bytes()],
29            Self::SEPARATOR,
30        ))
31    }
32
33    pub fn as_bytes(&self) -> &[u8; Self::LEN] {
34        self.0.as_bytes()
35    }
36}
37
38impl From<[u8; StreamId::LEN]> for StreamId {
39    fn from(bytes: [u8; StreamId::LEN]) -> Self {
40        Self(bytes.into())
41    }
42}
43
44impl From<StreamId> for [u8; StreamId::LEN] {
45    fn from(id: StreamId) -> Self {
46        id.0.into()
47    }
48}