From 29997564936115db19bd401df531b319c04842ab Mon Sep 17 00:00:00 2001 From: whitequark Date: Mon, 14 May 2018 12:49:36 +0000 Subject: [PATCH] firmware: impl std_artiq::io::{Read,Write} for io::{Read,Write}. --- artiq/firmware/Cargo.lock | 3 +++ artiq/firmware/libio/Cargo.toml | 1 + artiq/firmware/libio/lib.rs | 44 +++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/artiq/firmware/Cargo.lock b/artiq/firmware/Cargo.lock index 4d6a5a473..ef7c33ae5 100644 --- a/artiq/firmware/Cargo.lock +++ b/artiq/firmware/Cargo.lock @@ -130,6 +130,7 @@ name = "io" version = "0.0.0" dependencies = [ "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "std_artiq 0.0.0", ] [[package]] @@ -208,6 +209,7 @@ dependencies = [ "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "cslice 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "dyld 0.0.0", + "io 0.0.0", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "std_artiq 0.0.0", ] @@ -226,6 +228,7 @@ dependencies = [ "cslice 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "drtioaux 0.0.0", "fringe 1.1.0 (git+https://github.com/m-labs/libfringe?rev=bd23494)", + "io 0.0.0", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "logger_artiq 0.0.0", "managed 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/artiq/firmware/libio/Cargo.toml b/artiq/firmware/libio/Cargo.toml index 9729aca32..4f6ffd678 100644 --- a/artiq/firmware/libio/Cargo.toml +++ b/artiq/firmware/libio/Cargo.toml @@ -9,6 +9,7 @@ path = "lib.rs" [dependencies] byteorder = { version = "1.0", default-features = false, optional = true } +std_artiq = { path = "../libstd_artiq", optional = true } [features] alloc = [] diff --git a/artiq/firmware/libio/lib.rs b/artiq/firmware/libio/lib.rs index 5050c8934..f4a289cee 100644 --- a/artiq/firmware/libio/lib.rs +++ b/artiq/firmware/libio/lib.rs @@ -8,6 +8,9 @@ extern crate alloc; #[cfg(feature = "byteorder")] extern crate byteorder; +#[cfg(feature = "std_artiq")] +extern crate std_artiq; + use core::result; use core::fmt; @@ -185,3 +188,44 @@ impl> Write for Cursor { Ok(()) } } + +#[cfg(feature = "std_artiq")] +impl Read for T where T: std_artiq::io::Read { + type ReadError = std_artiq::io::Error; + + fn read(&mut self, buf: &mut [u8]) -> result::Result { + std_artiq::io::Read::read(self, buf) + } +} + +#[cfg(feature = "std_artiq")] +impl Write for T where T: std_artiq::io::Write { + type WriteError = std_artiq::io::Error; + type FlushError = std_artiq::io::Error; + + fn write(&mut self, buf: &[u8]) -> result::Result { + std_artiq::io::Write::write(self, buf) + } + + fn flush(&mut self) -> result::Result<(), Self::WriteError> { + std_artiq::io::Write::flush(self) + } +} + +#[cfg(feature = "std_artiq")] +impl From> for std_artiq::io::Error + where T: Into +{ + fn from(value: Error) -> std_artiq::io::Error { + match value { + Error::UnexpectedEof => + std_artiq::io::Error::new(std_artiq::io::ErrorKind::UnexpectedEof, + "unexpected end of stream"), + Error::Unrecognized => + std_artiq::io::Error::new(std_artiq::io::ErrorKind::InvalidData, + "unrecognized data"), + Error::Other(err) => + err.into() + } + } +}