2022-07-24 12:50:02 -04:00
|
|
|
///! An encapsulation of `BufReader`'s buffer management logic.
|
|
|
|
|
///
|
|
|
|
|
/// This module factors out the basic functionality of `BufReader` in order to protect two core
|
|
|
|
|
/// invariants:
|
|
|
|
|
/// * `filled` bytes of `buf` are always initialized
|
|
|
|
|
/// * `pos` is always <= `filled`
|
|
|
|
|
/// Since this module encapsulates the buffer management logic, we can ensure that the range
|
|
|
|
|
/// `pos..filled` is always a valid index into the initialized region of the buffer. This means
|
|
|
|
|
/// that user code which wants to do reads from a `BufReader` via `buffer` + `consume` can do so
|
|
|
|
|
/// without encountering any runtime bounds checks.
|
2022-06-30 21:55:19 -04:00
|
|
|
use crate::cmp;
|
|
|
|
|
use crate::io::{self, Read, ReadBuf};
|
|
|
|
|
use crate::mem::MaybeUninit;
|
|
|
|
|
|
|
|
|
|
pub struct Buffer {
|
2022-07-24 12:50:02 -04:00
|
|
|
// The buffer.
|
2022-06-30 21:55:19 -04:00
|
|
|
buf: Box<[MaybeUninit<u8>]>,
|
2022-07-24 12:50:02 -04:00
|
|
|
// The current seek offset into `buf`, must always be <= `filled`.
|
2022-06-30 21:55:19 -04:00
|
|
|
pos: usize,
|
2022-07-24 12:50:02 -04:00
|
|
|
// Each call to `fill_buf` sets `filled` to indicate how many bytes at the start of `buf` are
|
|
|
|
|
// initialized with bytes from a read.
|
|
|
|
|
filled: usize,
|
2022-06-30 21:55:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Buffer {
|
2022-07-20 18:21:15 -04:00
|
|
|
#[inline]
|
2022-06-30 21:55:19 -04:00
|
|
|
pub fn with_capacity(capacity: usize) -> Self {
|
|
|
|
|
let buf = Box::new_uninit_slice(capacity);
|
2022-07-24 12:50:02 -04:00
|
|
|
Self { buf, pos: 0, filled: 0 }
|
2022-06-30 21:55:19 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-20 18:21:15 -04:00
|
|
|
#[inline]
|
2022-06-30 21:55:19 -04:00
|
|
|
pub fn buffer(&self) -> &[u8] {
|
2022-07-24 12:50:02 -04:00
|
|
|
// SAFETY: self.pos and self.cap are valid, and self.cap => self.pos, and
|
2022-06-30 21:55:19 -04:00
|
|
|
// that region is initialized because those are all invariants of this type.
|
2022-07-24 12:50:02 -04:00
|
|
|
unsafe { MaybeUninit::slice_assume_init_ref(self.buf.get_unchecked(self.pos..self.filled)) }
|
2022-06-30 21:55:19 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-20 18:21:15 -04:00
|
|
|
#[inline]
|
2022-06-30 21:55:19 -04:00
|
|
|
pub fn capacity(&self) -> usize {
|
|
|
|
|
self.buf.len()
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-20 18:21:15 -04:00
|
|
|
#[inline]
|
2022-07-24 12:50:02 -04:00
|
|
|
pub fn filled(&self) -> usize {
|
|
|
|
|
self.filled
|
2022-06-30 21:55:19 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-20 18:21:15 -04:00
|
|
|
#[inline]
|
2022-06-30 21:55:19 -04:00
|
|
|
pub fn pos(&self) -> usize {
|
|
|
|
|
self.pos
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-20 18:21:15 -04:00
|
|
|
#[inline]
|
2022-06-30 21:55:19 -04:00
|
|
|
pub fn discard_buffer(&mut self) {
|
|
|
|
|
self.pos = 0;
|
2022-07-24 12:50:02 -04:00
|
|
|
self.filled = 0;
|
2022-06-30 21:55:19 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-20 18:21:15 -04:00
|
|
|
#[inline]
|
2022-06-30 21:55:19 -04:00
|
|
|
pub fn consume(&mut self, amt: usize) {
|
2022-07-24 12:50:02 -04:00
|
|
|
self.pos = cmp::min(self.pos + amt, self.filled);
|
2022-06-30 21:55:19 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-20 18:21:15 -04:00
|
|
|
#[inline]
|
2022-06-30 21:55:19 -04:00
|
|
|
pub fn unconsume(&mut self, amt: usize) {
|
|
|
|
|
self.pos = self.pos.saturating_sub(amt);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-20 18:21:15 -04:00
|
|
|
#[inline]
|
2022-06-30 21:55:19 -04:00
|
|
|
pub fn fill_buf(&mut self, mut reader: impl Read) -> io::Result<&[u8]> {
|
|
|
|
|
// If we've reached the end of our internal buffer then we need to fetch
|
2022-07-24 12:50:02 -04:00
|
|
|
// some more data from the reader.
|
2022-06-30 21:55:19 -04:00
|
|
|
// Branch using `>=` instead of the more correct `==`
|
|
|
|
|
// to tell the compiler that the pos..cap slice is always valid.
|
2022-07-24 12:50:02 -04:00
|
|
|
if self.pos >= self.filled {
|
|
|
|
|
debug_assert!(self.pos == self.filled);
|
2022-06-30 21:55:19 -04:00
|
|
|
|
|
|
|
|
let mut readbuf = ReadBuf::uninit(&mut self.buf);
|
|
|
|
|
|
|
|
|
|
reader.read_buf(&mut readbuf)?;
|
|
|
|
|
|
2022-07-24 12:50:02 -04:00
|
|
|
self.filled = readbuf.filled_len();
|
2022-06-30 21:55:19 -04:00
|
|
|
self.pos = 0;
|
|
|
|
|
}
|
|
|
|
|
Ok(self.buffer())
|
|
|
|
|
}
|
|
|
|
|
}
|