summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorAlan Somers <asomers@gmail.com>2018-11-21 12:23:29 -0700
committerAlan Somers <asomers@gmail.com>2018-11-23 11:41:47 -0700
commit7db6fd46dc617824de12ad647a0f1bfbfbb3a799 (patch)
tree9ba8de9e783ec795bfcad1c53eb981faca4e503b /src/lib.rs
parent6920394524a558381e49a66a8dc11fe997fcce6c (diff)
downloadnix-7db6fd46dc617824de12ad647a0f1bfbfbb3a799.zip
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.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 48426594..ae3cc734 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<Errno> {
+ 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 {