forked from M-Labs/artiq
1
0
Fork 0

firmware: impl std_artiq::io::{Read,Write} for io::{Read,Write}.

This commit is contained in:
whitequark 2018-05-14 12:49:36 +00:00
parent bfc8fd0807
commit 2999756493
3 changed files with 48 additions and 0 deletions

View File

@ -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)",

View File

@ -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 = []

View File

@ -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<T: ::alloc::Vec<[u8]>> Write for Cursor<T> {
Ok(())
}
}
#[cfg(feature = "std_artiq")]
impl<T> Read for T where T: std_artiq::io::Read {
type ReadError = std_artiq::io::Error;
fn read(&mut self, buf: &mut [u8]) -> result::Result<usize, Self::ReadError> {
std_artiq::io::Read::read(self, buf)
}
}
#[cfg(feature = "std_artiq")]
impl<T> 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<usize, Self::WriteError> {
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<T> From<Error<T>> for std_artiq::io::Error
where T: Into<std_artiq::io::Error>
{
fn from(value: Error<T>) -> 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()
}
}
}