Struct circbuf::CircBuf

source ·
pub struct CircBuf { /* private fields */ }
Expand description

Circular Buffer

A growable circular buffer for use with bytes.

Implementations§

source§

impl CircBuf

source

pub fn new() -> Self

Create a new CircBuf. The default size of the buffer is DEFAULT_CAPACITY bytes.

source

pub fn with_capacity(cap: usize) -> Result<Self, CircBufError>

Create a new CircBuf with a size of cap bytes. The capacity will be rounded up to the nearest power of two greater than or equal to cap. If the nearest power of two overflows usize, return CircBufError::Overflow, else return the buffer.

source

pub fn cap(&self) -> usize

Get the capacity of the buffer. This value is equal to one less than the length of the underlying buffer because we cannot let the write cursor to ever circle back to being equal with the read cursor.

source

pub fn len(&self) -> usize

Get the number of bytes stored in the buffer.

source

pub fn avail(&self) -> usize

Get the number of bytes available in the buffer.

source

pub fn is_empty(&self) -> bool

Get a bool indicating whether the buffer is empty or not.

source

pub fn is_full(&self) -> bool

Get a bool indicating whether the buffer is full or not.

source

pub fn find_from_index(&self, val: u8, index: usize) -> Option<usize>

Find the first occurence of val in the buffer starting from index. If val exists in the buffer return the index of the first occurence of val else return None.

source

pub fn find(&self, val: u8) -> Option<usize>

Find the first occurence of val in the buffer. If val exists in the buffer return the index of the first occurence of val else return None. A convenience method for find_from_index with 0 as the index.

source

pub fn peek(&self) -> Result<u8, CircBufError>

Get the next byte to be read from the buffer without removing it from it the buffer. Returns the byte if the buffer is not empty, else returns a BufEmpty error.

source

pub fn get(&mut self) -> Result<u8, CircBufError>

Get the next byte to be read from the buffer and remove it from it the buffer. Returns the byte if the buffer is not empty, else returns a BufEmpty error.

source

pub fn put(&mut self, val: u8) -> Result<(), CircBufError>

Put val into the buffer. Returns a BufFull error if the buffer is full, else returns an empty tuple ().

source

pub fn advance_read_raw(&mut self, num: usize)

Advance the buffer’s read cursor num bytes.

Warning

There is no check perform on internal write cursor. This can lead to data lost.

source

pub fn advance_read(&mut self, num: usize) -> Result<(), CircBufError>

Advance the buffer’s read cursor num bytes.

source

pub fn advance_write_raw(&mut self, num: usize)

Advance the buffer’s write cursor num bytes.

Warning

There is no check perform on internal read cursor. This can lead to data lost.

source

pub fn advance_write(&mut self, num: usize) -> Result<(), CircBufError>

Advance the buffer’s write cursor num bytes.

source

pub fn clear(&mut self)

Clear the buffer.

source

pub fn grow_with_factor(&mut self, factor: usize) -> Result<(), CircBufError>

Grow the size of the buffer by factor. The size of the buffer will be rounded up to the nearest power of two that is greater than or equal to the the current size of the buffer multiplied by factor. If the size of the buffer will overflow usize then CircBufError::Overflow will be returned else an empty tuple () will be returned.

source

pub fn grow(&mut self) -> Result<(), CircBufError>

Grow the size of the buffer. The buffer will be expanded by a factor of DEFAULT_SIZE_MULTIPLIER. If the size of the buffer will overflow usize then CircBufError::Overflow will be returned else an empty tuple () will be returned.

source

pub fn get_avail_upto_size(&mut self, size: usize) -> [&mut [u8]; 2]

Return an array that contains two mutable slices which point to the available bytes in the buffer. The combined lengths of the slices will be the minimum of size and self.avail(). If the available bytes in the buffer are contiguous then the second slice will be of size zero. Otherwise, the first slice will point to the bytes available at the end of the buffer and the second slice will point to the bytes available at the start of the buffer. The array can be used for vector IO.

source

pub fn get_bytes_upto_size(&self, size: usize) -> [&[u8]; 2]

Return an array that contains two slices which point to the bytes that have been written to the buffer. The combined lengths of the slices will be the minimum of size and self.len(). If the bytes are contiguous then the second slice will be of size zero. Otherwise, the first slice will point to the bytes at the end of the buffer and the second slice will point to the bytes available at the start of the buffer.

source

pub fn get_avail(&mut self) -> [&mut [u8]; 2]

Return an array that contains two slices which point to the bytes that are available in the buffer. A convenience method for get_avail_upto_size with size equal to self.avail() so all bytes written available in buffer will be returned.

source

pub fn get_bytes(&self) -> [&[u8]; 2]

Return an array that contains two slices which point to the bytes that have been written to the buffer. A convenience method for get_bytes_upto_size with size equal to self.len() so all bytes written to the buffer will be returned.

source

pub fn reader_peek(&self) -> CircBufPeekReader<'_>

Return a reader that doesn’t advance the read cursor.

Trait Implementations§

source§

impl Debug for CircBuf

source§

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

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

impl Default for CircBuf

source§

fn default() -> CircBuf

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

impl Read for CircBuf

source§

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

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · source§

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

Like read, except that it reads into a slice of buffers. Read more
source§

fn is_read_vectored(&self) -> bool

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

fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>

Read all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Read all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · source§

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

Read the exact number of bytes required to fill buf. Read more
source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
1.0.0 · source§

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

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · source§

fn bytes(self) -> Bytes<Self>where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · source§

fn chain<R>(self, next: R) -> Chain<Self, R>where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · source§

fn take(self, limit: u64) -> Take<Self>where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
source§

impl Write for CircBuf

source§

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

Do nothing

source§

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

Write a buffer into this writer, returning how many bytes were written. 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, fmt: 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 Selfwhere Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

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.