Tracks bit-level write operations during serialization to detect overflow and measure payload size. Reuse when implementing custom binary protocols or compact wire formats.
// Source: https://github.com/naia-lib/naia | License: Apache-2.0
// Provided via justclaw.it.com; keep this notice with the code.
use crate::BitWrite;
// BitCounter
pub struct BitCounter {
start_bits: u32,
current_bits: u32,
max_bits: u32,
}
impl BitCounter {
pub fn new(start_bits: u32, current_bits: u32, max_bits: u32) -> Self {
Self {
start_bits,
current_bits,
max_bits,
}
}
pub fn overflowed(&self) -> bool {
self.current_bits > self.max_bits
}
pub fn bits_needed(&self) -> u32 {
self.current_bits - self.start_bits
}
}
impl BitWrite for BitCounter {
fn write_bit(&mut self, _: bool) {
self.current_bits += 1;
}
fn write_byte(&mut self, _: u8) {
self.curren
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key