Skip to content

Commit

Permalink
Implement NixPath for str and OsStr
Browse files Browse the repository at this point in the history
This is a stop gap improvement until the NixPath reform is figured out.

refs nix-rust#221
  • Loading branch information
kamalmarhubi committed Jan 27, 2016
1 parent e8f014d commit a880a9d
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T, F>(&self, f:F) -> Result<T>
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<T, F>(&self, f:F) -> Result<T>
where F: FnOnce(&CStr) -> T {
self.as_bytes().with_nix_path(f)
}
}

impl NixPath for CStr {
fn len(&self) -> usize {
self.to_bytes().len()
Expand Down Expand Up @@ -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<T, F>(&self, f: F) -> Result<T> 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<T, F>(&self, f: F) -> Result<T> where F: FnOnce(&CStr) -> T {
self.as_os_str().as_bytes().with_nix_path(f)
self.as_os_str().with_nix_path(f)
}
}

Expand Down

0 comments on commit a880a9d

Please sign in to comment.