From 7db6fd46dc617824de12ad647a0f1bfbfbb3a799 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Wed, 21 Nov 2018 12:23:29 -0700 Subject: 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. --- src/lib.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/lib.rs') 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 { + 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 { -- cgit v1.2.3