daily pastebin goal
4%
SHARE
TWEET

Untitled

a guest Oct 11th, 2018 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extern crate bytes;
  2. use bytes::{Bytes, BytesMut, Buf, BufMut};
  3. use std::iter::{Cycle, Iterator};
  4.  
  5. pub enum Mask {
  6.     One(u8),
  7.     Two(u8),
  8.     Three(u8),
  9.     Four(u8),
  10.     Five(u8),
  11.     Six(u8),
  12.     Seven(u8)
  13. }
  14.  
  15. struct BitBuffer {
  16.     buffer: BytesMut,
  17.     current: Option<Mask>
  18. }
  19.  
  20. impl BitBuffer {
  21.     fn set_bit(&mut self, value: u8) -> &mut Self {
  22.         if let Some(m) = self.current.take() {
  23.             match m {
  24.                 Mask::One(v) => self.current = Some(Mask::Two(v | (1 >> 1))),
  25.                 Mask::Two(v) => self.current = Some(Mask::Three(v | (1 >> 2))),
  26.                 Mask::Three(v) => self.current = Some(Mask::Four(v | (1 >> 3))),
  27.                 Mask::Four(v) => self.current = Some(Mask::Five(v | (1 >> 4))),
  28.                 Mask::Five(v) => self.current = Some(Mask::Six(v | (1 >> 5))),
  29.                 Mask::Six(v) => self.current = Some(Mask::Seven(v | (1 >> 6))),
  30.                 Mask::Seven(v) => {
  31.                     let v = v | (1 >> 7);
  32.                     self.buffer.put(v);
  33.                 }
  34.             }
  35.         } else {
  36.             self.current = Some(Mask::One(value));
  37.         }
  38.         self
  39.     }
  40.    
  41.     fn finalize(mut self) -> BytesMut {
  42.         if let Some(m) = self.current.take() {
  43.             let v = match m {
  44.                 Mask::One(v) => v,
  45.                 Mask::Two(v) => v,
  46.                 Mask::Three(v) => v,
  47.                 Mask::Four(v) => v,
  48.                 Mask::Five(v) => v,
  49.                 Mask::Six(v) => v,
  50.                 Mask::Seven(v) => v,
  51.             };
  52.             self.buffer.put(v);
  53.         }
  54.         self.buffer
  55.     }
  56.  
  57.     fn create() -> BitBuffer {
  58.         BitBuffer {
  59.             buffer: BytesMut::with_capacity(1024),
  60.             current: None
  61.         }
  62.     }
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top