From 982fb3227ad438e8c7a16ac75ae6f296df2c8bd9 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 4 May 2020 22:33:14 +0200 Subject: Initial working version --- src/error.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/error.rs (limited to 'src/error.rs') diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..f25bc5d --- /dev/null +++ b/src/error.rs @@ -0,0 +1,27 @@ +use std::io; + +pub trait IntoIOError { + fn ioerr(self) -> io::Error; + fn perror(self, parent: io::Error) -> io::Error; +} + +// This impl bound is taken directly from the io::Error::new function. +impl>> IntoIOError for E { + fn ioerr(self) -> io::Error { + io::Error::new(io::ErrorKind::Other, self) + } + + fn perror(self, parent: io::Error) -> io::Error { + io::Error::new(parent.kind(), format!("{}: {}", self.into(), parent)) + } +} + +pub trait IntoIOResult { + fn iores(self) -> io::Result; +} + +impl IntoIOResult for Result { + fn iores(self) -> io::Result { + self.map_err(|e| e.ioerr()) + } +} -- cgit v1.2.3