Skip to content

Commit

Permalink
Implement NixPath for Option<&T> where T: NixPath
Browse files Browse the repository at this point in the history
  • Loading branch information
kamalmarhubi committed Jan 5, 2016
1 parent 3154b7d commit 881d471
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ impl NixPath for PathBuf {
}
}

/// Treats `None` as an empty string.
impl<'a, NP: ?Sized + NixPath> NixPath for Option<&'a NP> {
fn len(&self) -> usize {
self.map_or(0, NixPath::len)
}

fn with_nix_path<T, F>(&self, f: F) -> Result<T> where F: FnOnce(&CStr) -> T {
if let Some(nix_path) = *self {
nix_path.with_nix_path(f)
} else {
// TODO(#229): avoid extra stack allocation from [u8] impl by
// replacing with empty CStr.
b"".with_nix_path(f)
}
}
}

#[inline]
pub fn from_ffi(res: libc::c_int) -> Result<()> {
if res != 0 {
Expand Down

0 comments on commit 881d471

Please sign in to comment.