Skip to main content

s2_lite/
stream_id.rs

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