Bit Serializer

Latest version Documentation MIT Apache

Serialize data with control of every bit. Contains auxiliary methods for most basic types and varint encoding for integer types.

Usage

```rust let mut writer = BitWriter::default();

// You can write bools and they use only one bit writer.write_bool(true).unwrap();

// You can write values with how many bits you wish // Write the value 3 with only 2 bits let value: u32 = 3; writer.write_bits(value, 2).unwrap();

// You can also write write basic types writer.writeu8(0).unwrap(); // uses 8 bits writer.writeu16(1).unwrap(); // uses 16 bits writer.writeu32(2).unwrap(); // uses 32 bits writer.writeu64(3).unwrap(); // uses 64 bits writer.writei64(-1).unwrap(); // uses 16 bits writer.writei32(-2).unwrap(); // uses 32 bits writer.write_i64(-3).unwrap(); // uses 64 bits

// But you can also use the varint encoding variation // That tries to use the least number of bits to encode the value writer.writevarintu16(1).unwrap(); // uses 8 bits writer.writevarintu32(2).unwrap(); // uses 8 bits writer.writevarintu64(3).unwrap(); // uses 8 bits writer.writevarinti16(-1).unwrap(); // uses 8 bits writer.writevarinti32(-2).unwrap(); // uses 8 bits writer.writevarinti64(-3).unwrap(); // uses 8 bits

// Bigger values will use more than one byte

// Float types writer.writef32(1.0).unwrap(); // uses 32 bits writer.writef64(2.0).unwrap(); // uses 64 bits

// Since the serializers impl Write/Read, we can use bincode // Or write/read bytes directly to/from them let bytes = vec![7u8; 20]; writer.write_all(&bytes).unwrap();

[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]

struct SimpleStruct { value: u64, string: String, array: [u16; 12], }

let message = SimpleStruct { value: 999999999999, string: "some text to serialize".to_owned(), array: [500; 12], };

// Serialize using bincode passing a writer bincode::serialize_into(&mut writer, &message).unwrap();

// Consume the writer to get the buffer, so we can create the reader let writerbytes = writer.consume().unwrap(); let mut reader = BitReader::new(&writerbytes).unwrap();

// Now to the reading, just replace write for read, and do it in the same order :) assert!(reader.readbool().unwrap()); asserteq!(reader.read_bits(2).unwrap(), 3);

asserteq!(reader.readu8().unwrap(), 0); asserteq!(reader.readu16().unwrap(), 1); asserteq!(reader.readu32().unwrap(), 2); asserteq!(reader.readu64().unwrap(), 3); asserteq!(reader.readi64().unwrap(), -1); asserteq!(reader.readi32().unwrap(), -2); asserteq!(reader.readi64().unwrap(), -3);

asserteq!(reader.readvarintu16().unwrap(), 1); asserteq!(reader.readvarintu32().unwrap(), 2); asserteq!(reader.readvarintu64().unwrap(), 3); asserteq!(reader.readvarinti16().unwrap(), -1); asserteq!(reader.readvarinti32().unwrap(), -2); asserteq!(reader.readvarinti64().unwrap(), -3);

asserteq!(reader.readf32().unwrap(), 1.0); asserteq!(reader.readf64().unwrap(), 2.0);

let mut newbytes = vec![0u8; bytes.len()]; reader.readexact(&mut newbytes).unwrap(); asserteq!(bytes, new_bytes);

let demessage: SimpleStruct = bincode::deserializefrom(&mut reader).unwrap(); asserteq!(message, demessage); ```