naia_serde/
bit_counter.rs1use crate::BitWrite;
2
3pub struct BitCounter {
5 start_bits: u32,
6 current_bits: u32,
7 max_bits: u32,
8}
9
10impl BitCounter {
11 pub fn new(start_bits: u32, current_bits: u32, max_bits: u32) -> Self {
12 Self {
13 start_bits,
14 current_bits,
15 max_bits,
16 }
17 }
18
19 pub fn overflowed(&self) -> bool {
20 self.current_bits > self.max_bits
21 }
22
23 pub fn bits_needed(&self) -> u32 {
24 self.current_bits - self.start_bits
25 }
26}
27
28impl BitWrite for BitCounter {
29 fn write_bit(&mut self, _: bool) {
30 self.current_bits += 1;
31 }
32 fn write_byte(&mut self, _: u8) {
33 self.current_bits += 8;
34 }
35 fn count_bits(&mut self, bits: u32) {
36 self.current_bits += bits;
37 }
38 fn is_counter(&self) -> bool {
39 true
40 }
41}