From 4aedb5a5eecd19be2d5a76263c018a8e347d1b9a Mon Sep 17 00:00:00 2001 From: Simon Renblad Date: Mon, 7 Oct 2024 17:01:07 +0800 Subject: [PATCH] removing core_io dependency --- src/Cargo.lock | 5 - src/libboard_artiq/Cargo.toml.tpl | 1 - src/libboard_artiq/src/drtioaux.rs | 5 +- src/libboard_artiq/src/drtioaux_async.rs | 5 +- src/libboard_artiq/src/drtioaux_proto.rs | 14 +-- src/libboard_artiq/src/lib.rs | 1 - src/libio/Cargo.toml.tpl | 1 - src/libio/cursor.rs | 20 +++- src/libio/lib.rs | 135 ++++++++++++++++++++++- src/libio/proto.rs | 8 +- src/libksupport/Cargo.toml.tpl | 1 - src/libksupport/src/eh_artiq.rs | 9 +- src/libksupport/src/rpc.rs | 31 ++++-- src/runtime/Cargo.toml.tpl | 3 +- src/runtime/src/comms.rs | 6 +- src/satman/Cargo.toml.tpl | 1 - src/satman/src/main.rs | 1 - src/satman/src/subkernel.rs | 5 +- 18 files changed, 195 insertions(+), 57 deletions(-) diff --git a/src/Cargo.lock b/src/Cargo.lock index 1cd121c..645216a 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -229,7 +229,6 @@ name = "io" version = "0.0.0" dependencies = [ "byteorder", - "core_io", "libsupport_zynq", ] @@ -239,7 +238,6 @@ version = "0.1.0" dependencies = [ "build_zynq", "byteorder", - "core_io", "cslice", "dwarf", "dyld", @@ -278,7 +276,6 @@ name = "libboard_artiq" version = "0.0.0" dependencies = [ "build_zynq", - "core_io", "crc", "embedded-hal", "io", @@ -519,7 +516,6 @@ dependencies = [ "async-recursion", "build_zynq", "byteorder", - "core_io", "cslice", "dwarf", "dyld", @@ -560,7 +556,6 @@ version = "0.0.0" dependencies = [ "build_zynq", "byteorder", - "core_io", "crc", "cslice", "embedded-hal", diff --git a/src/libboard_artiq/Cargo.toml.tpl b/src/libboard_artiq/Cargo.toml.tpl index ab8270f..7e3b963 100644 --- a/src/libboard_artiq/Cargo.toml.tpl +++ b/src/libboard_artiq/Cargo.toml.tpl @@ -20,7 +20,6 @@ build_zynq = { path = "../libbuild_zynq" } log = "0.4" log_buffer = { version = "1.2" } crc = { version = "1.7", default-features = false } -core_io = { version = "0.1", features = ["collections"] } embedded-hal = "0.2" nb = "1.0" void = { version = "1", default-features = false } diff --git a/src/libboard_artiq/src/drtioaux.rs b/src/libboard_artiq/src/drtioaux.rs index 9e27ac9..4bb12cc 100644 --- a/src/libboard_artiq/src/drtioaux.rs +++ b/src/libboard_artiq/src/drtioaux.rs @@ -1,9 +1,8 @@ use core::slice; -use core_io::{Error as IoError, ErrorKind as IoErrorKind}; use crc; use io::{proto::{ProtoRead, ProtoWrite}, - Cursor}; + Cursor, Error as IoError}; use libboard_zynq::{time::Milliseconds, timer::GlobalTimer}; pub use crate::drtioaux_proto::Packet; @@ -80,7 +79,7 @@ pub fn recv(linkno: u8) -> Result, Error> { receive(linkno, |buffer| { if buffer.len() < 8 { - return Err(IoError::new(IoErrorKind::UnexpectedEof, "Unexpected end").into()); + return Err(IoError::UnexpectedEnd).into()); } let mut reader = Cursor::new(buffer); diff --git a/src/libboard_artiq/src/drtioaux_async.rs b/src/libboard_artiq/src/drtioaux_async.rs index 08f18cf..3244432 100644 --- a/src/libboard_artiq/src/drtioaux_async.rs +++ b/src/libboard_artiq/src/drtioaux_async.rs @@ -1,9 +1,8 @@ use core::slice; -use core_io::{Error as IoError, ErrorKind as IoErrorKind}; use crc; use io::{proto::{ProtoRead, ProtoWrite}, - Cursor}; + Cursor, Error as IoError}; use libasync::{block_async, task}; use libboard_zynq::{time::Milliseconds, timer::GlobalTimer}; use nb; @@ -58,7 +57,7 @@ pub async fn recv(linkno: u8) -> Result, Error> { receive(linkno, |buffer| { if buffer.len() < 8 { - return Err(IoError::new(IoErrorKind::UnexpectedEof, "Unexpected end").into()); + return Err(IoError::UnexpectedEnd).into()); } let mut reader = Cursor::new(buffer); diff --git a/src/libboard_artiq/src/drtioaux_proto.rs b/src/libboard_artiq/src/drtioaux_proto.rs index d00cf96..370d288 100644 --- a/src/libboard_artiq/src/drtioaux_proto.rs +++ b/src/libboard_artiq/src/drtioaux_proto.rs @@ -1,4 +1,4 @@ -use core_io::{Error as IoError, Read, Write}; +use io::{Error as IoError, Read, Write}; use io::proto::{ProtoRead, ProtoWrite}; const MAX_PACKET: usize = 1024; @@ -10,13 +10,13 @@ pub const SAT_PAYLOAD_MAX_SIZE: usize = /*max size*/MAX_PACKET - /*CRC*/4 - /*p pub const MASTER_PAYLOAD_MAX_SIZE: usize = SAT_PAYLOAD_MAX_SIZE - /*source*/1 - /*destination*/1 - /*ID*/4; #[derive(Debug)] -pub enum Error { +pub enum Error { UnknownPacket(u8), - Io(IoError), + Io(IoError), } -impl From for Error { - fn from(value: IoError) -> Error { +impl From> for Error { + fn from(value: IoError) -> Error { Error::Io(value) } } @@ -362,7 +362,7 @@ pub enum Packet { } impl Packet { - pub fn read_from(reader: &mut R) -> Result + pub fn read_from(reader: &mut R) -> Result> where R: Read + ?Sized { Ok(match reader.read_u8()? { 0x00 => Packet::EchoRequest, @@ -749,7 +749,7 @@ impl Packet { }) } - pub fn write_to(&self, writer: &mut W) -> Result<(), IoError> + pub fn write_to(&self, writer: &mut W) -> Result<(), IoError> where W: Write + ?Sized { match *self { Packet::EchoRequest => writer.write_u8(0x00)?, diff --git a/src/libboard_artiq/src/lib.rs b/src/libboard_artiq/src/lib.rs index 70c4cb6..b70ec4d 100644 --- a/src/libboard_artiq/src/lib.rs +++ b/src/libboard_artiq/src/lib.rs @@ -3,7 +3,6 @@ #![feature(naked_functions)] #![feature(asm)] -extern crate core_io; extern crate crc; extern crate embedded_hal; extern crate io; diff --git a/src/libio/Cargo.toml.tpl b/src/libio/Cargo.toml.tpl index f6bd041..e5a08d3 100644 --- a/src/libio/Cargo.toml.tpl +++ b/src/libio/Cargo.toml.tpl @@ -8,7 +8,6 @@ name = "io" path = "lib.rs" [dependencies] -core_io = { version = "0.1", features = ["collections"] } byteorder = { version = "1.0", default-features = false, optional = true } libsupport_zynq = { path = "@@ZYNQ_RS@@/libsupport_zynq", default-features = false, features = ["alloc_core"] } diff --git a/src/libio/cursor.rs b/src/libio/cursor.rs index bd397ac..b07ed4d 100644 --- a/src/libio/cursor.rs +++ b/src/libio/cursor.rs @@ -1,7 +1,7 @@ #[cfg(feature = "alloc")] use alloc::vec::Vec; -use core_io::{Error as IoError, Read, Write}; +use crate::{Read, Write}; #[derive(Debug, Clone)] pub struct Cursor { @@ -42,7 +42,9 @@ impl Cursor { } impl> Read for Cursor { - fn read(&mut self, buf: &mut [u8]) -> Result { + 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()); // ``copy_from_slice`` generates AXI bursts, use a regular loop instead @@ -55,7 +57,10 @@ impl> Read for Cursor { } impl Write for Cursor<&mut [u8]> { - fn write(&mut self, buf: &[u8]) -> Result { + 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()); for i in 0..len { @@ -66,20 +71,23 @@ impl Write for Cursor<&mut [u8]> { } #[inline] - fn flush(&mut self) -> Result<(), IoError> { + fn flush(&mut self) -> Result<(), Self::FlushError> { Ok(()) } } #[cfg(feature = "alloc")] impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> Result { + type WriteError = !; + type FlushError = !; + + fn write(&mut self, buf: &[u8]) -> Result { self.inner.extend_from_slice(buf); Ok(buf.len()) } #[inline] - fn flush(&mut self) -> Result<(), IoError> { + fn flush(&mut self) -> Result<(), Self::FlushError> { Ok(()) } } diff --git a/src/libio/lib.rs b/src/libio/lib.rs index 7abe5c1..ed115ac 100644 --- a/src/libio/lib.rs +++ b/src/libio/lib.rs @@ -3,7 +3,6 @@ #[cfg(feature = "alloc")] extern crate alloc; -extern crate core_io; #[cfg(feature = "byteorder")] extern crate byteorder; @@ -12,8 +11,142 @@ pub mod cursor; #[cfg(feature = "byteorder")] pub mod proto; +use core::fmt::{Display, Formatter}; + pub use cursor::Cursor; #[cfg(all(feature = "byteorder", feature = "alloc"))] pub use proto::ReadStringError; #[cfg(feature = "byteorder")] pub use proto::{ProtoRead, ProtoWrite}; + +#[derive(Debug, Clone, PartialEq)] +pub enum Error { + UnexpectedEnd, + Other(T), +} + +impl From for Error { + fn from(value: T) -> Error { + Error::Other(value) + } +} + +impl Display for Error { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> { + match self { + Error::UnexpectedEnd => write!(f, "unexpected end of stream"), + Error::Other(other) => write!(f, "other IO error: {}", other), + } + } +} + +pub trait Read { + type ReadError; + + /// Pull some bytes from this source into the specified buffer, returning + /// how many bytes were read. + 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<(), Error> { + while !buf.is_empty() { + let read_bytes = self.read(buf)?; + if read_bytes == 0 { + return Err(Error::UnexpectedEnd); + } + + buf = &mut { buf }[read_bytes..]; + } + + Ok(()) + } +} + +impl<'a, T: Read> Read for &'a mut T { + type ReadError = T::ReadError; + + fn read(&mut self, buf: &mut [u8]) -> Result { + T::read(self, buf) + } +} + +pub trait Write { + type WriteError; + type FlushError; + + /// Write a buffer into this object, returning how many bytes were written. + 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<(), Self::FlushError>; + + /// Attempts to write an entire buffer into `self`. + fn write_all(&mut self, mut buf: &[u8]) -> Result<(), Error> { + while buf.len() > 0 { + let written_bytes = self.write(buf)?; + if written_bytes == 0 { + return Err(Error::UnexpectedEnd); + } + + buf = &buf[written_bytes..]; + } + + Ok(()) + } + + /// Hints the writer how much bytes will be written after call to this function. + /// + /// At least `min` bytes should be written after the call to this function and + /// if `max` is `Some(x)` than at most `x` bytes should be written. + fn size_hint(&mut self, _min: usize, _max: Option) {} +} + +impl<'a, T: Write> Write for &'a mut T { + type WriteError = T::WriteError; + type FlushError = T::FlushError; + + fn write(&mut self, buf: &[u8]) -> Result { + T::write(self, buf) + } + + fn flush(&mut self) -> Result<(), Self::FlushError> { + T::flush(self) + } + + fn size_hint(&mut self, min: usize, max: Option) { + T::size_hint(self, min, max) + } +} + +impl<'a> Write for &'a mut [u8] { + type WriteError = !; + type FlushError = !; + + 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<(), Self::FlushError> { + Ok(()) + } +} + +#[cfg(feature = "alloc")] +impl<'a> Write for alloc::vec::Vec { + type WriteError = !; + type FlushError = !; + + fn write(&mut self, buf: &[u8]) -> Result { + self.extend_from_slice(buf); + Ok(buf.len()) + } + + #[inline] + fn flush(&mut self) -> Result<(), Self::FlushError> { + Ok(()) + } +} diff --git a/src/libio/proto.rs b/src/libio/proto.rs index a7c36b6..c4ba6dc 100644 --- a/src/libio/proto.rs +++ b/src/libio/proto.rs @@ -3,7 +3,9 @@ use alloc::{string::String, vec}; use core::str::Utf8Error; use byteorder::{ByteOrder, NativeEndian}; -use core_io::{Error as IoError, Read, Write}; +use Error as IoError; +use Read; +use Write; #[allow(dead_code)] #[derive(Debug, Clone, PartialEq)] @@ -147,7 +149,7 @@ pub trait ProtoWrite { impl ProtoRead for T where T: Read + ?Sized { - type ReadError = IoError; + type ReadError = IoError; fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::ReadError> { T::read_exact(self, buf) @@ -157,7 +159,7 @@ where T: Read + ?Sized impl ProtoWrite for T where T: Write + ?Sized { - type WriteError = IoError; + type WriteError = IoError; fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::WriteError> { T::write_all(self, buf) diff --git a/src/libksupport/Cargo.toml.tpl b/src/libksupport/Cargo.toml.tpl index 65f9e94..e06c1c9 100644 --- a/src/libksupport/Cargo.toml.tpl +++ b/src/libksupport/Cargo.toml.tpl @@ -12,7 +12,6 @@ build_zynq = { path = "../libbuild_zynq" } cslice = "0.3" log = "0.4" nb = "0.1" -core_io = { version = "0.1", features = ["collections"] } byteorder = { version = "1.3", default-features = false } void = { version = "1", default-features = false } log_buffer = { version = "1.2" } diff --git a/src/libksupport/src/eh_artiq.rs b/src/libksupport/src/eh_artiq.rs index 70b27a3..4741c5e 100644 --- a/src/libksupport/src/eh_artiq.rs +++ b/src/libksupport/src/eh_artiq.rs @@ -14,10 +14,9 @@ use core::mem; -use core_io::Error as ReadError; use cslice::{AsCSlice, CSlice}; use dwarf::eh::{self, EHAction, EHContext}; -use io::{Cursor, ProtoRead}; +use io::{Cursor, Error as ReadError, ProtoRead, Read}; use libc::{c_int, c_void, uintptr_t}; use log::{error, trace}; use unwind as uw; @@ -295,7 +294,9 @@ pub unsafe extern "C" fn raise(exception: *const Exception) -> ! { unreachable!(); } -fn read_exception_string<'a>(reader: &mut Cursor<&[u8]>) -> Result, ReadError> { +fn read_exception_string<'a, 'b>( + reader: &mut Cursor<&'b [u8]>, +) -> Result, ReadError< as Read>::ReadError>> { let len = reader.read_u32()? as usize; if len == usize::MAX { let data = reader.read_u32()?; @@ -311,7 +312,7 @@ fn read_exception_string<'a>(reader: &mut Cursor<&[u8]>) -> Result Result { +fn read_exception(raw_exception: &[u8]) -> Result as Read>::ReadError>> { let mut reader = Cursor::new(raw_exception); let mut byte = reader.read_u8()?; diff --git a/src/libksupport/src/rpc.rs b/src/libksupport/src/rpc.rs index 20e10cf..4bfa425 100644 --- a/src/libksupport/src/rpc.rs +++ b/src/libksupport/src/rpc.rs @@ -1,9 +1,8 @@ use core::str; use byteorder::{ByteOrder, NativeEndian}; -use core_io::{Error, Read, Write}; use cslice::{CMutSlice, CSlice}; -use io::{ProtoRead, ProtoWrite}; +use io::{Error, ProtoRead, ProtoWrite, Read, Write}; use log::trace; use self::tag::{split_tag, Tag, TagIterator}; @@ -37,16 +36,17 @@ pub unsafe fn align_ptr_mut(ptr: *mut ()) -> *mut T { // versions for reader rather than TcpStream // they will be made into sync for satellite subkernels later -unsafe fn recv_elements( +unsafe fn recv_elements( reader: &mut R, elt_tag: Tag, length: usize, storage: *mut (), alloc: &mut F, -) -> Result<(), Error> +) -> Result<(), E> where F: FnMut(usize) -> *mut (), R: Read + ?Sized, + E: From>, { match elt_tag { Tag::Bool => { @@ -79,10 +79,11 @@ where Ok(()) } -unsafe fn recv_value(reader: &mut R, tag: Tag, data: &mut *mut (), alloc: &mut F) -> Result<(), Error> +unsafe fn recv_value(reader: &mut R, tag: Tag, data: &mut *mut (), alloc: &mut F) -> Result<(), E> where F: FnMut(usize) -> *mut (), R: Read + ?Sized, + E: From>, { macro_rules! consume_value { ($ty:ty, | $ptr:ident | $map:expr) => {{ @@ -175,15 +176,16 @@ where } } -pub fn recv_return<'a, F, R>( +pub fn recv_return<'a, F, R, E>( reader: &mut R, tag_bytes: &'a [u8], data: *mut (), alloc: &mut F, -) -> Result<&'a [u8], Error> +) -> Result<&'a [u8], E> where F: FnMut(usize) -> *mut (), R: Read + ?Sized, + E: From>, { let mut it = TagIterator::new(tag_bytes); trace!("recv ...->{}", it); @@ -201,7 +203,7 @@ unsafe fn send_elements( length: usize, data: *const (), write_tags: bool, -) -> Result<(), Error> +) -> Result<(), Error> where W: Write + ?Sized, { @@ -233,8 +235,15 @@ where Ok(()) } -unsafe fn send_value(writer: &mut W, tag: Tag, data: &mut *const (), write_tags: bool) -> Result<(), Error> -where W: Write + ?Sized { +unsafe fn send_value( + writer: &mut W, + tag: Tag, + data: &mut *const (), + write_tags: bool, +) -> Result<(), Error> +where + W: Write + ?Sized, +{ macro_rules! consume_value { ($ty:ty, | $ptr:ident | $map:expr) => {{ let $ptr = align_ptr::<$ty>(*data); @@ -337,7 +346,7 @@ pub fn send_args( tag_bytes: &[u8], data: *const *const (), write_tags: bool, -) -> Result<(), Error> +) -> Result<(), Error> where W: Write + ?Sized, { diff --git a/src/runtime/Cargo.toml.tpl b/src/runtime/Cargo.toml.tpl index e57e63e..240485c 100644 --- a/src/runtime/Cargo.toml.tpl +++ b/src/runtime/Cargo.toml.tpl @@ -20,7 +20,6 @@ num-derive = "0.3" cslice = "0.3" log = "0.4" embedded-hal = "0.2" -core_io = { version = "0.1", features = ["collections"] } crc = { version = "1.7", default-features = false } byteorder = { version = "1.3", default-features = false } void = { version = "1", default-features = false } @@ -46,4 +45,4 @@ libboard_artiq = { path = "../libboard_artiq" } [dependencies.tar-no-std] git = "https://git.m-labs.hk/M-Labs/tar-no-std" -rev = "2ab6dc5" \ No newline at end of file +rev = "2ab6dc5" diff --git a/src/runtime/src/comms.rs b/src/runtime/src/comms.rs index 1f6c773..7b0070e 100644 --- a/src/runtime/src/comms.rs +++ b/src/runtime/src/comms.rs @@ -1,12 +1,12 @@ use alloc::{collections::BTreeMap, rc::Rc, string::String, vec, vec::Vec}; use core::{cell::RefCell, fmt, slice, str}; -use core_io::Error as IoError; use cslice::CSlice; use dyld::elf; use futures::{future::FutureExt, select_biased}; #[cfg(has_drtio)] use io::Cursor; +use io::Error as IoError; #[cfg(has_drtio)] use ksupport::rpc; use ksupport::{kernel, resolve_channel_name}; @@ -78,8 +78,8 @@ impl From for Error { } } -impl From for Error { - fn from(_error: IoError) -> Self { +impl From> for Error { + fn from(_error: IoError) -> Self { Error::IoError } } diff --git a/src/satman/Cargo.toml.tpl b/src/satman/Cargo.toml.tpl index 38f64e6..654f5a9 100644 --- a/src/satman/Cargo.toml.tpl +++ b/src/satman/Cargo.toml.tpl @@ -16,7 +16,6 @@ build_zynq = { path = "../libbuild_zynq" } [dependencies] log = { version = "0.4", default-features = false } byteorder = { version = "1.3", default-features = false } -core_io = { version = "0.1", features = ["collections"] } crc = { version = "1.7", default-features = false } cslice = "0.3" embedded-hal = "0.2" diff --git a/src/satman/src/main.rs b/src/satman/src/main.rs index afff3fd..abf6fe7 100644 --- a/src/satman/src/main.rs +++ b/src/satman/src/main.rs @@ -5,7 +5,6 @@ #[macro_use] extern crate log; extern crate byteorder; -extern crate core_io; extern crate crc; extern crate cslice; extern crate embedded_hal; diff --git a/src/satman/src/subkernel.rs b/src/satman/src/subkernel.rs index a49e29a..93490eb 100644 --- a/src/satman/src/subkernel.rs +++ b/src/satman/src/subkernel.rs @@ -4,11 +4,10 @@ use alloc::{collections::BTreeMap, vec::Vec}; use core::{option::NoneError, slice, str}; -use core_io::{Error as IoError, Write}; use cslice::AsCSlice; use dma::{Error as DmaError, Manager as DmaManager}; -use io::{Cursor, ProtoWrite}; -use ksupport::{eh_artiq, kernel, rpc, rtio}; +use io::{Cursor, Error as IoError, ProtoWrite, Write}; +use ksupport::{eh_artiq, kernel, rpc}; use libboard_artiq::{drtio_routing::RoutingTable, drtioaux, drtioaux_proto::{PayloadStatus, MASTER_PAYLOAD_MAX_SIZE},