summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKamal Marhubi <kamal@marhubi.com>2016-01-01 13:51:46 -0500
committerKamal Marhubi <kamal@marhubi.com>2016-01-27 21:37:25 -0500
commit25ea210ef927c9cdb8fa1c9c6658a0659796e3f4 (patch)
treeb3bc11ad68e77aef5a3eb5dd619c329ef1bdb089 /src
parent9f7c9fb25ba3173972e3c2a6ffa59431f26e1bc5 (diff)
downloadnix-25ea210ef927c9cdb8fa1c9c6658a0659796e3f4.zip
Implement NixPath for Option<&T> where T: NixPath
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index cad5ca18..94f15799 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -210,6 +210,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 {