Struct circbuf::CircBuf
[−]
[src]
pub struct CircBuf { /* fields omitted */ }
Circular Buffer
A growable circular buffer for use with bytes.
Methods
impl CircBuf
[src]
fn new() -> Self
Create a new CircBuf. The default size of the buffer is DEFAULT_CAPACITY
bytes.
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.
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.
fn len(&self) -> usize
Get the number of bytes stored in the buffer.
fn avail(&self) -> usize
Get the number of bytes available in the buffer.
fn is_empty(&self) -> bool
Get a bool
indicating whether the buffer is empty or not.
fn is_full(&self) -> bool
Get a bool
indicating whether the buffer is full or not.
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
.
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.
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.
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.
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 ()
.
fn advance_read(&mut self, num: usize)
Advance the buffer's read cursor num
bytes.
fn advance_write(&mut self, num: usize)
Advance the buffer's write cursor num
bytes.
fn clear(&mut self)
Clear the buffer.
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.
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.
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.
fn get_bytes_upto_size(&mut 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.
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.
fn get_bytes(&mut 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.
Trait Implementations
impl Debug for CircBuf
[src]
impl Read for CircBuf
[src]
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
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
1.0.0
Read all bytes until EOF in this source, placing them into buf
. Read more
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0
Read all bytes until EOF in this source, placing them into buf
. Read more
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0
Read the exact number of bytes required to fill buf
. Read more
fn by_ref(&mut self) -> &mut Self
1.0.0
Creates a "by reference" adaptor for this instance of Read
. Read more
fn bytes(self) -> Bytes<Self>
1.0.0
Transforms this Read
instance to an Iterator
over its bytes. Read more
fn chars(self) -> Chars<Self>
🔬 This is a nightly-only experimental API. (io
)
the semantics of a partial read/write of where errors happen is currently unclear and may change
Transforms this Read
instance to an Iterator
over char
s. Read more
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
1.0.0
R: Read,
Creates an adaptor which will chain this stream with another. Read more
fn take(self, limit: u64) -> Take<Self>
1.0.0
Creates an adaptor which will read at most limit
bytes from it. Read more
impl Write for CircBuf
[src]
fn write(&mut self, buf: &[u8]) -> Result<usize>
Write a buffer into this object, returning how many bytes were written. Read more
fn flush(&mut self) -> Result<()>
Clear the buffer by setting the read and write pointers to 0.
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0
Attempts to write an entire buffer into this write. Read more
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
1.0.0
Writes a formatted string into this writer, returning any error encountered. Read more
fn by_ref(&mut self) -> &mut Self
1.0.0
Creates a "by reference" adaptor for this instance of Write
. Read more