Skip to main content

GrowBuffer

Struct GrowBuffer 

pub struct GrowBuffer {
    buf: Vec<u8>,
    head: usize,
}
Available on crate feature alloc only.
Expand description

A heap-backed ByteBuffer that grows as needed.

Consumed bytes are reclaimed lazily on the next append, so draining many packets out of one read does not repeatedly shift the tail. Appending fails only if the allocation fails. Per-packet bounds come from the reader’s set_max_packet_size.

Fields§

§buf: Vec<u8>§head: usize

Offset of the first unconsumed byte.

Implementations§

§

impl GrowBuffer

pub const fn new() -> Self

Creates an empty buffer.

pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError>

Creates an empty buffer preallocating capacity bytes.

§Errors

Returns TryReserveError if the allocation fails.

pub fn take(&mut self) -> Vec<u8> ⓘ

Takes the live bytes as an owned Vec. The buffer is empty afterwards.

Lets an I/O driver hand a batch of encoded packets to a writer task by value without copying when the consumed prefix is empty.

fn reclaim(&mut self)

Trait Implementations§

§

impl ByteBuffer for GrowBuffer

§

fn filled(&self) -> &[u8] ⓘ

The contiguous run of live bytes (appended but not yet consumed).
§

fn consume(&mut self, n: usize)

Marks the leading n filled bytes as consumed. Read more
§

fn truncate(&mut self, len: usize)

Keeps the leading len filled bytes and drops the rest. Read more
§

fn clear(&mut self)

Drops all buffered bytes. Read more
§

impl Debug for GrowBuffer

§

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

Formats the value using the given formatter. Read more
§

impl Default for GrowBuffer

§

fn default() -> Self

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

impl Sink for GrowBuffer

§

type Error = TryReserveError

Error returned when the sink cannot take more bytes.
§

fn put(&mut self, bytes: &[u8]) -> Result<(), TryReserveError>

Appends bytes. Read more
§

fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError>

Makes room for additional more bytes, so that putting them does not fail or reallocate. Read more

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, 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 = !

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.