diff --git a/util/src/io.rs b/util/src/io.rs new file mode 100644 index 0000000..7dde4b4 --- /dev/null +++ b/util/src/io.rs @@ -0,0 +1,51 @@ +use std::{borrow::Borrow, io}; + +pub trait IoErrorKind { + fn io_error_kind(&self) -> io::ErrorKind; +} + +impl> IoErrorKind for T { + fn io_error_kind(&self) -> io::ErrorKind { + self.borrow().kind() + } +} + +pub trait TryIoErrorKind { + fn try_io_error_kind(&self) -> Option; +} + +impl TryIoErrorKind for T { + fn try_io_error_kind(&self) -> Option { + Some(self.io_error_kind()) + } +} + +pub trait IoResultKindHintExt: Sized { + type Error; + fn io_err_kind_hint(self) -> Result; +} + +impl IoResultKindHintExt for Result { + type Error = E; + fn io_err_kind_hint(self) -> Result { + self.map_err(|e| { + let kind = e.borrow().io_error_kind(); + (e, kind) + }) + } +} + +pub trait TryIoResultKindHintExt: Sized { + type Error; + fn try_io_err_kind_hint(self) -> Result)>; +} + +impl TryIoResultKindHintExt for Result { + type Error = E; + fn try_io_err_kind_hint(self) -> Result)> { + self.map_err(|e| { + let opt_kind = e.borrow().try_io_error_kind(); + (e, opt_kind) + }) + } +} diff --git a/util/src/lib.rs b/util/src/lib.rs index 637de5d..f16e98f 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -4,6 +4,7 @@ pub mod b64; pub mod fd; pub mod file; pub mod functional; +pub mod io; pub mod mem; pub mod ord; pub mod result;