From a880a9dba404afc9243685ad8ed7ac664975b4ae Mon Sep 17 00:00:00 2001 From: Kamal Marhubi Date: Wed, 27 Jan 2016 15:49:40 -0500 Subject: [PATCH] Implement NixPath for str and OsStr This is a stop gap improvement until the NixPath reform is figured out. refs #221 --- src/lib.rs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 512b5403e5..694d4a11b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,7 +48,7 @@ pub mod unistd; use libc::c_char; use std::{ptr, result}; -use std::ffi::CStr; +use std::ffi::{CStr, OsStr}; use std::path::{Path, PathBuf}; use std::os::unix::ffi::OsStrExt; use std::io; @@ -123,6 +123,28 @@ pub trait NixPath { where F: FnOnce(&CStr) -> T; } +impl NixPath for str { + fn len(&self) -> usize { + NixPath::len(OsStr::new(self)) + } + + fn with_nix_path(&self, f:F) -> Result + where F: FnOnce(&CStr) -> T { + OsStr::new(self).with_nix_path(f) + } +} + +impl NixPath for OsStr { + fn len(&self) -> usize { + self.as_bytes().len() + } + + fn with_nix_path(&self, f:F) -> Result + where F: FnOnce(&CStr) -> T { + self.as_bytes().with_nix_path(f) + } +} + impl NixPath for CStr { fn len(&self) -> usize { self.to_bytes().len() @@ -168,21 +190,21 @@ impl NixPath for [u8] { impl NixPath for Path { fn len(&self) -> usize { - self.as_os_str().as_bytes().len() + NixPath::len(self.as_os_str()) } fn with_nix_path(&self, f: F) -> Result where F: FnOnce(&CStr) -> T { - self.as_os_str().as_bytes().with_nix_path(f) + self.as_os_str().with_nix_path(f) } } impl NixPath for PathBuf { fn len(&self) -> usize { - self.as_os_str().as_bytes().len() + NixPath::len(self.as_os_str()) } fn with_nix_path(&self, f: F) -> Result where F: FnOnce(&CStr) -> T { - self.as_os_str().as_bytes().with_nix_path(f) + self.as_os_str().with_nix_path(f) } }