Trait BufferAccess

Source
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§

Source

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);
Source

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!");
Source

fn fill_buffer(&mut self) -> Result<&[u8]>

Fills the internal buffer from the underlying stream, returning its contents if successful.

§Errors

Returns any IO errors encountered.

§Example
use std::{fs::File, io::BufReader};
use data_streams::BufferAccess;
 
let mut source = BufReader::new(File::open("file.txt")?);
source.fill_buffer()?;
Source

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§

Source

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);
Source

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);
Source

fn bypass_buffer(&mut self) -> &mut impl DataSource
where 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.

Implementations on Foreign Types§

Source§

impl BufferAccess for &[u8]

Source§

fn buffer_capacity(&self) -> usize

Source§

fn buffer(&self) -> &[u8]

Source§

fn fill_buffer(&mut self) -> Result<&[u8]>

Source§

fn drain_buffer(&mut self, count: usize)

Source§

impl BufferAccess for VecDeque<u8>

Source§

fn buffer_capacity(&self) -> usize

Source§

fn buffer(&self) -> &[u8]

Source§

fn fill_buffer(&mut self) -> Result<&[u8]>

Source§

fn clear_buffer(&mut self)

Source§

fn drain_buffer(&mut self, count: usize)

Source§

impl BufferAccess for Vec<u8>

Source§

fn buffer_capacity(&self) -> usize

Source§

fn buffer(&self) -> &[u8]

Source§

fn fill_buffer(&mut self) -> Result<&[u8]>

Source§

fn drain_buffer(&mut self, count: usize)

Source§

impl<R: Read + ?Sized> BufferAccess for BufReader<R>

Source§

fn buffer_capacity(&self) -> usize

Source§

fn buffer(&self) -> &[u8]

Source§

fn fill_buffer(&mut self) -> Result<&[u8]>

Source§

fn drain_buffer(&mut self, count: usize)

Source§

impl<S: BufferAccess + ?Sized> BufferAccess for &mut S

Source§

fn buffer_capacity(&self) -> usize

Source§

fn buffer(&self) -> &[u8]

Source§

fn fill_buffer(&mut self) -> Result<&[u8]>

Source§

fn clear_buffer(&mut self)

Source§

fn drain_buffer(&mut self, count: usize)

Source§

impl<S: BufferAccess + ?Sized> BufferAccess for Box<S>

Source§

fn buffer_capacity(&self) -> usize

Source§

fn buffer(&self) -> &[u8]

Source§

fn fill_buffer(&mut self) -> Result<&[u8]>

Source§

fn clear_buffer(&mut self)

Source§

fn drain_buffer(&mut self, count: usize)

Source§

impl<T: AsRef<[u8]>> BufferAccess for Cursor<T>

Source§

fn buffer_capacity(&self) -> usize

Source§

fn buffer_count(&self) -> usize

Source§

fn buffer(&self) -> &[u8]

Source§

fn fill_buffer(&mut self) -> Result<&[u8]>

Source§

fn drain_buffer(&mut self, count: usize)

Source§

impl<T: BufferAccess + BufRead> BufferAccess for Take<T>

Source§

fn buffer_capacity(&self) -> usize

Source§

fn buffer_count(&self) -> usize

Source§

fn buffer(&self) -> &[u8]

Source§

fn fill_buffer(&mut self) -> Result<&[u8]>

Source§

fn drain_buffer(&mut self, count: usize)

Implementors§