diff options
author | Jeremy Fitzhardinge <jeremy@goop.org> | 2015-10-10 13:08:49 -0700 |
---|---|---|
committer | Jeremy Fitzhardinge <jeremy@goop.org> | 2015-10-27 00:05:44 -0700 |
commit | 0cfa2a10a23712d44b4c745f4664fa3b46b9e943 (patch) | |
tree | ebcac282ba62dc9176ef55767983f862351a9031 | |
parent | ad63c14489372506b67e976ae2cb28164d75a8b0 (diff) | |
download | nix-0cfa2a10a23712d44b4c745f4664fa3b46b9e943.zip |
Improve Error interoperability with std
- Add From implementaion for io::Error, so nix::Error can be turned into
a std::io::Error.
- Add From from Errno - a little more idiomatic than from_errno these days
- Implement std::error::Error for nix::Error
-rw-r--r-- | src/lib.rs | 34 |
1 files changed, 34 insertions, 0 deletions
@@ -48,6 +48,9 @@ use std::{ptr, result}; use std::ffi::CStr; use std::path::{Path, PathBuf}; use std::os::unix::ffi::OsStrExt; +use std::io; +use std::fmt; +use std::error; pub type Result<T> = result::Result<T, Error>; @@ -78,6 +81,37 @@ impl Error { } } +impl From<errno::Errno> for Error { + fn from(errno: errno::Errno) -> Error { Error::from_errno(errno) } +} + +impl error::Error for Error { + fn description(&self) -> &str { + match self { + &Error::InvalidPath => "Invalid path", + &Error::Sys(ref errno) => errno.desc(), + } + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + &Error::InvalidPath => write!(f, "Invalid path"), + &Error::Sys(errno) => write!(f, "{:?}: {}", errno, errno.desc()), + } + } +} + +impl From<Error> for io::Error { + fn from(err: Error) -> Self { + match err { + Error::InvalidPath => io::Error::new(io::ErrorKind::InvalidInput, err), + Error::Sys(errno) => io::Error::from_raw_os_error(errno as i32), + } + } +} + pub trait NixPath { fn len(&self) -> usize; |