From 4904a61ca08d7ca992afbb8bf94b1d08bd32462a Mon Sep 17 00:00:00 2001 From: Matt Harvey Date: Thu, 16 Sep 2021 18:42:08 +0000 Subject: [PATCH] Merge "Implements some std::io functions in kata_io" GitOrigin-RevId: b5b1afb7b0528acd100fd35a84a48b984f1b02c1 --- .../DebugConsole/kata-io/src/lib.rs | 51 +++++++++++++++---- .../DebugConsole/kata-line-reader/src/lib.rs | 10 +++- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/apps/system/components/DebugConsole/kata-io/src/lib.rs b/apps/system/components/DebugConsole/kata-io/src/lib.rs index 9ca92d3..5557257 100644 --- a/apps/system/components/DebugConsole/kata-io/src/lib.rs +++ b/apps/system/components/DebugConsole/kata-io/src/lib.rs @@ -4,14 +4,43 @@ pub struct Error; pub type Result = core::result::Result; -/// Interface for the CLI to consume bytes. +/// Partial mimic of std::io::Read. pub trait Read { fn read(&mut self, buf: &mut [u8]) -> Result; + + fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<()> { + while !buf.is_empty() { + match self.read(buf) { + Ok(0) => break, + Ok(n) => { + let tmp = buf; + buf = &mut tmp[n..]; + } + Err(e) => return Err(e), + } + } + if !buf.is_empty() { + Err(Error) + } else { + Ok(()) + } + } } -/// Interface for the CLI to emit bytes. +/// Partial mimic of std::io::Write. pub trait Write { fn write(&mut self, buf: &[u8]) -> Result; + + fn write_all(&mut self, mut buf: &[u8]) -> Result<()> { + while !buf.is_empty() { + match self.write(buf) { + Ok(0) => return Err(Error), + Ok(n) => buf = &buf[n..], + Err(e) => return Err(e), + } + } + Ok(()) + } } /// Adapter for writing core::fmt formatted strings. @@ -25,13 +54,13 @@ impl core::fmt::Write for dyn Write + '_ { } } -impl dyn Read + '_ { - pub fn get_u8(&mut self) -> Result { - let mut buf: [u8; 1] = [0u8]; - let n_read = self.read(&mut buf)?; - match n_read { - 1usize => Ok(buf[0]), - _ => Err(Error), - } - } +pub enum SeekFrom { + Start(u64), + End(i64), + Current(i64), +} + +/// Partial mimic of std::io::Seek. +pub trait Seek { + fn seek(&mut self, pos: SeekFrom) -> Result; } diff --git a/apps/system/components/DebugConsole/kata-line-reader/src/lib.rs b/apps/system/components/DebugConsole/kata-line-reader/src/lib.rs index ccecae5..5bce7e0 100644 --- a/apps/system/components/DebugConsole/kata-line-reader/src/lib.rs +++ b/apps/system/components/DebugConsole/kata-line-reader/src/lib.rs @@ -39,6 +39,12 @@ pub struct LineReader { buf: [u8; LINE_MAX], } +fn get_u8(reader: &mut dyn io::Read) -> io::Result { + let mut buf: [u8; 1] = [0u8]; + reader.read_exact(&mut buf)?; + Ok(buf[0]) +} + impl LineReader { pub fn new() -> LineReader { LineReader { @@ -55,13 +61,13 @@ impl LineReader { const BACKSPACE: u8 = 8u8; let mut len = 0; while len < self.buf.len() { - let mut c = input.get_u8()?; + let mut c = get_u8(input)?; while c == DEL || c == BACKSPACE { if len > 0 { output.write(&[BACKSPACE, b' ', BACKSPACE])?; len -= 1; } - c = input.get_u8()?; + c = get_u8(input)?; } if c == b'\r' || c == b'\n' { if len > 0 {