1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::Error;
/// Continuity counter.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ContinuityCounter(u8);
impl ContinuityCounter {
/// Maximum counter value.
pub const MAX: u8 = (1 << 4) - 1;
/// Makes a new `ContinuityCounter` instance that has the value `0`.
pub fn new() -> Self {
ContinuityCounter(0)
}
/// Makes a new `ContinuityCounter` instance with the given value.
///
/// # Errors
///
/// If `n` exceeds `ContinuityCounter::MAX`, it will return an `ErrorKind::InvalidInput` error.
pub fn from_u8(n: u8) -> Result<Self, Error> {
assert!(n <= Self::MAX, "Too large counter: {}", n);
Ok(ContinuityCounter(n))
}
/// Returns the value of the counter.
pub fn as_u8(&self) -> u8 {
self.0
}
/// Increments the counter.
///
/// # Examples
///
/// ```
/// use mpeg2ts::ts::ContinuityCounter;
///
/// let mut c = ContinuityCounter::new();
/// assert_eq!(c.as_u8(), 0);
///
/// for _ in 0..5 { c.increment(); }
/// assert_eq!(c.as_u8(), 5);
///
/// for _ in 0..11 { c.increment(); }
/// assert_eq!(c.as_u8(), 0);
/// ```
pub fn increment(&mut self) {
self.0 = (self.0 + 1) & Self::MAX;
}
}
impl Default for ContinuityCounter {
fn default() -> Self {
Self::new()
}
}