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]
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 encode<E: BitEncode>(val: E) -> Vec<u8>
fn write<E: BitEncode>(&mut self, val: E) -> &mut Self
Store any type implementing BitEncode
on the buffer.
fn uint8(&mut self, val: u8) -> &mut Self
Store a u8
on the buffer.
fn uint16(&mut self, val: u16) -> &mut Self
Store a 'u16' on the buffer.
fn uint32(&mut self, val: u32) -> &mut Self
Store a 'u32' on the buffer.
fn uint64(&mut self, val: u64) -> &mut Self
Store a 'u64' on the buffer.
fn int8(&mut self, val: i8) -> &mut Self
Store an i8
on the buffer.
fn int16(&mut self, val: i16) -> &mut Self
Store an i16
on the buffer.
fn int32(&mut self, val: i32) -> &mut Self
Store an i32
on the buffer.
fn int64(&mut self, val: i64) -> &mut Self
Store an i32
on the buffer.
fn float32(&mut self, val: f32) -> &mut Self
Store an f32
on the buffer.
fn float64(&mut self, val: f64) -> &mut Self
Store an f64
on the buffer.
fn bool(&mut self, val: bool) -> &mut Self
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, val: usize) -> &mut Self
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, val: &[u8]) -> &mut Self
Store an arbitary collection of bytes represented as &[u8]
,
easy to use by dereferencing Vec<u8>
with &
.
fn string(&mut self, val: &str) -> &mut Self
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.