diff options
author | Alan Somers <asomers@gmail.com> | 2018-11-21 12:23:29 -0700 |
---|---|---|
committer | Alan Somers <asomers@gmail.com> | 2018-11-23 11:41:47 -0700 |
commit | 7db6fd46dc617824de12ad647a0f1bfbfbb3a799 (patch) | |
tree | 9ba8de9e783ec795bfcad1c53eb981faca4e503b /src/lib.rs | |
parent | 6920394524a558381e49a66a8dc11fe997fcce6c (diff) | |
download | nix-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.rs | 17 |
1 files changed, 17 insertions, 0 deletions
@@ -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 { |