diff --git a/src/lib.rs b/src/lib.rs index 826ed838c3..d1e3019906 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(&self, f: F) -> Result 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 {