Skip to content

Commit

Permalink
Implement NixPath for CStr
Browse files Browse the repository at this point in the history
This makes NixPath more versatile while waiting to see if there will be
a NixPath overhaul.

Refs nix-rust#221
  • Loading branch information
kamalmarhubi committed Jan 1, 2016
1 parent 3154b7d commit 5c535c3
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ pub trait NixPath {
where F: FnOnce(&CStr) -> T;
}

impl NixPath for CStr {
fn len(&self) -> usize {
self.to_bytes().len()
}

fn with_nix_path<T, F>(&self, f: F) -> Result<T>
where F: FnOnce(&CStr) -> T {
// Equivalence with the [u8] impl.
if self.len() >= PATH_MAX as usize {
return Err(Error::InvalidPath);
}

Ok(f(self))
}
}

impl NixPath for [u8] {
fn len(&self) -> usize {
self.len()
Expand Down

0 comments on commit 5c535c3

Please sign in to comment.