Struct s2n_quic_core::buffer::ReceiveBuffer
source · pub struct ReceiveBuffer { /* private fields */ }Expand description
ReceiveBuffer is a buffer structure for combining chunks of bytes in an
ordered stream, which might arrive out of order.
ReceiveBuffer will accumulate the bytes, and provide them to its users
once a contiguous range of bytes at the current position of the stream has
been accumulated.
ReceiveBuffer is optimized for minimizing memory allocations and for
offering it’s users chunks of sizes that minimize call overhead.
If data is received in smaller chunks, only the first chunk will trigger a memory allocation. All other chunks can be copied into the already allocated region.
When users want to consume data from the buffer, the consumable part of the
internal receive buffer is split off and passed back to the caller. Due to
this chunk being a view onto a reference-counted internal buffer of type
BytesMut this is also efficient and does not require additional memory
allocation or copy.
Usage
use s2n_quic_transport::buffer::ReceiveBuffer;
let mut buffer = ReceiveBuffer::new();
// write a chunk of bytes at offset 4, which can not be consumed yet
assert!(buffer.write_at(4u32.into(), &[4, 5, 6, 7]).is_ok());
assert_eq!(0, buffer.len());
assert_eq!(None, buffer.pop());
// write a chunk of bytes at offset 0, which allows for consumption
assert!(buffer.write_at(0u32.into(), &[0, 1, 2, 3]).is_ok());
assert_eq!(8, buffer.len());
// Pop chunks. Since they all fitted into a single internal buffer,
// they will be returned in combined fashion.
assert_eq!(&[0u8, 1, 2, 3, 4, 5, 6, 7], &buffer.pop().unwrap()[..]);Implementations§
source§impl ReceiveBuffer
impl ReceiveBuffer
sourcepub fn new() -> ReceiveBuffer
pub fn new() -> ReceiveBuffer
Creates a new ReceiveBuffer
sourcepub fn is_writing_complete(&self) -> bool
pub fn is_writing_complete(&self) -> bool
Returns true if the buffer has completely been written to and the final size is known
sourcepub fn is_reading_complete(&self) -> bool
pub fn is_reading_complete(&self) -> bool
Returns true if the buffer has completely been read and the final size is known
sourcepub fn final_size(&self) -> Option<u64>
pub fn final_size(&self) -> Option<u64>
Returns the final size of the stream, if known
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the amount of bytes available for reading. This equals the amount of data that is stored in contiguous fashion at the start of the buffer.
sourcepub fn report(&self) -> (usize, usize)
pub fn report(&self) -> (usize, usize)
Returns the number of bytes and chunks available for consumption
sourcepub fn write_at(
&mut self,
offset: VarInt,
data: &[u8]
) -> Result<(), ReceiveBufferError>
pub fn write_at( &mut self, offset: VarInt, data: &[u8] ) -> Result<(), ReceiveBufferError>
Pushes a slice at a certain offset
sourcepub fn write_at_fin(
&mut self,
offset: VarInt,
data: &[u8]
) -> Result<(), ReceiveBufferError>
pub fn write_at_fin( &mut self, offset: VarInt, data: &[u8] ) -> Result<(), ReceiveBufferError>
Pushes a slice at a certain offset, which is the end of the buffer
sourcepub fn iter(&self) -> impl Iterator<Item = &[u8]>
pub fn iter(&self) -> impl Iterator<Item = &[u8]>
Iterates over all of the chunks waiting to be received
sourcepub fn drain(&mut self) -> impl Iterator<Item = BytesMut> + '_
pub fn drain(&mut self) -> impl Iterator<Item = BytesMut> + '_
Drains all of the currently available chunks
sourcepub fn pop(&mut self) -> Option<BytesMut>
pub fn pop(&mut self) -> Option<BytesMut>
Pops a buffer from the front of the receive queue if available
sourcepub fn pop_watermarked(&mut self, watermark: usize) -> Option<BytesMut>
pub fn pop_watermarked(&mut self, watermark: usize) -> Option<BytesMut>
Pops a buffer from the front of the receive queue, who’s length is always guaranteed to be
less than the provided watermark.
sourcepub fn consumed_len(&self) -> u64
pub fn consumed_len(&self) -> u64
Returns the amount of data that had already been consumed from the receive buffer.
sourcepub fn total_received_len(&self) -> u64
pub fn total_received_len(&self) -> u64
Returns the total amount of contiguous received data.
This includes the already consumed data as well as the data that is still buffered and available for consumption.
Trait Implementations§
source§impl Debug for ReceiveBuffer
impl Debug for ReceiveBuffer
source§impl Default for ReceiveBuffer
impl Default for ReceiveBuffer
source§impl PartialEq for ReceiveBuffer
impl PartialEq for ReceiveBuffer
source§fn eq(&self, other: &ReceiveBuffer) -> bool
fn eq(&self, other: &ReceiveBuffer) -> bool
self and other values to be equal, and is used
by ==.