mirror of
https://github.com/rosenpass/rosenpass.git
synced 2026-01-17 07:23:53 -08:00
feat: Convenience traits to get the ErrorKind of an io error for match clauses
This commit is contained in:
51
util/src/io.rs
Normal file
51
util/src/io.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use std::{borrow::Borrow, io};
|
||||
|
||||
pub trait IoErrorKind {
|
||||
fn io_error_kind(&self) -> io::ErrorKind;
|
||||
}
|
||||
|
||||
impl<T: Borrow<io::Error>> IoErrorKind for T {
|
||||
fn io_error_kind(&self) -> io::ErrorKind {
|
||||
self.borrow().kind()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TryIoErrorKind {
|
||||
fn try_io_error_kind(&self) -> Option<io::ErrorKind>;
|
||||
}
|
||||
|
||||
impl<T: IoErrorKind> TryIoErrorKind for T {
|
||||
fn try_io_error_kind(&self) -> Option<io::ErrorKind> {
|
||||
Some(self.io_error_kind())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait IoResultKindHintExt<T>: Sized {
|
||||
type Error;
|
||||
fn io_err_kind_hint(self) -> Result<T, (Self::Error, io::ErrorKind)>;
|
||||
}
|
||||
|
||||
impl<T, E: IoErrorKind> IoResultKindHintExt<T> for Result<T, E> {
|
||||
type Error = E;
|
||||
fn io_err_kind_hint(self) -> Result<T, (E, io::ErrorKind)> {
|
||||
self.map_err(|e| {
|
||||
let kind = e.borrow().io_error_kind();
|
||||
(e, kind)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TryIoResultKindHintExt<T>: Sized {
|
||||
type Error;
|
||||
fn try_io_err_kind_hint(self) -> Result<T, (Self::Error, Option<io::ErrorKind>)>;
|
||||
}
|
||||
|
||||
impl<T, E: TryIoErrorKind> TryIoResultKindHintExt<T> for Result<T, E> {
|
||||
type Error = E;
|
||||
fn try_io_err_kind_hint(self) -> Result<T, (E, Option<io::ErrorKind>)> {
|
||||
self.map_err(|e| {
|
||||
let opt_kind = e.borrow().try_io_error_kind();
|
||||
(e, opt_kind)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user