Impl Write and Read for std::io::{Write, Read}

This commit is contained in:
Sergey Pepyakin 2018-05-14 15:16:38 +03:00
parent 85261e9730
commit d1de2cfb61
2 changed files with 34 additions and 27 deletions

View File

@ -255,33 +255,23 @@ pub fn serialize<T: Serialize>(val: T) -> Result<Vec<u8>, T::Error> {
Ok(buf) Ok(buf)
} }
/// Deserialize module from file. /// Deserialize module from the file.
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub fn deserialize_file<P: AsRef<::std::path::Path>>(p: P) -> Result<Module, Error> { pub fn deserialize_file<P: AsRef<::std::path::Path>>(p: P) -> Result<Module, Error> {
use std::io::Read; let mut f = ::std::fs::File::open(p)
let mut contents = Vec::new();
::std::fs::File::open(p)
.and_then(|mut f| f.read_to_end(&mut contents))
.map_err(|e| Error::HeapOther(format!("Can't read from the file: {:?}", e)))?; .map_err(|e| Error::HeapOther(format!("Can't read from the file: {:?}", e)))?;
deserialize_buffer(&contents) Module::deserialize(&mut f)
} }
/// Serialize module to the file /// Serialize module to the file
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub fn serialize_to_file<P: AsRef<::std::path::Path>>(p: P, module: Module) -> Result<(), Error> { pub fn serialize_to_file<P: AsRef<::std::path::Path>>(p: P, module: Module) -> Result<(), Error> {
use std::io::Write;
let mut io = ::std::fs::File::create(p) let mut io = ::std::fs::File::create(p)
.map_err(|e| Error::HeapOther(format!("Can't create the file: {:?}", e)))?; .map_err(|e|
let mut buf = Vec::new(); Error::HeapOther(format!("Can't create the file: {:?}", e))
)?;
module.serialize(&mut buf)?;
io.write_all(&buf)
.map_err(|e| Error::HeapOther(format!("Can't write to the file: {:?}", e)))?;
module.serialize(&mut io)?;
Ok(()) Ok(())
} }

View File

@ -3,10 +3,8 @@
//! Basically it just a replacement for the std::io that is usable from //! Basically it just a replacement for the std::io that is usable from
//! the `no_std` environment. //! the `no_std` environment.
use std::vec::Vec;
/// IO specific error. /// IO specific error.
#[derive(Debug, PartialEq, Eq)] #[derive(Debug)]
pub enum Error { pub enum Error {
/// Some unexpected data left in the buffer after reading all data. /// Some unexpected data left in the buffer after reading all data.
TrailingData, TrailingData,
@ -16,6 +14,9 @@ pub enum Error {
/// Invalid data is encountered. /// Invalid data is encountered.
InvalidData, InvalidData,
#[cfg(feature = "std")]
IoError(::std::io::Error),
} }
/// IO specific Result. /// IO specific Result.
@ -35,13 +36,6 @@ pub trait Read {
fn read(&mut self, buf: &mut [u8]) -> Result<()>; fn read(&mut self, buf: &mut [u8]) -> Result<()>;
} }
impl Write for Vec<u8> {
fn write(&mut self, buf: &[u8]) -> Result<()> {
self.extend(buf);
Ok(())
}
}
/// Reader that saves the last position. /// Reader that saves the last position.
pub struct Cursor<T> { pub struct Cursor<T> {
inner: T, inner: T,
@ -75,6 +69,29 @@ impl<T: AsRef<[u8]>> Read for Cursor<T> {
} }
} }
#[cfg(not(feature = "std"))]
impl Write for ::std::vec::Vec<u8> {
fn write(&mut self, buf: &[u8]) -> Result<()> {
self.extend(buf);
Ok(())
}
}
#[cfg(feature = "std")]
impl<T: ::std::io::Read> Read for T {
fn read(&mut self, buf: &mut [u8]) -> Result<()> {
self.read_exact(buf)
.map_err(Error::IoError)
}
}
#[cfg(feature = "std")]
impl<T: ::std::io::Write> Write for T {
fn write(&mut self, buf: &[u8]) -> Result<()> {
self.write_all(buf).map_err(Error::IoError)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;