Skip to main content

hls_runtime/server/
ids.rs

1//! Distinct integer types for the origin's resource-naming arguments
2//! (issue #1159).
3//!
4//! `init_name(track, generation)`, `segment_name(track, seq, ext)` and
5//! `part_name(track, seq, idx, ext)` used to take bare, adjacent `u32`s, so a
6//! swapped argument still compiled and produced a plausible-looking but wrong
7//! URL. Each of these is its own newtype, so a swap is a compile error. `SegmentSeq`/`PartIndex` are defined once in `media_plane::ids` (#1188) and re-exported here.
8//!
9//! `Display` and `Debug` render the bare number: they feed the wire names
10//! (`seg-1-5-7.m4s`, `init-1-5-2.mp4`), which must not change.
11
12use core::fmt;
13
14macro_rules! id_newtype {
15    ($(#[$meta:meta])* $name:ident) => {
16        $(#[$meta])*
17        #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
18        pub struct $name(u32);
19
20        impl $name {
21            /// Wrap a raw value.
22            #[must_use]
23            pub const fn new(value: u32) -> Self {
24                Self(value)
25            }
26
27            /// The raw value.
28            #[must_use]
29            pub const fn value(self) -> u32 {
30                self.0
31            }
32        }
33
34        impl fmt::Debug for $name {
35            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36                fmt::Debug::fmt(&self.0, f)
37            }
38        }
39
40        impl fmt::Display for $name {
41            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42                fmt::Display::fmt(&self.0, f)
43            }
44        }
45    };
46}
47
48id_newtype! {
49    /// An init-segment generation: the version counter in
50    /// `init-{track}-{instance}-{generation}.mp4`, starting at `1`.
51    Generation
52}