Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
feat(io): add connections with std
Browse files Browse the repository at this point in the history
  • Loading branch information
RatCornu committed Jan 3, 2024
1 parent 76fea3b commit aadfe4d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/dev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ impl_device!(&mut [T]);
impl_device!(Vec<T>);
impl_device!(Box<[T]>);

#[cfg(not(no_std))]
#[cfg(feature = "std")]
impl<E: core::error::Error> Device<u8, E> for RefCell<File> {
#[inline]
fn size(&self) -> Size {
Expand Down
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub enum Error<E: core::error::Error> {

/// Path error
Path(PathError),

/// Standard I/O error
#[cfg(feature = "std")]
IO(std::io::Error),
}

impl<E: core::error::Error> Display for Error<E> {
Expand All @@ -28,6 +32,8 @@ impl<E: core::error::Error> Display for Error<E> {
Self::Device(device_error) => write!(formatter, "Device Error: {device_error}"),
Self::Fs(fs_error) => write!(formatter, "Filesystem Error: {fs_error}"),
Self::Path(path_error) => write!(formatter, "Path Error: {path_error}"),
#[cfg(feature = "std")]
Self::IO(io_error) => write!(formatter, "I/O Error: {io_error}"),
}
}
}
Expand Down
41 changes: 34 additions & 7 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ pub trait Read<E: core::error::Error> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error<E>>;
}

impl<E: core::error::Error, S: Read<E>> Read<E> for &mut S {
#[cfg(feature = "std")]
impl<E: core::error::Error, S: std::io::Read> Read<E> for S {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error<E>> {
(**self).read(buf)
std::io::Read::read(self, buf).map_err(|err| Error::IO(err))
}
}

Expand Down Expand Up @@ -54,15 +55,16 @@ pub trait Write<E: core::error::Error> {
fn flush(&mut self) -> Result<(), Error<E>>;
}

impl<E: core::error::Error, S: Write<E>> Write<E> for &mut S {
#[cfg(feature = "std")]
impl<E: core::error::Error, S: std::io::Write> Write<E> for S {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize, Error<E>> {
(**self).write(buf)
std::io::Write::write(self, buf).map_err(|err| Error::IO(err))
}

#[inline]
fn flush(&mut self) -> Result<(), Error<E>> {
(**self).flush()
std::io::Write::flush(self).map_err(|err| Error::IO(err))
}
}

Expand All @@ -85,6 +87,30 @@ pub enum SeekFrom {
Current(i64),
}

#[cfg(feature = "std")]
impl From<std::io::SeekFrom> for SeekFrom {
#[inline]
fn from(value: std::io::SeekFrom) -> Self {
match value {
std::io::SeekFrom::Start(value) => Self::Start(value),
std::io::SeekFrom::End(value) => Self::End(value),
std::io::SeekFrom::Current(value) => Self::Current(value),
}
}
}

#[cfg(feature = "std")]
impl From<SeekFrom> for std::io::SeekFrom {
#[inline]
fn from(value: SeekFrom) -> Self {
match value {
SeekFrom::Start(value) => Self::Start(value),
SeekFrom::End(value) => Self::End(value),
SeekFrom::Current(value) => Self::Current(value),
}
}
}

/// Provides a cursor which can be moved within a stream of bytes.
///
/// See [`std::io::Seek`] for more information: this trait is a `no_std` based variant.
Expand All @@ -99,9 +125,10 @@ pub trait Seek<E: core::error::Error> {
fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error<E>>;
}

impl<E: core::error::Error, S: Seek<E>> Seek<E> for &mut S {
#[cfg(feature = "std")]
impl<E: core::error::Error, S: std::io::Seek> Seek<E> for S {
#[inline]
fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error<E>> {
(**self).seek(pos)
std::io::Seek::seek(self, pos.into()).map_err(|err| Error::IO(err))
}
}

0 comments on commit aadfe4d

Please sign in to comment.