diff --git a/apps/system/components/DebugConsole/kata-io/src/lib.rs b/apps/system/components/DebugConsole/kata-io/src/lib.rs index b75d935..68e73d3 100644 --- a/apps/system/components/DebugConsole/kata-io/src/lib.rs +++ b/apps/system/components/DebugConsole/kata-io/src/lib.rs @@ -116,30 +116,6 @@ impl Read for &[u8] { } } -/// Forwarding implementation of Read for &mut -impl<'a, T: ?Sized> Read for &'a mut T -where - T: Read, -{ - fn read(&mut self, buf: &mut [u8]) -> Result { - (**self).read(buf) - } -} - -/// Forwarding implementation of Write for &mut -impl<'a, T: ?Sized> Write for &'a mut T -where - T: Write, -{ - fn write(&mut self, buf: &[u8]) -> Result { - (**self).write(buf) - } - - fn flush(&mut self) -> Result<()> { - (**self).flush() - } -} - pub struct BufReader { inner: R, buf: Box<[u8]>, @@ -206,3 +182,41 @@ impl BufRead for BufReader { self.pos = cmp::min(self.pos + amt, self.cap); } } + +/// Forwarding implementation of Read for &mut +impl<'a, T: ?Sized> Read for &'a mut T +where + T: Read, +{ + fn read(&mut self, buf: &mut [u8]) -> Result { + (**self).read(buf) + } +} + +/// Forwarding implementation of BufRead for &mut +impl<'a, T: ?Sized> BufRead for &'a mut T +where + T: BufRead, +{ + fn fill_buf(&mut self) -> Result<&[u8]> { + (**self).fill_buf() + } + + fn consume(&mut self, amt: usize) { + (**self).consume(amt) + } +} + +/// Forwarding implementation of Write for &mut +impl<'a, T: ?Sized> Write for &'a mut T +where + T: Write, +{ + fn write(&mut self, buf: &[u8]) -> Result { + (**self).write(buf) + } + + fn flush(&mut self) -> Result<()> { + (**self).flush() + } +} diff --git a/apps/system/components/DebugConsole/kata-shell/src/lib.rs b/apps/system/components/DebugConsole/kata-shell/src/lib.rs index ad15b8c..e0444c3 100644 --- a/apps/system/components/DebugConsole/kata-shell/src/lib.rs +++ b/apps/system/components/DebugConsole/kata-shell/src/lib.rs @@ -66,7 +66,7 @@ impl From for CommandError { } /// Read-eval-print loop for the DebugConsole command line interface. -pub fn repl(output: &mut dyn io::Write, input: &mut dyn io::Read) -> ! { +pub fn repl(output: &mut dyn io::Write, input: &mut T) -> ! { let mut line_reader = LineReader::new(); loop { const PROMPT: &str = "KATA> "; @@ -84,7 +84,7 @@ pub fn repl(output: &mut dyn io::Write, input: &mut dyn io::Read) -> ! { /// /// The line is split on whitespace. The first token is the command; the /// remaining tokens are the arguments. -fn dispatch_command(cmdline: &str, input: &mut dyn io::Read, output: &mut dyn io::Write) { +fn dispatch_command(cmdline: &str, input: &mut dyn io::BufRead, output: &mut dyn io::Write) { let mut args = cmdline.split_ascii_whitespace(); match args.nth(0) { Some(command) => { @@ -193,7 +193,7 @@ fn loglevel_command( /// Implements a command to receive a blob using ZMODEM. fn rz_command( - input: &mut dyn io::Read, + input: &mut dyn io::BufRead, mut output: &mut dyn io::Write, ) -> Result<(), CommandError> { let upload = rz::rz(input, &mut output)?; diff --git a/apps/system/components/DebugConsole/kata-shell/src/rz.rs b/apps/system/components/DebugConsole/kata-shell/src/rz.rs index 5962054..e88f16d 100644 --- a/apps/system/components/DebugConsole/kata-shell/src/rz.rs +++ b/apps/system/components/DebugConsole/kata-shell/src/rz.rs @@ -43,7 +43,7 @@ impl io::Write for Upload { } /// Receives using ZMODEM and wraps the result as an Upload. -pub fn rz(r: R, w: W) -> Result { +pub fn rz(r: R, w: W) -> Result { let mut upload = Upload::new(); zmodem::recv::recv(r, w, &mut upload)?; Ok(upload) diff --git a/apps/system/components/DebugConsole/zmodem/src/proto.rs b/apps/system/components/DebugConsole/zmodem/src/proto.rs index e13e164..0c6da92 100644 --- a/apps/system/components/DebugConsole/zmodem/src/proto.rs +++ b/apps/system/components/DebugConsole/zmodem/src/proto.rs @@ -106,37 +106,15 @@ where Ok(()) } -/// Read into buf until and including the byte delim -/// -/// This is a one-off implementation of a method of io::BufRead that the -/// original lexxvir/zmodem had used. (So far kata_io is deferring implementing -/// buffering to validate it will become needed in more places.) -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) -> io::Result -where - R: io::Read, -{ - let mut n = 0usize; - loop { - let b = read_byte(r)?; - buf.push(b); - n += 1; - if b == delim { - break; - } - } - - Ok(n) -} - /// Receives sequence: ZLDE ZCRC* /// Unescapes sequencies such as 'ZLDE ' /// If Ok returns in buf and ZCRC* byte as return value pub fn recv_zlde_frame(header: u8, r: &mut R, buf: &mut Vec) -> io::Result> where - R: io::Read, + R: io::BufRead, { loop { - read_until(r, ZLDE, buf)?; + r.read_until(ZLDE, buf)?; let b = read_byte(r)?; if !is_escaped(b) { @@ -173,7 +151,7 @@ pub fn recv_data( data_out: &mut DO, ) -> io::Result where - CI: io::Read, + CI: io::BufRead, CO: io::Write, DO: io::Write, { diff --git a/apps/system/components/DebugConsole/zmodem/src/recv.rs b/apps/system/components/DebugConsole/zmodem/src/recv.rs index 8eff426..96a15e9 100644 --- a/apps/system/components/DebugConsole/zmodem/src/recv.rs +++ b/apps/system/components/DebugConsole/zmodem/src/recv.rs @@ -59,7 +59,7 @@ pub fn recv( mut data_out: DO, ) -> io::Result where - CI: io::Read, + CI: io::BufRead, CO: io::Write, DO: io::Write, {