pub struct ContinuityCounter(/* private fields */);Expand description
The 4-bit continuity_counter field (ISO/IEC 13818-1 §2.4.3.3).
Wraps modulo 16 per PID; only packets that carry a payload increment it.
The value is always 0..=15, so an out-of-range counter cannot be written
into a header.
Implementations§
Source§impl ContinuityCounter
impl ContinuityCounter
Sourcepub const fn value(self) -> u8
pub const fn value(self) -> u8
The raw 4-bit value (0..=15).
Examples found in repository?
examples/edit_packet.rs (line 24)
18fn build_pcr_packet(pid: Pid, cc: ContinuityCounter, pcr: Pcr) -> [u8; TS_PACKET_SIZE] {
19 let mut raw = [0xFFu8; TS_PACKET_SIZE];
20 let pid = pid.value();
21 raw[0] = TS_SYNC_BYTE;
22 raw[1] = ((pid >> 8) as u8) & 0x1F;
23 raw[2] = (pid & 0xFF) as u8;
24 raw[3] = ADAPTATION_FLAG | PAYLOAD_FLAG | cc.value();
25 raw[4] = 7; // adaptation_field_length (1 flags + 6 PCR)
26 raw[5] = AF_PCR_FLAG;
27 raw[6..12].copy_from_slice(&pcr.to_field_bytes());
28 raw
29}
30
31fn main() {
32 // ── 1) Build a PCR-bearing packet ───────────────────────────────────────
33 let orig_pcr = Pcr::from_parts(10_000, 0).expect("in range");
34 let mut pkt_raw = build_pcr_packet(
35 Pid::from_const(0x0100),
36 ContinuityCounter::try_new(5).expect("4-bit counter"),
37 orig_pcr,
38 );
39
40 // Verify via parse
41 let pkt = TsPacket::parse(&pkt_raw).unwrap();
42 let af = pkt.adaptation_field().unwrap().unwrap();
43 assert_eq!(af.pcr, Some(orig_pcr));
44 assert_eq!(pkt.header.pid, Pid::from_const(0x0100));
45 assert_eq!(pkt.header.continuity_counter.value(), 5);
46 println!("--- PCR-bearing packet ---");
47 println!(
48 "original PCR : base={} ext={} ({} ticks @ 27 MHz)",
49 orig_pcr.base(),
50 orig_pcr.extension(),
51 orig_pcr.as_27mhz()
52 );
53
54 // ── 2) Set PCR to a shifted value ───────────────────────────────────────
55 let shifted_pcr = Pcr::from_27mhz(orig_pcr.as_27mhz() + 27_000_000).expect("in range");
56 OwnedTsPacket::set_pcr(&mut pkt_raw, shifted_pcr).expect("set_pcr");
57
58 let re_pkt = TsPacket::parse(&pkt_raw).unwrap();
59 let re_af = re_pkt.adaptation_field().unwrap().unwrap();
60 let re_pcr = re_af.pcr.unwrap();
61 assert_eq!(re_pcr, shifted_pcr);
62 println!(
63 "shifted PCR : base={} ext={} ({} ticks @ 27 MHz)",
64 re_pcr.base(),
65 re_pcr.extension(),
66 re_pcr.as_27mhz()
67 );
68 assert_eq!(re_pcr.as_27mhz() - orig_pcr.as_27mhz(), 27_000_000);
69
70 // ── 3) set_continuity_counter on a packet ──────────────────────────────
71 let mut cc_raw: [u8; TS_PACKET_SIZE] = OwnedTsPacket::serialize_with_payload(
72 Pid::from_const(0x0200),
73 true,
74 ContinuityCounter::ZERO,
75 &[],
76 )
77 .unwrap();
78 let orig_cc = TsPacket::parse(&cc_raw).unwrap().header.continuity_counter;
79 OwnedTsPacket::set_continuity_counter(
80 &mut cc_raw,
81 ContinuityCounter::try_new(15).expect("4-bit counter"),
82 );
83 let new_cc = TsPacket::parse(&cc_raw).unwrap().header.continuity_counter;
84 assert_eq!(new_cc.value(), 15);
85 println!("\n--- continuity_counter mutation ---");
86 println!("original CC: {orig_cc} → after set_continuity_counter: {new_cc}");
87
88 // ── 4) Build a null_packet and verify PID ───────────────────────────────
89 let null_raw =
90 OwnedTsPacket::null_packet(ContinuityCounter::try_new(7).expect("4-bit counter"));
91 let null_pkt = TsPacket::parse(&null_raw).unwrap();
92 assert_eq!(null_pkt.header.pid, Pid::from_const(0x1FFF));
93 assert_eq!(null_pkt.header.continuity_counter.value(), 7);
94 println!("\n--- null packet ---");
95 println!(
96 "null packet PID = 0x{:04X} (expect 0x1FFF), CC = {}",
97 null_pkt.header.pid.value(),
98 null_pkt.header.continuity_counter.value()
99 );
100
101 // ── 5) Round-trip a TsHeader ────────────────────────────────────────────
102 let orig_hdr = TsHeader::parse(&[0x47, 0x40, 0x00, 0x10]).unwrap();
103 let mut hdr_buf = [0u8; 4];
104 orig_hdr.serialize_into(&mut hdr_buf).unwrap();
105 let re_hdr = TsHeader::parse(&hdr_buf).unwrap();
106 assert_eq!(re_hdr.pid, orig_hdr.pid);
107 assert_eq!(re_hdr.pusi, orig_hdr.pusi);
108 assert_eq!(re_hdr.continuity_counter, orig_hdr.continuity_counter);
109 println!("\n--- round-trip (TsHeader) ---");
110 println!(
111 "TsHeader parse → serialize_into → parse: pid=0x{:04X}, pusi={}, cc={}",
112 re_hdr.pid.value(),
113 re_hdr.pusi,
114 re_hdr.continuity_counter.value()
115 );
116
117 // ── 6) Round-trip an OwnedTsPacket ──────────────────────────────────────
118 let owned = OwnedTsPacket::parse(
119 OwnedTsPacket::serialize_with_payload(
120 Pid::from_const(0x0100),
121 true,
122 ContinuityCounter::try_new(3).expect("4-bit counter"),
123 &[0xAA, 0xBB],
124 )
125 .unwrap(),
126 )
127 .unwrap();
128 assert_eq!(owned.pid(), Pid::from_const(0x0100));
129 assert_eq!(owned.continuity_counter().value(), 3);
130 assert!(owned.payload().is_some());
131 assert_eq!(owned.payload().unwrap().len(), 184); // full payload area (serialize_with_payload fills the rest with 0xFF)
132 assert_eq!(owned.payload().unwrap()[..2], [0xAA, 0xBB]);
133 println!(
134 "OwnedTsPacket serialize_with_payload → parse → fields match: pid=0x{:04X}, pusi={}, cc={}",
135 owned.pid().value(),
136 owned.pusi(),
137 owned.continuity_counter().value()
138 );
139
140 println!("\nAll write/edit operations completed successfully.");
141}Sourcepub const fn forward_distance_from(self, from: Self) -> u8
pub const fn forward_distance_from(self, from: Self) -> u8
The forward distance from from to self, (self - from) mod 16:
1 for an in-order step, 0 for a duplicate, 2 for one lost
packet, wrapping correctly from 15 to 0.
Sourcepub const fn try_new(value: u8) -> Result<Self, ContinuityCounterOutOfRange>
pub const fn try_new(value: u8) -> Result<Self, ContinuityCounterOutOfRange>
Construct from a raw value, rejecting anything wider than 4 bits.
§Errors
ContinuityCounterOutOfRange when value > 15.
Examples found in repository?
examples/edit_packet.rs (line 36)
31fn main() {
32 // ── 1) Build a PCR-bearing packet ───────────────────────────────────────
33 let orig_pcr = Pcr::from_parts(10_000, 0).expect("in range");
34 let mut pkt_raw = build_pcr_packet(
35 Pid::from_const(0x0100),
36 ContinuityCounter::try_new(5).expect("4-bit counter"),
37 orig_pcr,
38 );
39
40 // Verify via parse
41 let pkt = TsPacket::parse(&pkt_raw).unwrap();
42 let af = pkt.adaptation_field().unwrap().unwrap();
43 assert_eq!(af.pcr, Some(orig_pcr));
44 assert_eq!(pkt.header.pid, Pid::from_const(0x0100));
45 assert_eq!(pkt.header.continuity_counter.value(), 5);
46 println!("--- PCR-bearing packet ---");
47 println!(
48 "original PCR : base={} ext={} ({} ticks @ 27 MHz)",
49 orig_pcr.base(),
50 orig_pcr.extension(),
51 orig_pcr.as_27mhz()
52 );
53
54 // ── 2) Set PCR to a shifted value ───────────────────────────────────────
55 let shifted_pcr = Pcr::from_27mhz(orig_pcr.as_27mhz() + 27_000_000).expect("in range");
56 OwnedTsPacket::set_pcr(&mut pkt_raw, shifted_pcr).expect("set_pcr");
57
58 let re_pkt = TsPacket::parse(&pkt_raw).unwrap();
59 let re_af = re_pkt.adaptation_field().unwrap().unwrap();
60 let re_pcr = re_af.pcr.unwrap();
61 assert_eq!(re_pcr, shifted_pcr);
62 println!(
63 "shifted PCR : base={} ext={} ({} ticks @ 27 MHz)",
64 re_pcr.base(),
65 re_pcr.extension(),
66 re_pcr.as_27mhz()
67 );
68 assert_eq!(re_pcr.as_27mhz() - orig_pcr.as_27mhz(), 27_000_000);
69
70 // ── 3) set_continuity_counter on a packet ──────────────────────────────
71 let mut cc_raw: [u8; TS_PACKET_SIZE] = OwnedTsPacket::serialize_with_payload(
72 Pid::from_const(0x0200),
73 true,
74 ContinuityCounter::ZERO,
75 &[],
76 )
77 .unwrap();
78 let orig_cc = TsPacket::parse(&cc_raw).unwrap().header.continuity_counter;
79 OwnedTsPacket::set_continuity_counter(
80 &mut cc_raw,
81 ContinuityCounter::try_new(15).expect("4-bit counter"),
82 );
83 let new_cc = TsPacket::parse(&cc_raw).unwrap().header.continuity_counter;
84 assert_eq!(new_cc.value(), 15);
85 println!("\n--- continuity_counter mutation ---");
86 println!("original CC: {orig_cc} → after set_continuity_counter: {new_cc}");
87
88 // ── 4) Build a null_packet and verify PID ───────────────────────────────
89 let null_raw =
90 OwnedTsPacket::null_packet(ContinuityCounter::try_new(7).expect("4-bit counter"));
91 let null_pkt = TsPacket::parse(&null_raw).unwrap();
92 assert_eq!(null_pkt.header.pid, Pid::from_const(0x1FFF));
93 assert_eq!(null_pkt.header.continuity_counter.value(), 7);
94 println!("\n--- null packet ---");
95 println!(
96 "null packet PID = 0x{:04X} (expect 0x1FFF), CC = {}",
97 null_pkt.header.pid.value(),
98 null_pkt.header.continuity_counter.value()
99 );
100
101 // ── 5) Round-trip a TsHeader ────────────────────────────────────────────
102 let orig_hdr = TsHeader::parse(&[0x47, 0x40, 0x00, 0x10]).unwrap();
103 let mut hdr_buf = [0u8; 4];
104 orig_hdr.serialize_into(&mut hdr_buf).unwrap();
105 let re_hdr = TsHeader::parse(&hdr_buf).unwrap();
106 assert_eq!(re_hdr.pid, orig_hdr.pid);
107 assert_eq!(re_hdr.pusi, orig_hdr.pusi);
108 assert_eq!(re_hdr.continuity_counter, orig_hdr.continuity_counter);
109 println!("\n--- round-trip (TsHeader) ---");
110 println!(
111 "TsHeader parse → serialize_into → parse: pid=0x{:04X}, pusi={}, cc={}",
112 re_hdr.pid.value(),
113 re_hdr.pusi,
114 re_hdr.continuity_counter.value()
115 );
116
117 // ── 6) Round-trip an OwnedTsPacket ──────────────────────────────────────
118 let owned = OwnedTsPacket::parse(
119 OwnedTsPacket::serialize_with_payload(
120 Pid::from_const(0x0100),
121 true,
122 ContinuityCounter::try_new(3).expect("4-bit counter"),
123 &[0xAA, 0xBB],
124 )
125 .unwrap(),
126 )
127 .unwrap();
128 assert_eq!(owned.pid(), Pid::from_const(0x0100));
129 assert_eq!(owned.continuity_counter().value(), 3);
130 assert!(owned.payload().is_some());
131 assert_eq!(owned.payload().unwrap().len(), 184); // full payload area (serialize_with_payload fills the rest with 0xFF)
132 assert_eq!(owned.payload().unwrap()[..2], [0xAA, 0xBB]);
133 println!(
134 "OwnedTsPacket serialize_with_payload → parse → fields match: pid=0x{:04X}, pusi={}, cc={}",
135 owned.pid().value(),
136 owned.pusi(),
137 owned.continuity_counter().value()
138 );
139
140 println!("\nAll write/edit operations completed successfully.");
141}Sourcepub const fn from_wire_masked(value: u8) -> Self
pub const fn from_wire_masked(value: u8) -> Self
Construct from the low 4 bits of value, deliberately discarding
the rest: the explicit name for extracting the field out of header
byte 3 (ISO/IEC 13818-1 §2.4.3.3, bits [3:0]).
Trait Implementations§
Source§impl Clone for ContinuityCounter
impl Clone for ContinuityCounter
impl Copy for ContinuityCounter
Source§impl Debug for ContinuityCounter
impl Debug for ContinuityCounter
Source§impl Default for ContinuityCounter
impl Default for ContinuityCounter
Source§impl Display for ContinuityCounter
impl Display for ContinuityCounter
impl Eq for ContinuityCounter
Source§impl From<ContinuityCounter> for u8
impl From<ContinuityCounter> for u8
Source§fn from(cc: ContinuityCounter) -> Self
fn from(cc: ContinuityCounter) -> Self
Converts to this type from the input type.
Source§impl Hash for ContinuityCounter
impl Hash for ContinuityCounter
Source§impl Ord for ContinuityCounter
impl Ord for ContinuityCounter
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the minimum of two values. Read more
Source§impl PartialEq for ContinuityCounter
impl PartialEq for ContinuityCounter
Source§impl PartialOrd for ContinuityCounter
impl PartialOrd for ContinuityCounter
Source§impl Serialize for ContinuityCounter
Available on crate feature serde only.
impl Serialize for ContinuityCounter
Available on crate feature
serde only.impl StructuralPartialEq for ContinuityCounter
Auto Trait Implementations§
impl Forget for ContinuityCounter
impl Freeze for ContinuityCounter
impl Move for ContinuityCounter
impl RefUnwindSafe for ContinuityCounter
impl Send for ContinuityCounter
impl Sync for ContinuityCounter
impl Unpin for ContinuityCounter
impl UnsafeUnpin for ContinuityCounter
impl UnwindSafe for ContinuityCounter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more