Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub struct ByteBuilder {
- bytes: Vec<u8>,
- pending: u8,
- bit: u8,
- }
- impl ByteBuilder {
- pub fn new() -> Self {
- Self {
- bytes: vec![],
- pending: 0,
- bit: 0,
- }
- }
- pub fn push(&mut self, bit: impl Into<bool>) {
- self.pending <<= 1;
- self.bit += 1;
- self.pending |= u8::from(bit.into());
- if self.bit == 8 {
- self.bytes.push(std::mem::replace(&mut self.pending, 0));
- self.bit = 0;
- }
- }
- }
- impl From<ByteBuilder> for Vec<u8> {
- fn from(b: ByteBuilder) -> Self {
- b.bytes
- }
- }
Add Comment
Please, Sign In to add comment