//! Example of a close replacement for deku::input use deku::prelude::*; use std::io::{self, Cursor, Read, Seek, SeekFrom}; /// Every read to this struct will be saved into an internal cache. This is to keep the cache /// around for the crc without reading from the buffer twice struct ReaderCrc { reader: R, pub cache: Vec, } impl ReaderCrc { pub fn new(reader: R) -> Self { Self { reader, cache: vec![], } } } impl Read for ReaderCrc { fn read(&mut self, buf: &mut [u8]) -> io::Result { let n = self.reader.read(buf); self.cache.extend_from_slice(buf); n } } impl Seek for ReaderCrc { fn seek(&mut self, pos: SeekFrom) -> std::io::Result { self.reader.seek(pos) } } #[derive(Debug, DekuRead)] pub struct DekuStruct { pub a: u8, pub b: u8, } fn main() { let data = vec![0x01, 0x02]; let input = Cursor::new(&data); let mut reader = ReaderCrc::new(input); let (_, s) = DekuStruct::from_reader((&mut reader, 0)).unwrap(); assert_eq!(reader.cache, data); assert_eq!(s.a, 1); assert_eq!(s.b, 2); }