Skip to content

Commit

Permalink
Add Error::as_errno
Browse files Browse the repository at this point in the history
This method is useful when it can be statically determined that a
nix::Error be an errno, which I find to be very common.
  • Loading branch information
asomers committed Nov 21, 2018
1 parent 3f9548a commit 83ff69e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -36,6 +36,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#952](https://github.com/nix-rust/nix/pull/952))
- Added the `time_t` and `suseconds_t` public aliases within `sys::time`.
([#968](https://github.com/nix-rust/nix/pull/968))
- Added `Error::as_errno`.
([#975](https://github.com/nix-rust/nix/pull/975))

### Changed
- Increased required Rust version to 1.24.1
Expand Down
17 changes: 17 additions & 0 deletions src/lib.rs
Expand Up @@ -110,6 +110,23 @@ pub enum Error {
}

impl Error {
/// Convert this `Error` to an [`Errno`](enum.Errno.html).
///
/// # Example
///
/// ```
/// # use nix::Error;
/// # use nix::errno::Errno;
/// let e = Error::from(Errno::EPERM);
/// assert_eq!(Some(Errno::EPERM), e.as_errno());
/// ```
pub fn as_errno(&self) -> Option<Errno> {
if let Error::Sys(e) = self {
Some(*e)
} else {
None
}
}

/// Create a nix Error from a given errno
pub fn from_errno(errno: Errno) -> Error {
Expand Down

0 comments on commit 83ff69e

Please sign in to comment.