summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Brinkmann <git@marcbrinkmann.de>2016-01-27 17:11:52 +0100
committerKamal Marhubi <kamal@marhubi.com>2016-01-27 15:10:42 -0500
commit263ddf878fb6fa83776c3b1da5599ffb675f8163 (patch)
treeea110587a86bb32bbe59c2e3c5339e8442ec8ef2
parent939e4aa77a1403be0c51f12cd2921fc9c52918ce (diff)
downloadnix-263ddf878fb6fa83776c3b1da5599ffb675f8163.zip
Changed Error type to nix::Error.
-rw-r--r--src/net/if_.rs45
1 files changed, 17 insertions, 28 deletions
diff --git a/src/net/if_.rs b/src/net/if_.rs
index 19f4e6fc..11370386 100644
--- a/src/net/if_.rs
+++ b/src/net/if_.rs
@@ -5,40 +5,29 @@
use libc::{c_uint, if_nametoindex};
use std::ffi::{CString, NulError};
-use std::io;
-
-/// Error that can occur during interface name resolution.
-#[derive(Debug)]
-pub enum NameToIndexError {
- /// Failed to allocate a C-style string to for the syscall
- NulError,
- IOError(io::Error),
-}
-
-impl From<NulError> for NameToIndexError {
- fn from(_: NulError) -> NameToIndexError {
- NameToIndexError::NulError
- }
-}
-
-impl From<io::Error> for NameToIndexError {
- fn from(e: io::Error) -> NameToIndexError {
- NameToIndexError::IOError(e)
- }
-}
+use ::Error;
/// Resolve an interface into a interface number.
-pub fn name_to_index(name: &str) -> Result<c_uint, NameToIndexError> {
- let name = try!(CString::new(name));
+pub fn name_to_index(name: &str) -> Result<c_uint, Error> {
+ let name = match CString::new(name) {
+ Err(e) => match e { NulError(..) => {
+ // A NulError indicates that a '\0' was found inside the string,
+ // making it impossible to create valid C-String. To avoid having
+ // to create a new error type for this rather rare case,
+ // nix::Error's invalid_argument() constructor is used.
+ //
+ // We match the NulError individually here to ensure to avoid
+ // accidentally returning invalid_argument() for errors other than
+ // NulError (which currently don't exist).
+ return Err(Error::invalid_argument());
+ }},
+ Ok(s) => s
+ };
let if_index;
unsafe {
if_index = if_nametoindex(name.as_ptr());
}
- if if_index == 0 {
- return Err(NameToIndexError::from(io::Error::last_os_error()));
- }
-
- Ok(if_index)
+ if if_index == 0 { Err(Error::last()) } else { Ok(if_index) }
}