Struct Reassembler

Source
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

Source

pub fn new() -> Reassembler

Creates a new Reassembler

Source

pub fn is_writing_complete(&self) -> bool

Returns true if the buffer has completely been written to and the final size is known

Source

pub fn is_reading_complete(&self) -> bool

Returns true if the buffer has completely been read and the final size is known

Source

pub fn final_size(&self) -> Option<u64>

Returns the final size of the stream, if known

Source

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.

Source

pub fn is_empty(&self) -> bool

Returns true if no bytes are available for reading

Source

pub fn report(&self) -> (usize, usize)

Returns the number of bytes and chunks available for consumption

Source

pub fn write_at(&mut self, offset: VarInt, data: &[u8]) -> Result<(), Error>

Pushes a slice at a certain offset

Source

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

Source

pub fn write_reader<R>(&mut self, reader: &mut R) -> Result<(), Error<R::Error>>
where R: Reader + ?Sized,

Source

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.

Source

pub fn iter(&self) -> impl Iterator<Item = &[u8]>

Iterates over all of the chunks waiting to be received

Source

pub fn drain(&mut self) -> impl Iterator<Item = BytesMut> + '_

Drains all of the currently available chunks

Source

pub fn pop(&mut self) -> Option<BytesMut>

Pops a buffer from the front of the receive queue if available

Source

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.

Source

pub fn consumed_len(&self) -> u64

Returns the amount of data that had already been consumed from the receive buffer.

Source

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.

Source

pub fn reset(&mut self)

Resets the receive buffer.

This will drop all previously received data.

Trait Implementations§

Source§

impl Debug for Reassembler

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Reassembler

Source§

fn default() -> Reassembler

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Reassembler

Source§

fn eq(&self, other: &Reassembler) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Reader for Reassembler

Source§

fn current_offset(&self) -> VarInt

Returns the currently read offset for the stream
Source§

fn final_offset(&self) -> Option<VarInt>

Returns the final offset for the stream
Source§

fn has_buffered_fin(&self) -> bool

Returns true if the reader has the final offset buffered
Source§

fn is_consumed(&self) -> bool

Returns true if the reader is finished producing data
Source§

fn skip_until(&mut self, offset: VarInt) -> Result<(), Self::Error>

Skips the data in the reader until offset is reached, or the reader storage is exhausted.
Source§

fn with_max_data(&mut self, max_data: VarInt) -> Limit<'_, Self>

Limits the maximum offset that the caller can read from the reader
Source§

fn with_read_limit(&mut self, max_buffered_len: usize) -> Limit<'_, Self>

Limits the maximum amount of data that the caller can read from the reader
Source§

fn with_empty_buffer(&self) -> Empty<'_, Self>

Return an empty view onto the reader, with no change in current offset
Source§

fn with_checks(&mut self) -> Checked<'_, Self>

Enables checking the reader for correctness invariants Read more
Source§

impl Skip for Reassembler

Source§

fn skip( &mut self, len: VarInt, final_offset: Option<VarInt>, ) -> Result<(), Error>

Source§

impl Storage for Reassembler

Source§

type Error = Infallible

Source§

fn buffered_len(&self) -> usize

Returns the length of the chunk
Source§

fn buffer_is_empty(&self) -> bool

Returns if the chunk is empty
Source§

fn read_chunk(&mut self, watermark: usize) -> Result<Chunk<'_>, Self::Error>

Reads the current contiguous chunk
Source§

fn partial_copy_into<Dest>( &mut self, dest: &mut Dest, ) -> Result<Chunk<'_>, Self::Error>
where Dest: Storage + ?Sized,

Copies the reader into dest, with a trailing chunk of bytes. Read more
Source§

fn copy_into<Dest>(&mut self, dest: &mut Dest) -> Result<(), Self::Error>
where Dest: Storage + ?Sized,

Copies the reader into dest. Read more
Source§

fn full_copy(&mut self) -> FullCopy<'_, Self>

Forces the entire reader to be copied, even when calling partial_copy_into. Read more
Source§

fn track_read(&mut self) -> Tracked<'_, Self>

Tracks the number of bytes read from the storage
Source§

impl Writer for Reassembler

Source§

fn read_from<R>(&mut self, reader: &mut R) -> Result<(), Error<R::Error>>
where R: Reader + ?Sized,

Source§

impl StructuralPartialEq for Reassembler

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Infallible for T
where T: Storage<Error = Infallible> + ?Sized,

Source§

fn infallible_read_chunk(&mut self, watermark: usize) -> Chunk<'_>

Source§

fn infallible_partial_copy_into<Dest>(&mut self, dest: &mut Dest) -> Chunk<'_>
where Dest: Storage + ?Sized,

Source§

fn infallible_copy_into<Dest>(&mut self, dest: &mut Dest)
where Dest: Storage + ?Sized,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U> Upcast<T> for U
where T: UpcastFrom<U>,

Source§

fn upcast(self) -> T

Source§

impl<T, B> UpcastFrom<Counter<T, B>> for T

Source§

fn upcast_from(value: Counter<T, B>) -> T

Source§

impl<T> Duplex for T
where T: Reader + Writer,