broadcast-core 0.1.0

Frozen wire-format contract for the rust-broadcast crates: symmetric Parse/Serialize traits, checked field-width helpers, MSB-first bit I/O, reserved-bit fields, media timescales, the shared error kinds and the CENC scheme identity. no_std + alloc.
Documentation
//! Identifiers that cross crate boundaries.
//!
//! [`TrackId`] is the one track identity of the workspace: the
//! ISO/IEC 14496-12:2026 `track_ID` a file carries (`tkhd`, `tfhd`, `trex`),
//! or the id a non-ISOBMFF demuxer assigned to a track. The box layer, the
//! muxing hub and a [`Damage`](crate::damage::Damage) report name the same
//! type, so a track id crosses from one to the other without conversion.

use core::fmt;

/// A track identifier (1-based in a file, unique within a movie).
///
/// A thin newtype over the integer the wire carries, so a track id cannot be
/// passed where a sequence number or timescale is expected: build one with
/// [`new`](Self::new) and read it with [`value`](Self::value). `Debug` and
/// `Display` render the bare number, and serde serialises the bare number.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct TrackId(u32);

impl TrackId {
    /// Wrap a raw track id.
    #[must_use]
    pub const fn new(value: u32) -> Self {
        Self(value)
    }

    /// The raw track id.
    #[must_use]
    pub const fn value(self) -> u32 {
        self.0
    }

    /// The id following this one, or `None` after the largest id (there is
    /// no id after it, and wrapping would name the invalid id 0).
    #[must_use]
    pub const fn checked_next(self) -> Option<Self> {
        match self.0.checked_add(1) {
            Some(next) => Some(Self(next)),
            None => None,
        }
    }
}

impl fmt::Debug for TrackId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.0, f)
    }
}

impl fmt::Display for TrackId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.0, f)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::format;

    #[test]
    fn renders_as_the_bare_number() {
        assert_eq!(format!("{} {:?}", TrackId::new(7), TrackId::new(7)), "7 7");
        assert_eq!(format!("{:?}", Some(TrackId::new(3))), "Some(3)");
        assert_eq!(format!("{:04}", TrackId::new(5)), "0005");
        assert_eq!(TrackId::new(9).value(), 9);
    }

    #[test]
    fn checked_next_stops_at_the_largest_id() {
        assert_eq!(TrackId::new(9).checked_next(), Some(TrackId::new(10)));
        assert_eq!(TrackId::new(u32::MAX).checked_next(), None);
    }
}