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

source

pub fn new() -> ReceiveBuffer

Creates a new ReceiveBuffer

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<(), ReceiveBufferError>

Pushes a slice at a certain offset

source

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

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 ReceiveBuffer

source§

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

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

impl Default for ReceiveBuffer

source§

fn default() -> Self

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

impl PartialEq for ReceiveBuffer

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for ReceiveBuffer

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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, U> Into<U> for Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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 Uwhere 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