Struct bitsparrow::Encoder [] [src]

pub struct Encoder { /* fields omitted */ }

Encoder takes in typed data and produces a binary buffer represented as Vec<u8>.

Methods

impl Encoder
[src]

Create a new instance of the Encoder.

Create a new instance of the Encoder with a preallocated buffer capacity.

Store any type implementing BitEncode on the buffer.

Store a u8 on the buffer.

Store a 'u16' on the buffer.

Store a 'u32' on the buffer.

Store a 'u64' on the buffer.

Store an i8 on the buffer.

Store an i16 on the buffer.

Store an i32 on the buffer.

Store an i32 on the buffer.

Store an f32 on the buffer.

Store an f64 on the buffer.

Store a bool on the buffer. Calling bool multiple times in a row will attempt to store the information on a single byte.

use bitsparrow::Encoder;

let buffer = Encoder::new()
                    .bool(true)
                    .bool(false)
                    .bool(false)
                    .bool(false)
                    .bool(false)
                    .bool(true)
                    .bool(true)
                    .bool(true)
                    .end();

// booleans are stacked as bits on a single byte, right to left.
assert_eq!(buffer, &[0b11100001]);

Store a usize on the buffer. This will use a variable amount of bytes depending on the value of usize, making it a very powerful and flexible type to send around. BitSparrow uses size internally to prefix string and bytes as those can have an arbitrary length, and using a large number type such as u32 could be an overkill if all you want to send is "Foo". Detailed explanation on how BitSparrow stores size can be found on the homepage.

Store an arbitary collection of bytes represented as &[u8], easy to use by dereferencing Vec<u8> with &.

Store an arbitrary UTF-8 Rust string on the buffer.

Finish encoding, obtain the buffer and reset the encoder.