This commit is contained in:
2026-05-02 16:20:20 +02:00
parent 1ae56389fc
commit 5e51b4bfe1
5 changed files with 434 additions and 143 deletions
+12 -12
View File
@@ -3,10 +3,10 @@
use std::io;
use std::io::{BufRead, Read};
pub enum ReadInfo {
NormalChunk(usize),
LastChunk(usize),
EmptyChunk,
pub enum ReadInfoChunk {
Normal(usize),
Last(usize),
Empty,
}
pub struct AheadReader {
@@ -46,7 +46,7 @@ impl AheadReader {
Ok(total)
}
pub fn read_ahead(&mut self, userbuf: &mut [u8]) -> io::Result<ReadInfo> {
pub fn read_ahead(&mut self, userbuf: &mut [u8]) -> io::Result<ReadInfoChunk> {
// 1st read
if self.bufsz == 0 {
eprintln!("[reader] first read");
@@ -62,11 +62,11 @@ impl AheadReader {
self.inner.read_exact(userbuf)
}
fn first_read(&mut self, userbuf: &mut [u8]) -> io::Result<ReadInfo> {
fn first_read(&mut self, userbuf: &mut [u8]) -> io::Result<ReadInfoChunk> {
// 1st read directly to userbuf (we have no cached data yet)
let n = self.read_until_full(userbuf)?;
if n == 0 {
return Ok(ReadInfo::EmptyChunk);
return Ok(ReadInfoChunk::Empty);
}
// 2nd read directly into our internal buf
@@ -75,13 +75,13 @@ impl AheadReader {
self.buf = tmp;
self.bufsz = n2;
if n2 == 0 {
return Ok(ReadInfo::LastChunk(n));
return Ok(ReadInfoChunk::Last(n));
}
Ok(ReadInfo::NormalChunk(n))
Ok(ReadInfoChunk::Normal(n))
}
fn normal_read(&mut self, userbuf: &mut [u8]) -> io::Result<ReadInfo> {
fn normal_read(&mut self, userbuf: &mut [u8]) -> io::Result<ReadInfoChunk> {
// copy internal buf to userbuf
userbuf.copy_from_slice(&self.buf);
let userbuf_sz = self.bufsz;
@@ -92,9 +92,9 @@ impl AheadReader {
self.buf = tmp;
self.bufsz = n2;
if n2 == 0 {
return Ok(ReadInfo::LastChunk(userbuf_sz));
return Ok(ReadInfoChunk::Last(userbuf_sz));
}
Ok(ReadInfo::NormalChunk(userbuf_sz))
Ok(ReadInfoChunk::Normal(userbuf_sz))
}
}