From e8e49ef6bbf5595fb23ae1aacdbe149b79c77d19 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Wed, 21 Nov 2018 12:23:29 -0700 Subject: [PATCH] Add Error::as_errno This method is useful when it can be statically determined that a nix::Error be an errno, which I find to be very common. --- CHANGELOG.md | 2 ++ src/lib.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dd7a4b9d2..40dae36889 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/lib.rs b/src/lib.rs index 484265942b..ae3cc73413 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { + if let &Error::Sys(ref e) = self { + Some(*e) + } else { + None + } + } /// Create a nix Error from a given errno pub fn from_errno(errno: Errno) -> Error {