Struct bitsparrow::Encoder [] [src]

pub struct Encoder {
    // some fields omitted
}

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

Methods

impl Encoder
[src]

fn new() -> Encoder

Create a new instance of the Encoder.

fn with_capacity(capacity: usize) -> Encoder

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

fn uint8(&mut self, uint8: u8) -> &mut Encoder

Store a u8 on the buffer.

fn uint16(&mut self, uint16: u16) -> &mut Encoder

Store a 'u16' on the buffer.

fn uint32(&mut self, uint32: u32) -> &mut Encoder

Store a 'u32' on the buffer.

fn uint64(&mut self, uint64: u64) -> &mut Encoder

Store a 'u64' on the buffer.

fn int8(&mut self, int8: i8) -> &mut Encoder

Store an i8 on the buffer.

fn int16(&mut self, int16: i16) -> &mut Encoder

Store an i16 on the buffer.

fn int32(&mut self, int32: i32) -> &mut Encoder

Store an i32 on the buffer.

fn int64(&mut self, int64: i64) -> &mut Encoder

Store an i32 on the buffer.

fn float32(&mut self, float32: f32) -> &mut Encoder

Store a float32 on the buffer.

fn float64(&mut self, float64: f64) -> &mut Encoder

Store a float64 on the buffer.

fn bool(&mut self, bool: bool) -> &mut Encoder

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]);

fn size(&mut self, size: usize) -> &mut Encoder

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.

fn bytes(&mut self, bytes: &[u8]) -> &mut Encoder

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

fn string(&mut self, string: &str) -> &mut Encoder

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

fn end(&mut self) -> Vec<u8>

Finish encoding, obtain the buffer and reset the encoder.