alice_protocol_reader/
stdin_reader.rs1use super::bufreader_wrapper::BufferedReaderWrapper;
7use std::io::{self, Read, SeekFrom};
8
9#[derive(Debug)]
11pub struct StdInReaderSeeker<R> {
12 pub reader: R,
14}
15
16impl BufferedReaderWrapper for StdInReaderSeeker<io::Stdin> {
18 fn seek_relative_offset(&mut self, offset: i64) -> io::Result<()> {
19 let mut buf = vec![0; offset as usize];
21 match io::stdin().lock().read_exact(&mut buf) {
22 Ok(_) => Ok(()),
23 Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => {
25 Err(io::Error::new(
27 io::ErrorKind::InvalidInput,
28 format!("Failed to read and discard a payload from stdin of size {offset} (according to previously loaded RDH): {e}"),
29 ))
30 }
31 Err(e) => Err(e),
32 }
33 }
34}
35
36impl io::Read for StdInReaderSeeker<io::Stdin> {
37 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
38 self.reader.lock().read(buf)
39 }
40}
41impl io::Seek for StdInReaderSeeker<io::Stdin> {
42 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
43 match pos {
44 SeekFrom::Start(_) => Err(io::Error::new(
45 io::ErrorKind::Other,
46 "Cannot seek from start in stdin",
47 )),
48 SeekFrom::Current(_) | SeekFrom::End(_) => {
49 debug_assert!(
50 false,
51 "Seeking from current or end in stdin is not supported"
52 );
53 unsafe { std::hint::unreachable_unchecked() }
54 }
55 }
56 }
57}