Skip to main content

arcly_stream/
identity.rs

1//! Cheaply-cloneable identifiers for applications and streams.
2
3use std::fmt;
4use std::sync::Arc;
5
6/// Shared implementation for cheaply-cloneable `Arc<str>` newtype identifiers.
7macro_rules! arc_str_newtype {
8    ($(#[$meta:meta])* $Name:ident) => {
9        $(#[$meta])*
10        #[derive(Debug, Clone, PartialEq, Eq, Hash)]
11        pub struct $Name(Arc<str>);
12
13        impl serde::Serialize for $Name {
14            fn serialize<S: serde::Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> {
15                s.serialize_str(&self.0)
16            }
17        }
18
19        impl<'de> serde::Deserialize<'de> for $Name {
20            fn deserialize<D: serde::Deserializer<'de>>(d: D) -> std::result::Result<Self, D::Error> {
21                let s = String::deserialize(d)?;
22                Ok(Self(Arc::from(s.as_str())))
23            }
24        }
25
26        impl $Name {
27            /// Create a new identifier from any string-like value.
28            pub fn new(s: impl Into<String>) -> Self {
29                Self(Arc::from(s.into().as_str()))
30            }
31
32            /// Borrow the identifier as a `&str`.
33            pub fn as_str(&self) -> &str {
34                &self.0
35            }
36        }
37
38        impl fmt::Display for $Name {
39            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40                f.write_str(&self.0)
41            }
42        }
43
44        impl From<&str> for $Name {
45            fn from(s: &str) -> Self { Self(Arc::from(s)) }
46        }
47
48        impl From<String> for $Name {
49            fn from(s: String) -> Self { Self(Arc::from(s.as_str())) }
50        }
51    };
52}
53
54arc_str_newtype!(
55    /// Name of an application (e.g. `"live"`, `"vod"`).
56    /// Cheaply cloneable — backed by an `Arc<str>`.
57    AppName
58);
59
60arc_str_newtype!(
61    /// Unique identifier for a stream within an application.
62    /// Cheaply cloneable — backed by an `Arc<str>`.
63    StreamId
64);
65
66/// Composite key uniquely identifying a stream: `(app_name, stream_id)`.
67#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
68pub struct StreamKey {
69    /// Application the stream belongs to.
70    pub app: AppName,
71    /// Stream identifier within the application.
72    pub stream_id: StreamId,
73}
74
75impl StreamKey {
76    /// Build a key from an application name and stream id.
77    pub fn new(app: impl Into<AppName>, stream_id: impl Into<StreamId>) -> Self {
78        Self {
79            app: app.into(),
80            stream_id: stream_id.into(),
81        }
82    }
83}
84
85impl fmt::Display for StreamKey {
86    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87        write!(f, "{}/{}", self.app, self.stream_id)
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    #[test]
96    fn newtypes_are_cheap_clones_with_value_equality() {
97        let a = AppName::from("live");
98        let b = a.clone();
99        assert_eq!(a, b);
100        assert_eq!(a.as_str(), "live");
101        // Clones share the same Arc allocation (cheap, not a deep copy).
102        assert!(std::ptr::eq(a.as_str().as_ptr(), b.as_str().as_ptr()));
103    }
104
105    #[test]
106    fn stream_key_display_is_app_slash_stream() {
107        let key = StreamKey::new("live", "cam-1");
108        assert_eq!(key.to_string(), "live/cam-1");
109    }
110
111    #[test]
112    fn identifiers_roundtrip_through_serde() {
113        let key = StreamKey::new("vod", "movie");
114        let json = serde_json::to_string(&key).expect("serialize");
115        let back: StreamKey = serde_json::from_str(&json).expect("deserialize");
116        assert_eq!(key, back);
117    }
118}