pub struct Ring { /* private fields */ }
Expand description
Fixed-size reliable read/write buffer with sequential address mapping.
This uses a circular address mapping scheme. That is, for any buffer of
size N
, the pointer address range of 0..N
maps to the same physical
memory as the range N..2*N
. This guarantees that the entire read or
write range may be addressed as a single sequence of bytes.
Unlike the InfiniteRing
, this type otherise
acts as a “normal” buffer. Writes fill up the buffer, and when full, no
furthur writes may be performed until a read occurs. The writable length
sequence is the capacity of the buffer, less any pending readable bytes.
§Examples
use vmap::io::{Ring, SeqWrite};
use std::io::{BufRead, Read, Write};
let mut buf = Ring::new(4000).unwrap();
let mut i = 1;
// Fill up the buffer with lines.
while buf.write_len() > 20 {
write!(&mut buf, "this is test line {}\n", i)?;
i += 1;
}
// No more space is available.
assert!(write!(&mut buf, "this is test line {}\n", i).is_err());
let mut line = String::new();
// Read the first line written.
let len = buf.read_line(&mut line)?;
assert_eq!(line, "this is test line 1\n");
line.clear();
// Read the second line written.
let len = buf.read_line(&mut line)?;
assert_eq!(line, "this is test line 2\n");
// Now there is enough space to write more.
write!(&mut buf, "this is test line {}\n", i)?;
Implementations§
source§impl Ring
impl Ring
sourcepub fn new(hint: usize) -> Result<Self>
pub fn new(hint: usize) -> Result<Self>
Constructs a new buffer instance.
The hint is a minimum size for the buffer. This size will be rounded up to the nearest page size for the actual capacity. The allocation will occupy double the space in the virtual memory table, but the physical memory usage will remain at the desired capacity.
Trait Implementations§
source§impl BufRead for Ring
impl BufRead for Ring
source§fn fill_buf(&mut self) -> Result<&[u8]>
fn fill_buf(&mut self) -> Result<&[u8]>
source§fn consume(&mut self, len: usize)
fn consume(&mut self, len: usize)
amt
bytes have been consumed from the buffer,
so they should no longer be returned in calls to read
. Read moresource§fn has_data_left(&mut self) -> Result<bool, Error>
fn has_data_left(&mut self) -> Result<bool, Error>
buf_read_has_data_left
)Read
has any data left to be read. Read moresource§fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
bufread_skip_until
)byte
or EOF is reached. Read more1.0.0 · source§fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
0xA
byte) is reached, and append
them to the provided String
buffer. Read moresource§impl Read for Ring
impl Read for Ring
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 SeqRead for Ring
impl SeqRead for Ring
source§fn as_read_ptr(&self) -> *const u8
fn as_read_ptr(&self) -> *const u8
source§fn read_offset(&self) -> usize
fn read_offset(&self) -> usize
source§impl SeqWrite for Ring
impl SeqWrite for Ring
source§fn as_write_ptr(&mut self) -> *mut u8
fn as_write_ptr(&mut self) -> *mut u8
source§fn write_offset(&self) -> usize
fn write_offset(&self) -> usize
source§fn write_len(&self) -> usize
fn write_len(&self) -> usize
source§fn write_capacity(&self) -> usize
fn write_capacity(&self) -> usize
source§fn feed(&mut self, len: usize)
fn feed(&mut self, len: usize)
source§impl Write for Ring
impl Write for Ring
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
)