Skip to content

Commit

Permalink
WriteAllError is going away in eio 0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Sep 25, 2023
1 parent 414bccb commit 5ce040a
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions src/utils/io.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::io::{Read, Write, WriteAllError};
use embedded_io::Error;

use crate::io::{Read, Write};

pub fn try_read_full<R: Read>(mut read: R, buf: &mut [u8]) -> Result<usize, (R::Error, usize)> {
let mut offset = 0;
Expand All @@ -23,7 +25,6 @@ pub fn try_read_full<R: Read>(mut read: R, buf: &mut [u8]) -> Result<usize, (R::
pub enum CopyError<R, W> {
Read(R),
Write(W),
WriteAll(WriteAllError<W>),
}

impl<R: core::fmt::Debug, W: core::fmt::Debug> core::fmt::Display for CopyError<R, W> {
Expand All @@ -36,19 +37,18 @@ impl<R: core::fmt::Debug, W: core::fmt::Debug> core::fmt::Display for CopyError<
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<R: core::fmt::Debug, W: core::fmt::Debug> std::error::Error for CopyError<R, W> {}

// impl<R, W> Error for CopyError<R, W>
// where
// R: Error,
// W: Error,
// {
// fn kind(&self) -> embedded_io::ErrorKind {
// match self {
// Self::Read(e) => e.kind(),
// Self::Write(e) => e.kind(),
// Self::WriteAll(e) => e.kind(),
// }
// }
// }
impl<R, W> Error for CopyError<R, W>
where
R: Error,
W: Error,
{
fn kind(&self) -> embedded_io::ErrorKind {
match self {
Self::Read(e) => e.kind(),
Self::Write(e) => e.kind(),
}
}
}

pub fn copy<R, W>(read: R, write: W, buf: &mut [u8]) -> Result<u64, CopyError<R::Error, W::Error>>
where
Expand Down Expand Up @@ -95,7 +95,8 @@ where

write
.write_all(&buf[0..size_read])
.map_err(CopyError::WriteAll)?;
.map_err(map_write_err)
.map_err(CopyError::Write)?;

copied += size_read as u64;
len -= size_read as u64;
Expand All @@ -106,10 +107,19 @@ where
Ok(copied)
}

pub(crate) fn map_write_err<W>(e: embedded_io::WriteAllError<W>) -> W {
match e {
embedded_io::WriteAllError::WriteZero => panic!("write() returned Ok(0)"),
embedded_io::WriteAllError::Other(e) => e,
}
}

#[cfg(feature = "nightly")]
pub mod asynch {
use crate::io::asynch::{Read, Write};

use super::map_write_err;

pub use super::CopyError;

pub async fn try_read_full<R: Read>(
Expand Down Expand Up @@ -183,7 +193,8 @@ pub mod asynch {
write
.write_all(&buf[0..size_read])
.await
.map_err(CopyError::WriteAll)?;
.map_err(map_write_err)
.map_err(CopyError::Write)?;

copied += size_read as u64;
len -= size_read as u64;
Expand Down

0 comments on commit 5ce040a

Please sign in to comment.