pub struct Encoder { /* private fields */ }
Expand description
Data type used to encode data efficient
This structure has been built from the ground up to avoid branching while encoding.
Implementations§
Source§impl Encoder
impl Encoder
Sourcepub fn new<P: PacketVal>(msg: &P) -> Encoder
pub fn new<P: PacketVal>(msg: &P) -> Encoder
Pass an already constructed packet in. This will allocate a buffer the size of that packet
Sourcepub fn from_vec<P: PacketVal>(msg: &P, x: Vec<u8>) -> Encoder
pub fn from_vec<P: PacketVal>(msg: &P, x: Vec<u8>) -> Encoder
To avoid allocations this method allows for a pre-allocated vector be passed in. The Vector’s size will be checked, and it MAY be resized if too small. If it’s capacity is sufficient no allocations will be done.
Sourcepub fn get_vec(self) -> Vec<u8> ⓘ
pub fn get_vec(self) -> Vec<u8> ⓘ
Consumes this type (destroying it) but returns the underlying vector as to not dellocator it’s memory (be used again).
Sourcepub unsafe fn with_capacity(size: usize) -> Self
pub unsafe fn with_capacity(size: usize) -> Self
Used internally for testing, maybe useful to the developer reading this this allows for the input value to set the len/capacity of the internal memory
#Unsafe
This method is unsafe. If you encode a packet LARGER then the method your program may seg fault as there is no bounds checking when encoding.
Sourcepub fn as_slice<'a>(&'a self) -> &'a [u8] ⓘ
pub fn as_slice<'a>(&'a self) -> &'a [u8] ⓘ
While the underlying vec
is fully populated this returns
only the data written to it. So if with::capacity
is used
to create a buffer larger then a packet this can be used
to read only the packet data.
Sourcepub fn encode_u16(&mut self, x: u16)
pub fn encode_u16(&mut self, x: u16)
Encode a u16 used internally.
Sourcepub fn encode_u32(&mut self, x: u32)
pub fn encode_u32(&mut self, x: u32)
Encode a u32 used internally.
Sourcepub fn encode_u64(&mut self, x: u64)
pub fn encode_u64(&mut self, x: u64)
Encode a u64 used internally.
Sourcepub fn encode_slice(&mut self, x: &[u8])
pub fn encode_slice(&mut self, x: &[u8])
Encode a u8 used internally.