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
impl BytePages
Sourcepub fn new(size: BytePageSize) -> Self
pub fn new(size: BytePageSize) -> Self
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.
Sourcepub fn page_size(&self) -> BytePageSize
pub fn page_size(&self) -> BytePageSize
Returns the capacity category used for new pages.
Sourcepub fn set_page_size(&mut self, size: BytePageSize)
pub fn set_page_size(&mut self, size: BytePageSize)
Sourcepub fn prepend<T>(&mut self, buf: T) -> bool
pub fn prepend<T>(&mut self, buf: T) -> bool
Inserts a non-empty page at the front of the collection.
Returns whether a page was inserted.
Sourcepub fn append<T>(&mut self, buf: T)
pub fn append<T>(&mut self, buf: 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.
Sourcepub fn extend_from_slice(&mut self, extend: &[u8])
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.
Sourcepub fn num_pages(&self) -> usize
pub fn num_pages(&self) -> usize
Returns the number of allocated pages containing buffered data.
Sourcepub fn take(&mut self) -> Option<BytePage>
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.
Sourcepub fn iter(&self) -> impl Iterator<Item = &[u8]>
pub fn iter(&self) -> impl Iterator<Item = &[u8]>
Returns an iterator over the buffered data, one slice per page.
The current writable page is yielded last, if it holds data. The
pages are stored on the heap, so their data stays at the same address
when self is moved, until self is modified or dropped.
Sourcepub fn advance_to(&mut self, cnt: usize)
pub fn advance_to(&mut self, cnt: usize)
Advance the internal cursor.
Afterwards self contains elements [cnt, len). Fully consumed pages
are dropped, the current writable page keeps its spare capacity.
§Panics
Panics if cnt > len.
Sourcepub fn move_to(&mut self, pages: &mut BytePages)
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 as is, without copying or merging. The filled part of
the current page of pages is split off in front of the moved data,
its spare capacity stays available for later writes. The current page
of self is moved as a regular page.
Sourcepub fn split_to(&mut self, at: usize) -> BytePages ⓘ
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.
Sourcepub fn split_into(&mut self, at: usize, to: &mut BytePages)
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.
Sourcepub fn try_get_current_from(&mut self, pages: &mut BytePages)
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.
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
fn remaining_mut(&self) -> usize
Source§unsafe fn advance_mut(&mut self, cnt: usize)
unsafe fn advance_mut(&mut self, cnt: usize)
BufMut Read moreSource§fn chunk_mut(&mut self) -> &mut UninitSlice
fn chunk_mut(&mut self) -> &mut UninitSlice
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 moreSource§fn has_remaining_mut(&self) -> bool
fn has_remaining_mut(&self) -> bool
self for more bytes. Read moreSource§fn put_u16(&mut self, n: u16)
fn put_u16(&mut self, n: u16)
self in big-endian byte order. Read moreSource§fn put_u16_le(&mut self, n: u16)
fn put_u16_le(&mut self, n: u16)
self in little-endian byte order. Read moreSource§fn put_i16(&mut self, n: i16)
fn put_i16(&mut self, n: i16)
self in big-endian byte order. Read moreSource§fn put_i16_le(&mut self, n: i16)
fn put_i16_le(&mut self, n: i16)
self in little-endian byte order. Read moreSource§fn put_u32(&mut self, n: u32)
fn put_u32(&mut self, n: u32)
self in big-endian byte order. Read moreSource§fn put_u32_le(&mut self, n: u32)
fn put_u32_le(&mut self, n: u32)
self in little-endian byte order. Read moreSource§fn put_i32(&mut self, n: i32)
fn put_i32(&mut self, n: i32)
self in big-endian byte order. Read moreSource§fn put_i32_le(&mut self, n: i32)
fn put_i32_le(&mut self, n: i32)
self in little-endian byte order. Read moreSource§fn put_u64(&mut self, n: u64)
fn put_u64(&mut self, n: u64)
self in big-endian byte order. Read moreSource§fn put_u64_le(&mut self, n: u64)
fn put_u64_le(&mut self, n: u64)
self in little-endian byte order. Read moreSource§fn put_i64(&mut self, n: i64)
fn put_i64(&mut self, n: i64)
self in big-endian byte order. Read moreSource§fn put_i64_le(&mut self, n: i64)
fn put_i64_le(&mut self, n: i64)
self in little-endian byte order. Read moreSource§fn put_u128(&mut self, n: u128)
fn put_u128(&mut self, n: u128)
self in big-endian byte order. Read moreSource§fn put_u128_le(&mut self, n: u128)
fn put_u128_le(&mut self, n: u128)
self in little-endian byte order. Read moreSource§fn put_i128(&mut self, n: i128)
fn put_i128(&mut self, n: i128)
self in big-endian byte order. Read moreSource§fn put_i128_le(&mut self, n: i128)
fn put_i128_le(&mut self, n: i128)
self in little-endian byte order. Read moreSource§fn put_uint(&mut self, n: u64, nbytes: usize)
fn put_uint(&mut self, n: u64, nbytes: usize)
self in big-endian byte order. Read moreSource§fn put_uint_le(&mut self, n: u64, nbytes: usize)
fn put_uint_le(&mut self, n: u64, nbytes: usize)
self in the little-endian byte order. Read moreSource§fn put_int(&mut self, n: i64, nbytes: usize)
fn put_int(&mut self, n: i64, nbytes: usize)
self in big-endian byte order. Read moreSource§fn put_int_le(&mut self, n: i64, nbytes: usize)
fn put_int_le(&mut self, n: i64, nbytes: usize)
self in little-endian byte order. Read moreSource§fn put_f32(&mut self, n: f32)
fn put_f32(&mut self, n: f32)
self in big-endian byte order. Read moreSource§fn put_f32_le(&mut self, n: f32)
fn put_f32_le(&mut self, n: f32)
self in little-endian byte order. Read moreSource§fn put_f64(&mut self, n: f64)
fn put_f64(&mut self, n: f64)
self in big-endian byte order. Read moreSource§fn put_f64_le(&mut self, n: f64)
fn put_f64_le(&mut self, n: f64)
self in little-endian byte order. Read moreSource§impl Write for BytePages
impl Write for BytePages
Source§fn write(&mut self, src: &[u8]) -> Result<usize, Error>
fn write(&mut self, src: &[u8]) -> Result<usize, Error>
Source§fn flush(&mut self) -> Result<(), Error>
fn flush(&mut self) -> Result<(), Error>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)