summary refs log tree commit diff
diff options
context:
space:
mode:
authorZach Reizner <zachr@google.com>2017-05-19 15:42:36 -0700
committerchrome-bot <chrome-bot@chromium.org>2017-05-25 17:16:02 -0700
commit37285dc09d8fe5988fb98dc20bf00fdda38b0843 (patch)
tree06042049bff44c474470b589fa90e180a99634e0
parentd6c579fcef98a4de7221a814dfece1c4f7430c71 (diff)
downloadcrosvm-37285dc09d8fe5988fb98dc20bf00fdda38b0843.tar
crosvm-37285dc09d8fe5988fb98dc20bf00fdda38b0843.tar.gz
crosvm-37285dc09d8fe5988fb98dc20bf00fdda38b0843.tar.bz2
crosvm-37285dc09d8fe5988fb98dc20bf00fdda38b0843.tar.lz
crosvm-37285dc09d8fe5988fb98dc20bf00fdda38b0843.tar.xz
crosvm-37285dc09d8fe5988fb98dc20bf00fdda38b0843.tar.zst
crosvm-37285dc09d8fe5988fb98dc20bf00fdda38b0843.zip
sys_util: Add conversion from errno io::Error.
Change-Id: Ia49aa8eac1dedbc4e3f6277120bf332404e8b818
Reviewed-on: https://chromium-review.googlesource.com/509918
Commit-Ready: Dylan Reid <dgreid@chromium.org>
Tested-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
-rw-r--r--sys_util/src/errno.rs11
1 files changed, 9 insertions, 2 deletions
diff --git a/sys_util/src/errno.rs b/sys_util/src/errno.rs
index efabb8f..e0c13b1 100644
--- a/sys_util/src/errno.rs
+++ b/sys_util/src/errno.rs
@@ -3,11 +3,12 @@
 // found in the LICENSE file.
 
 use std::result;
+use std::io;
 
 use libc::__errno_location;
 
-/// An error number, retrieved from [errno](http://man7.org/linux/man-pages/man3/errno.3.html), set
-/// by a libc function that returned an error.
+/// An error number, retrieved from errno (man 3 errno), set by a libc
+/// function that returned an error.
 #[derive(Clone, Copy, Debug, PartialEq)]
 pub struct Error(i32);
 pub type Result<T> = result::Result<T, Error>;
@@ -32,6 +33,12 @@ impl Error {
     }
 }
 
+impl From<io::Error> for Error {
+    fn from(e: io::Error) -> Self {
+        Error::new(e.raw_os_error().unwrap_or_default())
+    }
+}
+
 /// Returns the last errno as a Result that is always an error.
 pub fn errno_result<T>() -> Result<T> {
     Err(Error::last())