Module swap_buffer_queue::write
source · Available on crate feature
write
only.Expand description
Buffer
implementations to be used with
Write::write
.
WriteArrayBuffer
and WriteVecBuffer
are well suited when there are objects to be
serialized with a known-serialization size. Indeed, objects can then be serialized directly on
the queue’s buffer, avoiding allocation.
Examples
// Creates a WriteVecBuffer queue with a 2-bytes header
let queue: Queue<WriteVecBuffer<2>> = Queue::with_capacity((1 << 16) - 1);
queue
.try_enqueue((256, |slice: &mut [u8]| { /* write the slice */ }))
.unwrap();
queue
.try_enqueue((42, |slice: &mut [u8]| { /* write the slice */ }))
.unwrap();
let mut slice = queue.try_dequeue().unwrap();
// Adds a header with the len of the buffer
let len = (slice.len() as u16).to_be_bytes();
slice.header().copy_from_slice(&len);
// Let's pretend we have a writer
let mut writer: Vec<u8> = Default::default();
assert_eq!(writer.write(slice.frame()).unwrap(), 300);
Structs
- A bytes slice with a
HEADER_SIZE
-bytes header and aTRAILER_SIZE
-bytes trailer. - A
N
-bytes buffer with aHEADER_SIZE
-bytes header and aTRAILER_SIZE
-bytes trailer. - A bytes buffer with a
HEADER_SIZE
-bytes header and aTRAILER_SIZE
-bytes trailer.
Traits
- Bytes slice writer, used by
WriteArrayBuffer
andWriteVecBuffer
.