#![deny(missing_docs)]
use std::io;
pub trait IoResultExt<T>: sealed::Sealed + Sized {
fn optional(self) -> io::Result<Option<T>>;
fn can_exist(self) -> Self
where
T: Default;
}
impl<T> IoResultExt<T> for io::Result<T> {
fn optional(self) -> io::Result<Option<T>> {
match self {
Ok(t) => Ok(Some(t)),
Err(ref e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e),
}
}
fn can_exist(self) -> Self
where
T: Default,
{
match self {
Ok(t) => Ok(t),
Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok(T::default()),
Err(e) => Err(e),
}
}
}
mod sealed {
pub trait Sealed {}
impl<T> Sealed for ::std::io::Result<T> {}
}