pub trait BufferAccess: DataSource {
// Required methods
fn buffer_capacity(&self) -> usize;
fn buffer(&self) -> &[u8] ⓘ;
fn fill_buffer(&mut self) -> Result<&[u8]>;
fn drain_buffer(&mut self, count: usize);
// Provided methods
fn buffer_count(&self) -> usize { ... }
fn clear_buffer(&mut self) { ... }
fn bypass_buffer(&mut self) -> &mut impl DataSource
where Self: Sized { ... }
}
Expand description
Accesses a source’s internal buffer.
Required Methods§
Sourcefn buffer_capacity(&self) -> usize
fn buffer_capacity(&self) -> usize
Returns the capacity of the internal buffer.
§Example
use data_streams::BufferAccess;
let buf = Vec::<u8>::with_capacity(16);
assert_eq!(buf.buffer_capacity(), 16);
Sourcefn buffer(&self) -> &[u8] ⓘ
fn buffer(&self) -> &[u8] ⓘ
Returns a slice over the filled portion of the internal buffer. This slice may not contain the whole buffer, for example if it can’t be represented as just one slice.
§Example
use data_streams::BufferAccess;
let buf: &[u8] = b"Hello!";
assert_eq!(buf.buffer(), b"Hello!");
Sourcefn fill_buffer(&mut self) -> Result<&[u8]>
fn fill_buffer(&mut self) -> Result<&[u8]>
Sourcefn drain_buffer(&mut self, count: usize)
fn drain_buffer(&mut self, count: usize)
Consumes count
bytes from the internal buffer. The count
must be <=
the length of the slice returned by either buffer
or
fill_buffer
§Panics
This method panics if count
exceeds the buffer length.
§Example
use std::{fs::File, io::BufReader};
use data_streams::BufferAccess;
let mut source = BufReader::new(File::open("file.txt")?);
source.fill_buffer()?;
source.drain_buffer(512);
Provided Methods§
Sourcefn buffer_count(&self) -> usize
fn buffer_count(&self) -> usize
Returns the byte count contained in the internal buffer.
§Example
use data_streams::BufferAccess;
let buf: &[u8] = &[0; 16];
assert_eq!(buf.buffer_count(), 16);
Sourcefn clear_buffer(&mut self)
fn clear_buffer(&mut self)
Clears the internal buffer.
§Example
use std::{fs::File, io::BufReader};
use data_streams::BufferAccess;
let mut source = BufReader::new(File::open("file.txt")?);
source.fill_buffer()?;
source.clear_buffer();
assert_eq!(source.buffer_count(), 0);
Sourcefn bypass_buffer(&mut self) -> &mut impl DataSourcewhere
Self: Sized,
fn bypass_buffer(&mut self) -> &mut impl DataSourcewhere
Self: Sized,
Bypasses the internal buffer by returning the underlying source, or self
if this behavior is not supported. Note that not fully draining the buffer
before bypassing it will cause data loss.