Skip to content

Instantly share code, notes, and snippets.

@awxkee
Last active May 26, 2025 19:16
Show Gist options
  • Select an option

  • Save awxkee/e4e97539e1f8f5a9c63c9e1ac95a4f2d to your computer and use it in GitHub Desktop.

Select an option

Save awxkee/e4e97539e1f8f5a9c63c9e1ac95a4f2d to your computer and use it in GitHub Desktop.
trait StreamingIO: Read + Seek {}
struct IODynWrapper {
cursor: Box<dyn StreamingIO>,
buffer: Vec<u8>,
position: usize,
}
impl Read for IODynWrapper {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
// Same as read_exact but with read.
unimplemented!()
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> std::io::Result<()> {
if self.position + buf.len() < self.buffer.len() {
let ref0 = &self.buffer[self.position..(self.position + buf.len())];
for (dst, src) in buf.iter_mut().zip(ref0) {
*dst = *src;
}
self.position += buf.len();
Ok(())
} else if self.position < self.buffer.len() && self.position + buf.len() > self.buffer.len() {
let diff = self.position + buf.len() - self.buffer.len();
self.cursor.read_exact(&mut buf[..diff])?;
let ref0 = &self.buffer[self.position..(self.position + buf.len())];
for (dst, src) in buf.iter_mut().zip(ref0) {
*dst = *src;
}
self.position += diff;
Ok(())
} else {
let rs = self.cursor.read_exact(buf);
if let Ok(_) = rs {
self.buffer.extend_from_slice(buf);
self.position += buf.len();
}
rs
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment