From 5ce040afa3ed94b817d071048c3f1817eabbf46b Mon Sep 17 00:00:00 2001 From: ivmarkov Date: Mon, 25 Sep 2023 05:30:42 +0000 Subject: [PATCH] WriteAllError is going away in eio 0.6 --- src/utils/io.rs | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/src/utils/io.rs b/src/utils/io.rs index f0289cc..b120d6f 100644 --- a/src/utils/io.rs +++ b/src/utils/io.rs @@ -1,4 +1,6 @@ -use crate::io::{Read, Write, WriteAllError}; +use embedded_io::Error; + +use crate::io::{Read, Write}; pub fn try_read_full(mut read: R, buf: &mut [u8]) -> Result { let mut offset = 0; @@ -23,7 +25,6 @@ pub fn try_read_full(mut read: R, buf: &mut [u8]) -> Result { Read(R), Write(W), - WriteAll(WriteAllError), } impl core::fmt::Display for CopyError { @@ -36,19 +37,18 @@ impl core::fmt::Display for CopyError< #[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl std::error::Error for CopyError {} -// impl Error for CopyError -// 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 Error for CopyError +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(read: R, write: W, buf: &mut [u8]) -> Result> where @@ -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; @@ -106,10 +107,19 @@ where Ok(copied) } +pub(crate) fn map_write_err(e: embedded_io::WriteAllError) -> 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( @@ -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;