pub struct Buffer { /* private fields */ }Expand description
the Buffer contains the underlying memory and data positions
In all cases, 0 ≤ position ≤ end ≤ capacity should be true
Implementations§
Source§impl Buffer
impl Buffer
Sourcepub fn with_capacity(capacity: usize) -> Buffer ⓘ
pub fn with_capacity(capacity: usize) -> Buffer ⓘ
allocates a new buffer of maximum size capacity
Sourcepub fn from_slice(data: &[u8]) -> Buffer ⓘ
pub fn from_slice(data: &[u8]) -> Buffer ⓘ
allocates a new buffer containing the slice data
the buffer starts full, its available data size is exactly data.len()
Sourcepub fn grow(&mut self, new_size: usize) -> bool
pub fn grow(&mut self, new_size: usize) -> bool
increases the size of the buffer
this does nothing if the buffer is already large enough
Sourcepub fn available_data(&self) -> usize
pub fn available_data(&self) -> usize
returns how much data can be read from the buffer
Sourcepub fn available_space(&self) -> usize
pub fn available_space(&self) -> usize
returns how much free space is available to write to
Sourcepub fn consume(&mut self, count: usize) -> usize
pub fn consume(&mut self, count: usize) -> usize
advances the position tracker
if the position gets past the buffer’s half,
this will call shift() to move the remaining data
to the beginning of the buffer
Sourcepub fn consume_noshift(&mut self, count: usize) -> usize
pub fn consume_noshift(&mut self, count: usize) -> usize
advances the position tracker
This method is similar to consume() but will not move data
to the beginning of the buffer
Sourcepub fn fill(&mut self, count: usize) -> usize
pub fn fill(&mut self, count: usize) -> usize
after having written data to the buffer, use this function to indicate how many bytes were written
if there is not enough available space, this function can call
shift() to move the remaining data to the beginning of the
buffer
Sourcepub fn position(&self) -> usize
pub fn position(&self) -> usize
Get the current position
§Examples
use oval::Buffer;
use std::io::{Read,Write};
let mut output = [0;5];
let mut b = Buffer::with_capacity(10);
let res = b.write(&b"abcdefgh"[..]);
b.read(&mut output);
// Position must be 5
assert_eq!(b.position(), 5);
assert_eq!(b.available_data(), 3);Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
moves the position and end trackers to the beginning this function does not modify the data
Trait Implementations§
Source§impl Read for Buffer
impl Read for Buffer
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>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> 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 Buffer
impl Write for Buffer
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
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)