From e292c8ab651f780611caea32a3f1c41aa99843cf Mon Sep 17 00:00:00 2001 From: whitequark Date: Mon, 14 May 2018 13:03:57 +0000 Subject: [PATCH] firmware: migrate mgmt_proto to new libio. --- artiq/firmware/libio/lib.rs | 6 +++--- artiq/firmware/libproto/Cargo.toml | 2 +- artiq/firmware/libproto/lib.rs | 2 ++ artiq/firmware/libproto/mgmt_proto.rs | 19 ++++++++++--------- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/artiq/firmware/libio/lib.rs b/artiq/firmware/libio/lib.rs index f4a289cee..4fb1cef91 100644 --- a/artiq/firmware/libio/lib.rs +++ b/artiq/firmware/libio/lib.rs @@ -154,12 +154,12 @@ impl> Read for Cursor { } } -impl> Write for Cursor { +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.as_mut()[self.pos..]; + let data = &mut self.inner[self.pos..]; let len = buf.len().min(data.len()); data[..len].copy_from_slice(&buf[..len]); self.pos += len; @@ -173,7 +173,7 @@ impl> Write for Cursor { } #[cfg(feature = "alloc")] -impl> Write for Cursor { +impl Write for Cursor<::alloc::Vec> { type WriteError = !; type FlushError = !; diff --git a/artiq/firmware/libproto/Cargo.toml b/artiq/firmware/libproto/Cargo.toml index 5efb9ce81..c1ff3a4c3 100644 --- a/artiq/firmware/libproto/Cargo.toml +++ b/artiq/firmware/libproto/Cargo.toml @@ -11,6 +11,6 @@ path = "lib.rs" byteorder = { version = "1.0", default-features = false } cslice = { version = "0.3" } log = { version = "0.4", default-features = false, optional = true } -io = { path = "../libio", features = ["byteorder"] } +io = { path = "../libio", features = ["byteorder", "alloc"] } std_artiq = { path = "../libstd_artiq", features = ["alloc"] } dyld = { path = "../libdyld" } diff --git a/artiq/firmware/libproto/lib.rs b/artiq/firmware/libproto/lib.rs index 31b8c5d1b..4f4e27018 100644 --- a/artiq/firmware/libproto/lib.rs +++ b/artiq/firmware/libproto/lib.rs @@ -1,5 +1,7 @@ #![no_std] +#![feature(alloc)] +extern crate alloc; extern crate byteorder; extern crate cslice; #[cfg(feature = "log")] diff --git a/artiq/firmware/libproto/mgmt_proto.rs b/artiq/firmware/libproto/mgmt_proto.rs index 8ac1a95ec..44e6b09dc 100644 --- a/artiq/firmware/libproto/mgmt_proto.rs +++ b/artiq/firmware/libproto/mgmt_proto.rs @@ -1,9 +1,10 @@ -use std::vec::Vec; -use std::io::{self, Read, Write}; -use {ReadExt, WriteExt}; +use alloc::vec::Vec; #[cfg(feature = "log")] use log; +use io::{Read, Write, Error, Result}; +use io::proto::{ProtoRead, ProtoWrite}; + #[derive(Debug)] pub enum Request { GetLog, @@ -40,9 +41,10 @@ pub enum Reply<'a> { } impl Request { - pub fn read_from(reader: &mut Read) -> io::Result { + pub fn read_from(reader: &mut T) -> Result { #[cfg(feature = "log")] - fn read_log_level_filter(reader: &mut Read) -> io::Result { + fn read_log_level_filter(reader: &mut T) -> + Result { Ok(match reader.read_u8()? { 0 => log::LevelFilter::Off, 1 => log::LevelFilter::Error, @@ -50,8 +52,7 @@ impl Request { 3 => log::LevelFilter::Info, 4 => log::LevelFilter::Debug, 5 => log::LevelFilter::Trace, - _ => return Err(io::Error::new(io::ErrorKind::InvalidData, - "invalid log level")) + _ => return Err(Error::Unrecognized) }) } @@ -73,13 +74,13 @@ impl Request { 4 => Request::Hotswap(reader.read_bytes()?), 5 => Request::Reboot, 8 => Request::DebugAllocator, - _ => return Err(io::Error::new(io::ErrorKind::InvalidData, "unknown request type")) + _ => return Err(Error::Unrecognized) }) } } impl<'a> Reply<'a> { - pub fn write_to(&self, writer: &mut Write) -> io::Result<()> { + pub fn write_to(&self, writer: &mut T) -> Result<(), T::WriteError> { match *self { Reply::Success => { writer.write_u8(1)?;