From 9e8f8bb669e501065e6bedbbccdc9a9c4be7ecdf Mon Sep 17 00:00:00 2001 From: whitequark Date: Tue, 15 May 2018 06:24:04 +0000 Subject: [PATCH] firmware: extract io::Cursor into its own file. Also remove unused io::CursorError. --- artiq/firmware/libio/cursor.rs | 86 +++++++++++++++++++++++++++++++ artiq/firmware/libio/lib.rs | 93 ++-------------------------------- 2 files changed, 89 insertions(+), 90 deletions(-) create mode 100644 artiq/firmware/libio/cursor.rs diff --git a/artiq/firmware/libio/cursor.rs b/artiq/firmware/libio/cursor.rs new file mode 100644 index 000000000..b820bdddc --- /dev/null +++ b/artiq/firmware/libio/cursor.rs @@ -0,0 +1,86 @@ +use {Read, Write}; + +#[derive(Debug, Clone)] +pub struct Cursor { + inner: T, + pos: usize +} + +impl Cursor { + #[inline] + pub fn new(inner: T) -> Cursor { + Cursor { inner, pos: 0 } + } + + #[inline] + pub fn into_inner(self) -> T { + self.inner + } + + #[inline] + pub fn get_ref(&self) -> &T { + &self.inner + } + + #[inline] + pub fn get_mut(&mut self) -> &mut T { + &mut self.inner + } + + #[inline] + pub fn position(&self) -> usize { + self.pos + } + + #[inline] + pub fn set_position(&mut self, pos: usize) { + self.pos = pos + } +} + +impl> Read for Cursor { + type ReadError = !; + + fn read(&mut self, buf: &mut [u8]) -> Result { + let data = &self.inner.as_ref()[self.pos..]; + let len = buf.len().min(data.len()); + buf[..len].copy_from_slice(&data[..len]); + self.pos += len; + Ok(len) + } +} + +impl<'a> Write for Cursor<&'a mut [u8]> { + type WriteError = !; + type FlushError = !; + + fn write(&mut self, buf: &[u8]) -> Result { + let data = &mut self.inner[self.pos..]; + let len = buf.len().min(data.len()); + data[..len].copy_from_slice(&buf[..len]); + self.pos += len; + Ok(len) + } + + #[inline] + fn flush(&mut self) -> Result<(), Self::FlushError> { + Ok(()) + } +} + +#[cfg(feature = "alloc")] +impl Write for Cursor<::alloc::Vec> { + type WriteError = !; + type FlushError = !; + + #[inline] + fn write(&mut self, buf: &[u8]) -> Result { + self.inner.extend_from_slice(buf); + Ok(buf.len()) + } + + #[inline] + fn flush(&mut self) -> Result<(), Self::FlushError> { + Ok(()) + } +} diff --git a/artiq/firmware/libio/lib.rs b/artiq/firmware/libio/lib.rs index c536e5383..884f57f27 100644 --- a/artiq/firmware/libio/lib.rs +++ b/artiq/firmware/libio/lib.rs @@ -11,9 +11,12 @@ extern crate byteorder; use core::result; use core::fmt; +mod cursor; #[cfg(feature = "byteorder")] pub mod proto; +pub use cursor::Cursor; + pub type Result = result::Result>; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -152,93 +155,3 @@ impl<'a> Write for alloc::Vec { Ok(()) } } - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum CursorError { - EndOfBuffer -} - -#[derive(Debug, Clone)] -pub struct Cursor { - inner: T, - pos: usize -} - -impl Cursor { - #[inline] - pub fn new(inner: T) -> Cursor { - Cursor { inner, pos: 0 } - } - - #[inline] - pub fn into_inner(self) -> T { - self.inner - } - - #[inline] - pub fn get_ref(&self) -> &T { - &self.inner - } - - #[inline] - pub fn get_mut(&mut self) -> &mut T { - &mut self.inner - } - - #[inline] - pub fn position(&self) -> usize { - self.pos - } - - #[inline] - pub fn set_position(&mut self, pos: usize) { - self.pos = pos - } -} - -impl> Read for Cursor { - type ReadError = !; - - fn read(&mut self, buf: &mut [u8]) -> result::Result { - let data = &self.inner.as_ref()[self.pos..]; - let len = buf.len().min(data.len()); - buf[..len].copy_from_slice(&data[..len]); - self.pos += len; - Ok(len) - } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - type WriteError = !; - type FlushError = !; - - fn write(&mut self, buf: &[u8]) -> result::Result { - let data = &mut self.inner[self.pos..]; - let len = buf.len().min(data.len()); - data[..len].copy_from_slice(&buf[..len]); - self.pos += len; - Ok(len) - } - - #[inline] - fn flush(&mut self) -> result::Result<(), Self::FlushError> { - Ok(()) - } -} - -#[cfg(feature = "alloc")] -impl Write for Cursor<::alloc::Vec> { - type WriteError = !; - type FlushError = !; - - #[inline] - fn write(&mut self, buf: &[u8]) -> result::Result { - self.inner.extend_from_slice(buf); - Ok(buf.len()) - } - - #[inline] - fn flush(&mut self) -> result::Result<(), Self::FlushError> { - Ok(()) - } -}