From 26faaad2b775b7bd7180988f0380674553552140 Mon Sep 17 00:00:00 2001 From: whitequark Date: Tue, 15 May 2018 08:29:03 +0000 Subject: [PATCH] firmware: remove io::Result in favor of Result. This doesn't yet use impl Fail since std_artiq::io::Error doesn't impl it. --- artiq/firmware/libio/lib.rs | 28 +++++++--------- .../firmware/libproto_artiq/analyzer_proto.rs | 4 +-- .../firmware/libproto_artiq/drtioaux_proto.rs | 6 ++-- artiq/firmware/libproto_artiq/mgmt_proto.rs | 8 ++--- artiq/firmware/libproto_artiq/moninj_proto.rs | 6 ++-- artiq/firmware/libproto_artiq/rpc_proto.rs | 14 ++++---- .../firmware/libproto_artiq/session_proto.rs | 10 +++--- artiq/firmware/runtime/analyzer.rs | 2 +- artiq/firmware/runtime/kern_hwreq.rs | 2 +- artiq/firmware/runtime/mgmt.rs | 8 ++--- artiq/firmware/runtime/moninj.rs | 4 +-- artiq/firmware/runtime/session.rs | 32 +++++++++---------- 12 files changed, 60 insertions(+), 64 deletions(-) diff --git a/artiq/firmware/libio/lib.rs b/artiq/firmware/libio/lib.rs index 654f68c36..b8bc09d7b 100644 --- a/artiq/firmware/libio/lib.rs +++ b/artiq/firmware/libio/lib.rs @@ -11,8 +11,6 @@ extern crate alloc; #[cfg(feature = "byteorder")] extern crate byteorder; -use core::result; - mod cursor; #[cfg(feature = "byteorder")] mod proto; @@ -21,8 +19,6 @@ pub use cursor::Cursor; #[cfg(feature = "byteorder")] pub use proto::{ProtoRead, ProtoWrite}; -pub type Result = result::Result>; - #[derive(Fail, Debug, Clone, PartialEq)] pub enum Error { #[fail(display = "unexpected end of stream")] @@ -44,10 +40,10 @@ pub trait Read { /// Pull some bytes from this source into the specified buffer, returning /// how many bytes were read. - fn read(&mut self, buf: &mut [u8]) -> result::Result; + fn read(&mut self, buf: &mut [u8]) -> Result; /// Read the exact number of bytes required to fill `buf`. - fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<(), Self::ReadError> { + fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<(), Error> { while !buf.is_empty() { let read_bytes = self.read(buf)?; if read_bytes == 0 { @@ -64,7 +60,7 @@ pub trait Read { impl<'a, T: Read> Read for &'a mut T { type ReadError = T::ReadError; - fn read(&mut self, buf: &mut [u8]) -> result::Result { + fn read(&mut self, buf: &mut [u8]) -> Result { T::read(self, buf) } } @@ -74,14 +70,14 @@ pub trait Write { type FlushError; /// Write a buffer into this object, returning how many bytes were written. - fn write(&mut self, buf: &[u8]) -> result::Result; + fn write(&mut self, buf: &[u8]) -> Result; /// Flush this output stream, ensuring that all intermediately buffered contents /// reach their destination. - fn flush(&mut self) -> result::Result<(), Self::FlushError>; + fn flush(&mut self) -> Result<(), Self::FlushError>; /// Attempts to write an entire buffer into `self`. - fn write_all(&mut self, mut buf: &[u8]) -> Result<(), Self::WriteError> { + fn write_all(&mut self, mut buf: &[u8]) -> Result<(), Error> { while buf.len() > 0 { let written_bytes = self.write(buf)?; if written_bytes == 0 { @@ -105,11 +101,11 @@ impl<'a, T: Write> Write for &'a mut T { type WriteError = T::WriteError; type FlushError = T::FlushError; - fn write(&mut self, buf: &[u8]) -> result::Result { + fn write(&mut self, buf: &[u8]) -> Result { T::write(self, buf) } - fn flush(&mut self) -> result::Result<(), Self::FlushError> { + fn flush(&mut self) -> Result<(), Self::FlushError> { T::flush(self) } @@ -122,14 +118,14 @@ impl<'a> Write for &'a mut [u8] { type WriteError = !; type FlushError = !; - fn write(&mut self, buf: &[u8]) -> result::Result { + fn write(&mut self, buf: &[u8]) -> Result { let len = buf.len().min(self.len()); self[..len].copy_from_slice(&buf[..len]); Ok(len) } #[inline] - fn flush(&mut self) -> result::Result<(), Self::FlushError> { + fn flush(&mut self) -> Result<(), Self::FlushError> { Ok(()) } } @@ -139,13 +135,13 @@ impl<'a> Write for alloc::Vec { type WriteError = !; type FlushError = !; - fn write(&mut self, buf: &[u8]) -> result::Result { + fn write(&mut self, buf: &[u8]) -> Result { self.extend_from_slice(buf); Ok(buf.len()) } #[inline] - fn flush(&mut self) -> result::Result<(), Self::FlushError> { + fn flush(&mut self) -> Result<(), Self::FlushError> { Ok(()) } } diff --git a/artiq/firmware/libproto_artiq/analyzer_proto.rs b/artiq/firmware/libproto_artiq/analyzer_proto.rs index 02f81fce2..c74632ec2 100644 --- a/artiq/firmware/libproto_artiq/analyzer_proto.rs +++ b/artiq/firmware/libproto_artiq/analyzer_proto.rs @@ -1,4 +1,4 @@ -use io::{Write, ProtoWrite, Result}; +use io::{Write, ProtoWrite, Error}; #[derive(Debug)] pub struct Header { @@ -10,7 +10,7 @@ pub struct Header { } impl Header { - pub fn write_to(&self, writer: &mut T) -> Result<(), T::WriteError> { + pub fn write_to(&self, writer: &mut T) -> Result<(), Error> { writer.write_u32(self.sent_bytes)?; writer.write_u64(self.total_byte_count)?; writer.write_u8(self.overflow_occurred as u8)?; diff --git a/artiq/firmware/libproto_artiq/drtioaux_proto.rs b/artiq/firmware/libproto_artiq/drtioaux_proto.rs index 1bd4eabb7..0aae19617 100644 --- a/artiq/firmware/libproto_artiq/drtioaux_proto.rs +++ b/artiq/firmware/libproto_artiq/drtioaux_proto.rs @@ -1,4 +1,4 @@ -use io::{Read, ProtoRead, Write, ProtoWrite, Error, Result}; +use io::{Read, ProtoRead, Write, ProtoWrite, Error}; #[derive(Debug)] pub enum Packet { @@ -36,7 +36,7 @@ pub enum Packet { } impl Packet { - pub fn read_from(reader: &mut T) -> Result { + pub fn read_from(reader: &mut T) -> Result> { Ok(match reader.read_u8()? { 0x00 => Packet::EchoRequest, 0x01 => Packet::EchoReply, @@ -133,7 +133,7 @@ impl Packet { }) } - pub fn write_to(&self, writer: &mut T) -> Result<(), T::WriteError> { + pub fn write_to(&self, writer: &mut T) -> Result<(), Error> { match *self { Packet::EchoRequest => writer.write_u8(0x00)?, diff --git a/artiq/firmware/libproto_artiq/mgmt_proto.rs b/artiq/firmware/libproto_artiq/mgmt_proto.rs index d7c2cb8be..3bb33e65d 100644 --- a/artiq/firmware/libproto_artiq/mgmt_proto.rs +++ b/artiq/firmware/libproto_artiq/mgmt_proto.rs @@ -2,7 +2,7 @@ use alloc::Vec; #[cfg(feature = "log")] use log; -use io::{Read, ProtoRead, Write, ProtoWrite, Error, Result}; +use io::{Read, ProtoRead, Write, ProtoWrite, Error}; #[derive(Debug)] pub enum Request { @@ -40,10 +40,10 @@ pub enum Reply<'a> { } impl Request { - pub fn read_from(reader: &mut T) -> Result { + pub fn read_from(reader: &mut T) -> Result> { #[cfg(feature = "log")] fn read_log_level_filter(reader: &mut T) -> - Result { + Result> { Ok(match reader.read_u8()? { 0 => log::LevelFilter::Off, 1 => log::LevelFilter::Error, @@ -79,7 +79,7 @@ impl Request { } impl<'a> Reply<'a> { - pub fn write_to(&self, writer: &mut T) -> Result<(), T::WriteError> { + pub fn write_to(&self, writer: &mut T) -> Result<(), Error> { match *self { Reply::Success => { writer.write_u8(1)?; diff --git a/artiq/firmware/libproto_artiq/moninj_proto.rs b/artiq/firmware/libproto_artiq/moninj_proto.rs index d40dc40dc..285ce9f09 100644 --- a/artiq/firmware/libproto_artiq/moninj_proto.rs +++ b/artiq/firmware/libproto_artiq/moninj_proto.rs @@ -1,4 +1,4 @@ -use io::{Read, ProtoRead, Write, ProtoWrite, Error, Result}; +use io::{Read, ProtoRead, Write, ProtoWrite, Error}; #[derive(Debug)] pub enum HostMessage { @@ -14,7 +14,7 @@ pub enum DeviceMessage { } impl HostMessage { - pub fn read_from(reader: &mut T) -> Result { + pub fn read_from(reader: &mut T) -> Result> { Ok(match reader.read_u8()? { 0 => HostMessage::Monitor { enable: if reader.read_u8()? == 0 { false } else { true }, @@ -36,7 +36,7 @@ impl HostMessage { } impl DeviceMessage { - pub fn write_to(&self, writer: &mut T) -> Result<(), T::WriteError> { + pub fn write_to(&self, writer: &mut T) -> Result<(), Error> { match *self { DeviceMessage::MonitorStatus { channel, probe, value } => { writer.write_u8(0)?; diff --git a/artiq/firmware/libproto_artiq/rpc_proto.rs b/artiq/firmware/libproto_artiq/rpc_proto.rs index 3e0b6594d..f5f8581c1 100644 --- a/artiq/firmware/libproto_artiq/rpc_proto.rs +++ b/artiq/firmware/libproto_artiq/rpc_proto.rs @@ -1,12 +1,12 @@ use core::str; use cslice::{CSlice, CMutSlice}; -use io::{ProtoRead, Read, Write, ProtoWrite, Result}; +use io::{ProtoRead, Read, Write, ProtoWrite, Error}; use self::tag::{Tag, TagIterator, split_tag}; unsafe fn recv_value(reader: &mut T, tag: Tag, data: &mut *mut (), - alloc: &Fn(usize) -> Result<*mut (), T::ReadError>) - -> Result<(), T::ReadError> + alloc: &Fn(usize) -> Result<*mut (), Error>) + -> Result<(), Error> where T: Read + ?Sized { macro_rules! consume_value { @@ -75,8 +75,8 @@ unsafe fn recv_value(reader: &mut T, tag: Tag, data: &mut *mut (), } pub fn recv_return(reader: &mut T, tag_bytes: &[u8], data: *mut (), - alloc: &Fn(usize) -> Result<*mut (), T::ReadError>) - -> Result<(), T::ReadError> + alloc: &Fn(usize) -> Result<*mut (), Error>) + -> Result<(), Error> where T: Read + ?Sized { let mut it = TagIterator::new(tag_bytes); @@ -91,7 +91,7 @@ pub fn recv_return(reader: &mut T, tag_bytes: &[u8], data: *mut (), } unsafe fn send_value(writer: &mut T, tag: Tag, data: &mut *const ()) - -> Result<(), T::WriteError> + -> Result<(), Error> where T: Write + ?Sized { macro_rules! consume_value { @@ -168,7 +168,7 @@ unsafe fn send_value(writer: &mut T, tag: Tag, data: &mut *const ()) } pub fn send_args(writer: &mut T, service: u32, tag_bytes: &[u8], data: *const *const ()) - -> Result<(), T::WriteError> + -> Result<(), Error> where T: Write + ?Sized { let (arg_tags_bytes, return_tag_bytes) = split_tag(tag_bytes); diff --git a/artiq/firmware/libproto_artiq/session_proto.rs b/artiq/firmware/libproto_artiq/session_proto.rs index 0253c8e8c..5ef1fee20 100644 --- a/artiq/firmware/libproto_artiq/session_proto.rs +++ b/artiq/firmware/libproto_artiq/session_proto.rs @@ -1,8 +1,8 @@ use alloc::{Vec, String}; -use io::{Read, ProtoRead, Write, ProtoWrite, Error, Result}; +use io::{Read, ProtoRead, Write, ProtoWrite, Error}; -fn read_sync(reader: &mut T) -> Result<(), T::ReadError> { +fn read_sync(reader: &mut T) -> Result<(), Error> { let mut sync = [0; 4]; for i in 0.. { sync[i % 4] = reader.read_u8()?; @@ -11,7 +11,7 @@ fn read_sync(reader: &mut T) -> Result<(), T::ReadError> { Ok(()) } -fn write_sync(writer: &mut T) -> Result<(), T::WriteError> { +fn write_sync(writer: &mut T) -> Result<(), Error> { writer.write_all(&[0x5a; 4]) } @@ -41,7 +41,7 @@ pub enum Request { } impl Request { - pub fn read_from(reader: &mut T) -> Result { + pub fn read_from(reader: &mut T) -> Result> { read_sync(reader)?; Ok(match reader.read_u8()? { 3 => Request::SystemInfo, @@ -114,7 +114,7 @@ pub enum Reply<'a> { } impl<'a> Reply<'a> { - pub fn write_to(&self, writer: &mut T) -> Result<(), T::WriteError> { + pub fn write_to(&self, writer: &mut T) -> Result<(), Error> { write_sync(writer)?; match *self { Reply::SystemInfo { ident, finished_cleanly } => { diff --git a/artiq/firmware/runtime/analyzer.rs b/artiq/firmware/runtime/analyzer.rs index 8bebf851b..f6842e433 100644 --- a/artiq/firmware/runtime/analyzer.rs +++ b/artiq/firmware/runtime/analyzer.rs @@ -35,7 +35,7 @@ fn disarm() { } } -fn worker(stream: &mut TcpStream) -> io::Result<(), ::std::io::Error> { +fn worker(stream: &mut TcpStream) -> Result<(), io::Error<::std::io::Error>> { let data = unsafe { &BUFFER.data[..] }; let overflow_occurred = unsafe { csr::rtio_analyzer::message_encoder_overflow_read() != 0 }; let total_byte_count = unsafe { csr::rtio_analyzer::dma_byte_count_read() }; diff --git a/artiq/firmware/runtime/kern_hwreq.rs b/artiq/firmware/runtime/kern_hwreq.rs index 610e5be96..ae684511a 100644 --- a/artiq/firmware/runtime/kern_hwreq.rs +++ b/artiq/firmware/runtime/kern_hwreq.rs @@ -291,7 +291,7 @@ mod spi { } } -pub fn process_kern_hwreq(io: &Io, request: &kern::Message) -> io::Result { +pub fn process_kern_hwreq(io: &Io, request: &kern::Message) -> Result> { match request { #[cfg(has_rtio_core)] &kern::RtioInitRequest => { diff --git a/artiq/firmware/runtime/mgmt.rs b/artiq/firmware/runtime/mgmt.rs index b23b6bbcc..c6b744982 100644 --- a/artiq/firmware/runtime/mgmt.rs +++ b/artiq/firmware/runtime/mgmt.rs @@ -8,7 +8,7 @@ use sched::{TcpListener, TcpStream}; use mgmt_proto::*; use profiler; -fn check_magic(stream: &mut TcpStream) -> io::Result<(), ::std::io::Error> { +fn check_magic(stream: &mut TcpStream) -> Result<(), io::Error<::std::io::Error>> { const MAGIC: &'static [u8] = b"ARTIQ management\n"; let mut magic: [u8; 17] = [0; 17]; @@ -20,7 +20,7 @@ fn check_magic(stream: &mut TcpStream) -> io::Result<(), ::std::io::Error> { } } -fn worker(io: &Io, stream: &mut TcpStream) -> io::Result<(), ::std::io::Error> { +fn worker(io: &Io, stream: &mut TcpStream) -> Result<(), io::Error<::std::io::Error>> { check_magic(stream)?; info!("new connection from {}", stream.remote_endpoint()); @@ -34,7 +34,7 @@ fn worker(io: &Io, stream: &mut TcpStream) -> io::Result<(), ::std::io::Error> { } Request::ClearLog => { - BufferLogger::with(|logger| -> io::Result<(), ::std::io::Error> { + BufferLogger::with(|logger| -> Result<(), io::Error<::std::io::Error>> { let mut buffer = io.until_ok(|| logger.buffer())?; Ok(buffer.clear()) })?; @@ -43,7 +43,7 @@ fn worker(io: &Io, stream: &mut TcpStream) -> io::Result<(), ::std::io::Error> { } Request::PullLog => { - BufferLogger::with(|logger| -> io::Result<(), ::std::io::Error> { + BufferLogger::with(|logger| -> Result<(), io::Error<::std::io::Error>> { loop { // Do this *before* acquiring the buffer, since that sets the log level // to OFF. diff --git a/artiq/firmware/runtime/moninj.rs b/artiq/firmware/runtime/moninj.rs index 36fae9430..e19c6b3d2 100644 --- a/artiq/firmware/runtime/moninj.rs +++ b/artiq/firmware/runtime/moninj.rs @@ -9,7 +9,7 @@ use drtioaux; use moninj_proto::*; -fn check_magic(stream: &mut TcpStream) -> io::Result<(), ::std::io::Error> { +fn check_magic(stream: &mut TcpStream) -> Result<(), io::Error<::std::io::Error>> { const MAGIC: &'static [u8] = b"ARTIQ moninj\n"; let mut magic: [u8; 13] = [0; 13]; @@ -159,7 +159,7 @@ fn read_injection_status(channel: u32, probe: u8) -> u8 { 0 } -fn connection_worker(io: &Io, mut stream: &mut TcpStream) -> io::Result<(), ::std::io::Error> { +fn connection_worker(io: &Io, mut stream: &mut TcpStream) -> Result<(), io::Error<::std::io::Error>> { let mut watch_list = BTreeMap::new(); let mut next_check = 0; diff --git a/artiq/firmware/runtime/session.rs b/artiq/firmware/runtime/session.rs index dced8025c..8d987ced9 100644 --- a/artiq/firmware/runtime/session.rs +++ b/artiq/firmware/runtime/session.rs @@ -102,7 +102,7 @@ impl<'a> Drop for Session<'a> { } } -fn check_magic(stream: &mut TcpStream) -> io::Result<(), ::std::io::Error> { +fn check_magic(stream: &mut TcpStream) -> Result<(), io::Error<::std::io::Error>> { const MAGIC: &'static [u8] = b"ARTIQ coredev\n"; let mut magic: [u8; 14] = [0; 14]; @@ -114,7 +114,7 @@ fn check_magic(stream: &mut TcpStream) -> io::Result<(), ::std::io::Error> { } } -fn host_read(stream: &mut TcpStream) -> io::Result { +fn host_read(stream: &mut TcpStream) -> Result> { let request = host::Request::read_from(stream)?; match &request { &host::Request::LoadKernel(_) => debug!("comm<-host LoadLibrary(...)"), @@ -123,12 +123,12 @@ fn host_read(stream: &mut TcpStream) -> io::Result io::Result<(), ::std::io::Error> { +fn host_write(stream: &mut TcpStream, reply: host::Reply) -> Result<(), io::Error<::std::io::Error>> { debug!("comm->host {:?}", reply); reply.write_to(stream) } -pub fn kern_send(io: &Io, request: &kern::Message) -> io::Result<(), ::std::io::Error> { +pub fn kern_send(io: &Io, request: &kern::Message) -> Result<(), io::Error<::std::io::Error>> { match request { &kern::LoadRequest(_) => debug!("comm->kern LoadRequest(...)"), &kern::DmaRetrieveReply { trace, duration } => { @@ -144,8 +144,8 @@ pub fn kern_send(io: &Io, request: &kern::Message) -> io::Result<(), ::std::io:: Ok(io.until(mailbox::acknowledged)?) } -fn kern_recv_notrace(io: &Io, f: F) -> io::Result - where F: FnOnce(&kern::Message) -> io::Result { +fn kern_recv_notrace(io: &Io, f: F) -> Result> + where F: FnOnce(&kern::Message) -> Result> { io.until(|| mailbox::receive() != 0)?; if !kernel::validate(mailbox::receive()) { let message = format!("invalid kernel CPU pointer 0x{:x}", mailbox::receive()); @@ -171,20 +171,20 @@ fn kern_recv_dotrace(reply: &kern::Message) { } #[inline(always)] -fn kern_recv(io: &Io, f: F) -> io::Result - where F: FnOnce(&kern::Message) -> io::Result { +fn kern_recv(io: &Io, f: F) -> Result> + where F: FnOnce(&kern::Message) -> Result> { kern_recv_notrace(io, |reply| { kern_recv_dotrace(reply); f(reply) }) } -pub fn kern_acknowledge() -> io::Result<(), ::std::io::Error> { +pub fn kern_acknowledge() -> Result<(), io::Error<::std::io::Error>> { mailbox::acknowledge(); Ok(()) } -unsafe fn kern_load(io: &Io, session: &mut Session, library: &[u8]) -> io::Result<(), ::std::io::Error> { +unsafe fn kern_load(io: &Io, session: &mut Session, library: &[u8]) -> Result<(), io::Error<::std::io::Error>> { if session.running() { unexpected!("attempted to load a new kernel while a kernel was running") } @@ -209,7 +209,7 @@ unsafe fn kern_load(io: &Io, session: &mut Session, library: &[u8]) -> io::Resul }) } -fn kern_run(session: &mut Session) -> io::Result<(), ::std::io::Error> { +fn kern_run(session: &mut Session) -> Result<(), io::Error<::std::io::Error>> { if session.kernel_state != KernelState::Loaded { unexpected!("attempted to run a kernel while not in Loaded state") } @@ -221,7 +221,7 @@ fn kern_run(session: &mut Session) -> io::Result<(), ::std::io::Error> { fn process_host_message(io: &Io, stream: &mut TcpStream, - session: &mut Session) -> io::Result<(), ::std::io::Error> { + session: &mut Session) -> Result<(), io::Error<::std::io::Error>> { match host_read(stream)? { host::Request::SystemInfo => { host_write(stream, host::Reply::SystemInfo { @@ -359,7 +359,7 @@ fn process_host_message(io: &Io, } fn process_kern_message(io: &Io, mut stream: Option<&mut TcpStream>, - session: &mut Session) -> io::Result { + session: &mut Session) -> Result> { kern_recv_notrace(io, |request| { match (request, session.kernel_state) { (&kern::LoadReply(_), KernelState::Loaded) | @@ -516,7 +516,7 @@ fn process_kern_message(io: &Io, mut stream: Option<&mut TcpStream>, } fn process_kern_queued_rpc(stream: &mut TcpStream, - _session: &mut Session) -> io::Result<(), ::std::io::Error> { + _session: &mut Session) -> Result<(), io::Error<::std::io::Error>> { rpc_queue::dequeue(|slice| { debug!("comm<-kern (async RPC)"); let length = NetworkEndian::read_u32(slice) as usize; @@ -529,7 +529,7 @@ fn process_kern_queued_rpc(stream: &mut TcpStream, fn host_kernel_worker(io: &Io, stream: &mut TcpStream, - congress: &mut Congress) -> io::Result<(), ::std::io::Error> { + congress: &mut Congress) -> Result<(), io::Error<::std::io::Error>> { let mut session = Session::new(congress); loop { @@ -568,7 +568,7 @@ fn host_kernel_worker(io: &Io, fn flash_kernel_worker(io: &Io, congress: &mut Congress, - config_key: &str) -> io::Result<(), ::std::io::Error> { + config_key: &str) -> Result<(), io::Error<::std::io::Error>> { let mut session = Session::new(congress); config::read(config_key, |result| {