pub struct CircBuf { /* private fields */ }
Expand description
Circular Buffer
A growable circular buffer for use with bytes.
Implementations§
source§impl CircBuf
impl CircBuf
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new CircBuf. The default size of the buffer is DEFAULT_CAPACITY
bytes.
sourcepub fn with_capacity(cap: usize) -> Result<Self, CircBufError>
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.
sourcepub fn cap(&self) -> usize
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.
sourcepub fn find_from_index(&self, val: u8, index: usize) -> Option<usize>
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
.
sourcepub fn find(&self, val: u8) -> Option<usize>
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.
sourcepub fn peek(&self) -> Result<u8, CircBufError>
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.
sourcepub fn get(&mut self) -> Result<u8, CircBufError>
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.
sourcepub fn put(&mut self, val: u8) -> Result<(), CircBufError>
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 ()
.
sourcepub fn advance_read_raw(&mut self, num: usize)
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.
sourcepub fn advance_read(&mut self, num: usize) -> Result<(), CircBufError>
pub fn advance_read(&mut self, num: usize) -> Result<(), CircBufError>
Advance the buffer’s read cursor num
bytes.
sourcepub fn advance_write_raw(&mut self, num: usize)
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.
sourcepub fn advance_write(&mut self, num: usize) -> Result<(), CircBufError>
pub fn advance_write(&mut self, num: usize) -> Result<(), CircBufError>
Advance the buffer’s write cursor num
bytes.
sourcepub fn grow_with_factor(&mut self, factor: usize) -> Result<(), CircBufError>
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.
sourcepub fn grow(&mut self) -> Result<(), CircBufError>
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.
sourcepub fn get_avail_upto_size(&mut self, size: usize) -> [&mut [u8]; 2]
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.
sourcepub fn get_bytes_upto_size(&self, size: usize) -> [&[u8]; 2]
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.
sourcepub fn get_avail(&mut self) -> [&mut [u8]; 2]
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.
sourcepub fn get_bytes(&self) -> [&[u8]; 2]
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.
sourcepub fn reader_peek(&self) -> CircBufPeekReader<'_> ⓘ
pub fn reader_peek(&self) -> CircBufPeekReader<'_> ⓘ
Return a reader that doesn’t advance the read cursor.
Trait Implementations§
source§impl Read for CircBuf
impl Read for CircBuf
source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moresource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
buf
. Read more1.0.0 · source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moresource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere Self: Sized,
Read
. Read moresource§impl Write for CircBuf
impl Write for CircBuf
source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
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
)