summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKamal Marhubi <kamal@marhubi.com>2016-01-27 16:15:30 -0500
committerKamal Marhubi <kamal@marhubi.com>2016-01-27 16:15:30 -0500
commit75d038b5ce75c054fae110fe0c9c02bcbcd1567d (patch)
tree49273615c6bb13326eeb1fda959977ef36e72a17 /src
parentd391523c0ec4bda1d3940255f3a52770906371d0 (diff)
downloadnix-75d038b5ce75c054fae110fe0c9c02bcbcd1567d.zip
Use NixPath for if_nametoindex
Diffstat (limited to 'src')
-rw-r--r--src/net/if_.rs29
1 files changed, 7 insertions, 22 deletions
diff --git a/src/net/if_.rs b/src/net/if_.rs
index d62af4fa..7edfc998 100644
--- a/src/net/if_.rs
+++ b/src/net/if_.rs
@@ -5,30 +5,15 @@
use libc;
use libc::c_uint;
-use std::ffi::{CString, NulError};
-use ::{Result, Error};
+use {Result, Error, NixPath};
/// Resolve an interface into a interface number.
-pub fn if_nametoindex(name: &str) -> Result<c_uint> {
- 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
- };
+pub fn if_nametoindex<P: ?Sized + NixPath>(name: &P) -> Result<c_uint> {
+ let if_index = try!(name.with_nix_path(|name| unsafe { libc::if_nametoindex(name.as_ptr()) }));
- let if_index;
- unsafe {
- if_index = libc::if_nametoindex(name.as_ptr());
+ if if_index == 0 {
+ Err(Error::last())
+ } else {
+ Ok(if_index)
}
-
- if if_index == 0 { Err(Error::last()) } else { Ok(if_index) }
}