firmware: migrate mgmt_proto to new libio.

This commit is contained in:
whitequark 2018-05-14 13:03:57 +00:00
parent 0c1caf0744
commit e292c8ab65
4 changed files with 16 additions and 13 deletions

View File

@ -154,12 +154,12 @@ impl<T: AsRef<[u8]>> Read for Cursor<T> {
} }
} }
impl<T: AsMut<[u8]>> Write for Cursor<T> { impl<'a> Write for Cursor<&'a mut [u8]> {
type WriteError = !; type WriteError = !;
type FlushError = !; type FlushError = !;
fn write(&mut self, buf: &[u8]) -> result::Result<usize, Self::WriteError> { fn write(&mut self, buf: &[u8]) -> result::Result<usize, Self::WriteError> {
let data = &mut self.inner.as_mut()[self.pos..]; let data = &mut self.inner[self.pos..];
let len = buf.len().min(data.len()); let len = buf.len().min(data.len());
data[..len].copy_from_slice(&buf[..len]); data[..len].copy_from_slice(&buf[..len]);
self.pos += len; self.pos += len;
@ -173,7 +173,7 @@ impl<T: AsMut<[u8]>> Write for Cursor<T> {
} }
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
impl<T: ::alloc::Vec<[u8]>> Write for Cursor<T> { impl Write for Cursor<::alloc::Vec<u8>> {
type WriteError = !; type WriteError = !;
type FlushError = !; type FlushError = !;

View File

@ -11,6 +11,6 @@ path = "lib.rs"
byteorder = { version = "1.0", default-features = false } byteorder = { version = "1.0", default-features = false }
cslice = { version = "0.3" } cslice = { version = "0.3" }
log = { version = "0.4", default-features = false, optional = true } 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"] } std_artiq = { path = "../libstd_artiq", features = ["alloc"] }
dyld = { path = "../libdyld" } dyld = { path = "../libdyld" }

View File

@ -1,5 +1,7 @@
#![no_std] #![no_std]
#![feature(alloc)]
extern crate alloc;
extern crate byteorder; extern crate byteorder;
extern crate cslice; extern crate cslice;
#[cfg(feature = "log")] #[cfg(feature = "log")]

View File

@ -1,9 +1,10 @@
use std::vec::Vec; use alloc::vec::Vec;
use std::io::{self, Read, Write};
use {ReadExt, WriteExt};
#[cfg(feature = "log")] #[cfg(feature = "log")]
use log; use log;
use io::{Read, Write, Error, Result};
use io::proto::{ProtoRead, ProtoWrite};
#[derive(Debug)] #[derive(Debug)]
pub enum Request { pub enum Request {
GetLog, GetLog,
@ -40,9 +41,10 @@ pub enum Reply<'a> {
} }
impl Request { impl Request {
pub fn read_from(reader: &mut Read) -> io::Result<Request> { pub fn read_from<T: Read>(reader: &mut T) -> Result<Request, T::ReadError> {
#[cfg(feature = "log")] #[cfg(feature = "log")]
fn read_log_level_filter(reader: &mut Read) -> io::Result<log::LevelFilter> { fn read_log_level_filter<T: Read>(reader: &mut T) ->
Result<log::LevelFilter, T::ReadError> {
Ok(match reader.read_u8()? { Ok(match reader.read_u8()? {
0 => log::LevelFilter::Off, 0 => log::LevelFilter::Off,
1 => log::LevelFilter::Error, 1 => log::LevelFilter::Error,
@ -50,8 +52,7 @@ impl Request {
3 => log::LevelFilter::Info, 3 => log::LevelFilter::Info,
4 => log::LevelFilter::Debug, 4 => log::LevelFilter::Debug,
5 => log::LevelFilter::Trace, 5 => log::LevelFilter::Trace,
_ => return Err(io::Error::new(io::ErrorKind::InvalidData, _ => return Err(Error::Unrecognized)
"invalid log level"))
}) })
} }
@ -73,13 +74,13 @@ impl Request {
4 => Request::Hotswap(reader.read_bytes()?), 4 => Request::Hotswap(reader.read_bytes()?),
5 => Request::Reboot, 5 => Request::Reboot,
8 => Request::DebugAllocator, 8 => Request::DebugAllocator,
_ => return Err(io::Error::new(io::ErrorKind::InvalidData, "unknown request type")) _ => return Err(Error::Unrecognized)
}) })
} }
} }
impl<'a> Reply<'a> { impl<'a> Reply<'a> {
pub fn write_to(&self, writer: &mut Write) -> io::Result<()> { pub fn write_to<T: Write>(&self, writer: &mut T) -> Result<(), T::WriteError> {
match *self { match *self {
Reply::Success => { Reply::Success => {
writer.write_u8(1)?; writer.write_u8(1)?;