pub struct Reassembler { /* private fields */ }
Expand description
Reassembler
is a buffer structure for combining chunks of bytes in an
ordered stream, which might arrive out of order.
Reassembler
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.
Reassembler
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_core::buffer::Reassembler;
let mut buffer = Reassembler::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 Reassembler
impl Reassembler
Sourcepub fn new() -> Reassembler
pub fn new() -> Reassembler
Creates a new Reassembler
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<(), Error>
pub fn write_at(&mut self, offset: VarInt, data: &[u8]) -> Result<(), Error>
Pushes a slice at a certain offset
Sourcepub fn write_at_fin(&mut self, offset: VarInt, data: &[u8]) -> Result<(), Error>
pub fn write_at_fin(&mut self, offset: VarInt, data: &[u8]) -> Result<(), Error>
Pushes a slice at a certain offset, which is the end of the buffer
pub fn write_reader<R>(&mut self, reader: &mut R) -> Result<(), Error<R::Error>>
Sourcepub fn skip(&mut self, len: VarInt) -> Result<(), Error>
pub fn skip(&mut self, len: VarInt) -> Result<(), Error>
Advances the read and write cursors and discards any held data
This can be used for copy-avoidance applications where a packet is received in order and doesn’t need to be stored temporarily for future packets to unblock the stream.
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 Reassembler
impl Debug for Reassembler
Source§impl Default for Reassembler
impl Default for Reassembler
Source§fn default() -> Reassembler
fn default() -> Reassembler
Source§impl PartialEq for Reassembler
impl PartialEq for Reassembler
Source§impl Reader for Reassembler
impl Reader for Reassembler
Source§fn current_offset(&self) -> VarInt
fn current_offset(&self) -> VarInt
Source§fn final_offset(&self) -> Option<VarInt>
fn final_offset(&self) -> Option<VarInt>
Source§fn has_buffered_fin(&self) -> bool
fn has_buffered_fin(&self) -> bool
true
if the reader has the final offset bufferedSource§fn is_consumed(&self) -> bool
fn is_consumed(&self) -> bool
true
if the reader is finished producing dataSource§fn skip_until(&mut self, offset: VarInt) -> Result<(), Self::Error>
fn skip_until(&mut self, offset: VarInt) -> Result<(), Self::Error>
offset
is reached, or the reader storage is exhausted.Source§fn with_max_data(&mut self, max_data: VarInt) -> Limit<'_, Self>
fn with_max_data(&mut self, max_data: VarInt) -> Limit<'_, Self>
Source§fn with_read_limit(&mut self, max_buffered_len: usize) -> Limit<'_, Self>
fn with_read_limit(&mut self, max_buffered_len: usize) -> Limit<'_, Self>
Source§fn with_empty_buffer(&self) -> Empty<'_, Self>
fn with_empty_buffer(&self) -> Empty<'_, Self>
Source§fn with_checks(&mut self) -> Checked<'_, Self>
fn with_checks(&mut self) -> Checked<'_, Self>
Source§impl Skip for Reassembler
impl Skip for Reassembler
Source§impl Storage for Reassembler
impl Storage for Reassembler
type Error = Infallible
Source§fn buffered_len(&self) -> usize
fn buffered_len(&self) -> usize
Source§fn buffer_is_empty(&self) -> bool
fn buffer_is_empty(&self) -> bool
Source§fn read_chunk(&mut self, watermark: usize) -> Result<Chunk<'_>, Self::Error>
fn read_chunk(&mut self, watermark: usize) -> Result<Chunk<'_>, Self::Error>
Source§fn partial_copy_into<Dest>(
&mut self,
dest: &mut Dest,
) -> Result<Chunk<'_>, Self::Error>
fn partial_copy_into<Dest>( &mut self, dest: &mut Dest, ) -> Result<Chunk<'_>, Self::Error>
dest
, with a trailing chunk of bytes. Read moreSource§fn copy_into<Dest>(&mut self, dest: &mut Dest) -> Result<(), Self::Error>
fn copy_into<Dest>(&mut self, dest: &mut Dest) -> Result<(), Self::Error>
dest
. Read moreSource§fn full_copy(&mut self) -> FullCopy<'_, Self>
fn full_copy(&mut self) -> FullCopy<'_, Self>
partial_copy_into
. Read more