-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitvec.rs
38 lines (34 loc) · 1.23 KB
/
bitvec.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use crate::{Pointer, PointerType, Position, Read, ReadType, StaticSize, Write, WriteType};
use bitvec::prelude::*;
use num::ToPrimitive;
use std::convert::TryInto;
impl<'a> ReadType<'a> for BitVec<u8, Lsb0> {
type ReadType = Pointer<&'a BitSlice<u8, Lsb0>>;
}
impl<'a> Read<'a> for &'a BitSlice<u8, Lsb0> {
type Output = &'a BitSlice<u8, Lsb0>;
fn read(bytes: &'a [u8], position: Position<Self>) -> Self::Output {
let position = position.cast();
let bit_len = PointerType::read(bytes, position);
let bit_len = bit_len.to_usize().unwrap();
let byte_len = (bit_len / 8) + if bit_len % 8 == 0 { 0 } else { 1 };
let position = *position + PointerType::STATIC_SIZE;
let position = position as usize;
let slice = &bytes[position..position + byte_len];
let bit_slice = BitSlice::from_slice(slice);
&bit_slice[..bit_len]
}
}
impl WriteType for BitVec<u8, Lsb0> {
type WriteType = Position<BitVec<u8, Lsb0>>;
}
impl Write for BitVec<u8, Lsb0> {
type Output = BitVec<u8, Lsb0>;
fn write(&self, writer: &mut crate::Writer) -> Position<Self::Output> {
let position = writer.position();
let bit_len: PointerType = self.len().try_into().unwrap();
writer.write(&bit_len);
writer.write_raw::<[u8]>(self.as_raw_slice());
position
}
}