Skip to main content

BytePages

Struct BytePages 

Source
pub struct BytePages { /* private fields */ }
Expand description

A growable sequence of byte pages.

Data is stored in fixed-capacity pages selected by BytePageSize. This avoids reallocating and copying one large contiguous buffer as data grows.

Implementations§

Source§

impl BytePages

Source

pub fn new(size: BytePageSize) -> BytePages ⓘ

Creates a new BytePages with the specified page size.

Pages are allocated lazily using the specified capacity category.

§Panics

Panics if size is BytePageSize::Unset.

Source

pub fn page_size(&self) -> BytePageSize

Returns the capacity category used for new pages.

Source

pub fn set_page_size(&mut self, size: BytePageSize)

Sets the page size for new pages.

§Panics

Panics if size is BytePageSize::Unset.

Source

pub fn prepend<T>(&mut self, buf: T) -> bool
where BytePage: From<T>,

Inserts a non-empty page at the front of the collection.

Returns whether a page was inserted.

Source

pub fn append<T>(&mut self, buf: T)
where BytePage: From<T>,

Appends a page to the back of the collection.

Empty pages are ignored. If the current page holds no data and buf is a unique buffer with spare capacity, it becomes the new current page. Data of up to 4 KiB is copied into the current page, and into new pages as needed. Larger data is added as a separate page without copying: the filled part of the current page is split off in front of it, and the spare capacity of the current page stays available for later writes.

Source

pub fn extend_from_slice(&mut self, extend: &[u8])

Appends the given bytes to this page object.

Tries to write the data into the current page first. If there is insufficient space, one or more new pages are allocated as needed, and the remaining data is copied into them.

Source

pub fn len(&self) -> usize

Returns the total number of buffered bytes.

Source

pub fn is_empty(&self) -> bool

Returns true if no bytes are buffered.

Source

pub fn num_pages(&self) -> usize

Returns the number of allocated pages containing buffered data.

Source

pub fn take(&mut self) -> Option<BytePage>

Removes and returns the first page from the collection.

The current writable page is returned last. Returns None if there are no pages with data, an empty current page keeps its spare capacity for later writes.

Source

pub fn copy_to(&self, pages: &mut BytePages)

Appends all buffered data to another BytePages value, self is left unchanged.

Pages are shared with pages rather than copied, unless they are small enough to be copied into the current page of pages, see append.

Source

pub fn move_to(&mut self, pages: &mut BytePages)

Moves all buffered data to the back of another BytePages value, leaving self empty.

Pages are moved according to the rules of append.

Source

pub fn split_to(&mut self, at: usize) -> BytePages ⓘ

Splits the buffer into two at the given index.

Afterwards, self contains elements [at, len), and the returned BytePages contains elements [0, at). If at > len, all data is moved.

Depending on the underlying storage, this operation might be O(1) or could involve a memory copy.

Source

pub fn split_into(&mut self, at: usize, to: &mut BytePages)

Splits the buffer, adding the resulting items to the supplied pages object.

Afterwards, self contains elements [at, len), and elements [0, at) are appended to to. If at > len, all data is moved.

Depending on the underlying storage, this operation might be O(1) or could involve a memory copy.

Source

pub fn clear(&mut self)

Clears the buffer, removing all data.

Source

pub fn freeze(&mut self) -> Bytes ⓘ

Drains all pages into one immutable Bytes value.

Source

pub fn try_get_current_from(&mut self, pages: &mut BytePages)

Moves the current writable page from pages if this value is empty.

Trait Implementations§

Source§

impl BufMut for BytePages

Pages are allocated on demand, so remaining_mut() reports usize::MAX - len and chunk_mut() is never empty. The chunk covers the spare capacity of the current page only.

Source§

fn remaining_mut(&self) -> usize

Returns the number of bytes that can be written from the current position until the end of the buffer is reached. Read more
Source§

unsafe fn advance_mut(&mut self, cnt: usize)

Advance the internal cursor of the BufMut Read more
Source§

fn chunk_mut(&mut self) -> &mut UninitSlice

Returns a mutable slice starting at the current BufMut position and of length between 0 and BufMut::remaining_mut(). Note that this can be shorter than the whole remainder of the buffer (this allows non-continuous implementation). Read more
Source§

fn put<T>(&mut self, src: T)
where T: Buf, BytePages: Sized,

Transfer bytes into self from src and advance the cursor by the number of bytes written. Read more
Source§

fn put_slice(&mut self, src: &[u8])

Transfer bytes into self from src and advance the cursor by the number of bytes written. Read more
Source§

fn put_u8(&mut self, n: u8)

Writes an unsigned 8 bit integer to self. Read more
Source§

fn put_i8(&mut self, n: i8)

Writes a signed 8 bit integer to self. Read more
Source§

fn has_remaining_mut(&self) -> bool

Returns true if there is space in self for more bytes. Read more
Source§

fn put_u16(&mut self, n: u16)

Writes an unsigned 16 bit integer to self in big-endian byte order. Read more
Source§

fn put_u16_le(&mut self, n: u16)

Writes an unsigned 16 bit integer to self in little-endian byte order. Read more
Source§

fn put_i16(&mut self, n: i16)

Writes a signed 16 bit integer to self in big-endian byte order. Read more
Source§

fn put_i16_le(&mut self, n: i16)

Writes a signed 16 bit integer to self in little-endian byte order. Read more
Source§

fn put_u32(&mut self, n: u32)

Writes an unsigned 32 bit integer to self in big-endian byte order. Read more
Source§

fn put_u32_le(&mut self, n: u32)

Writes an unsigned 32 bit integer to self in little-endian byte order. Read more
Source§

fn put_i32(&mut self, n: i32)

Writes a signed 32 bit integer to self in big-endian byte order. Read more
Source§

fn put_i32_le(&mut self, n: i32)

Writes a signed 32 bit integer to self in little-endian byte order. Read more
Source§

fn put_u64(&mut self, n: u64)

Writes an unsigned 64 bit integer to self in big-endian byte order. Read more
Source§

fn put_u64_le(&mut self, n: u64)

Writes an unsigned 64 bit integer to self in little-endian byte order. Read more
Source§

fn put_i64(&mut self, n: i64)

Writes a signed 64 bit integer to self in big-endian byte order. Read more
Source§

fn put_i64_le(&mut self, n: i64)

Writes a signed 64 bit integer to self in little-endian byte order. Read more
Source§

fn put_u128(&mut self, n: u128)

Writes an unsigned 128 bit integer to self in big-endian byte order. Read more
Source§

fn put_u128_le(&mut self, n: u128)

Writes an unsigned 128 bit integer to self in little-endian byte order. Read more
Source§

fn put_i128(&mut self, n: i128)

Writes a signed 128 bit integer to self in big-endian byte order. Read more
Source§

fn put_i128_le(&mut self, n: i128)

Writes a signed 128 bit integer to self in little-endian byte order. Read more
Source§

fn put_uint(&mut self, n: u64, nbytes: usize)

Writes an unsigned n-byte integer to self in big-endian byte order. Read more
Source§

fn put_uint_le(&mut self, n: u64, nbytes: usize)

Writes an unsigned n-byte integer to self in the little-endian byte order. Read more
Source§

fn put_int(&mut self, n: i64, nbytes: usize)

Writes a signed n-byte integer to self in big-endian byte order. Read more
Source§

fn put_int_le(&mut self, n: i64, nbytes: usize)

Writes a signed n-byte integer to self in little-endian byte order. Read more
Source§

fn put_f32(&mut self, n: f32)

Writes an IEEE754 single-precision (4 bytes) floating point number to self in big-endian byte order. Read more
Source§

fn put_f32_le(&mut self, n: f32)

Writes an IEEE754 single-precision (4 bytes) floating point number to self in little-endian byte order. Read more
Source§

fn put_f64(&mut self, n: f64)

Writes an IEEE754 double-precision (8 bytes) floating point number to self in big-endian byte order. Read more
Source§

fn put_f64_le(&mut self, n: f64)

Writes an IEEE754 double-precision (8 bytes) floating point number to self in little-endian byte order. Read more
Source§

impl Clone for BytePages

Source§

fn clone(&self) -> BytePages ⓘ

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BytePages

Source§

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

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

impl Default for BytePages

Source§

fn default() -> BytePages ⓘ

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

impl Drop for BytePages

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl From<BytePages> for Bytes

Source§

fn from(pages: BytePages) -> Bytes ⓘ

A single page is converted without copying.

Source§

impl From<BytePages> for BytesMut

Source§

fn from(pages: BytePages) -> BytesMut ⓘ

A single page is converted without copying if nothing else refers to its buffer.

Source§

impl From<BytePages> for Body

Source§

fn from(pages: BytePages) -> Body

Converts to this type from the input type.
Source§

impl MessageBody for BytePages

Source§

fn size(&self) -> BodySize

Returns the body size hint.
Source§

fn poll_next_chunk( &mut self, _: &mut Context<'_>, ) -> Poll<Option<Result<Bytes, Rc<dyn Error>>>>

Polls the next body chunk.
Source§

impl Write for BytePages

Source§

fn write(&mut self, src: &[u8]) -> Result<usize, Error>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<(), Error>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.