diff options
author | Kamal Marhubi <kamal@marhubi.com> | 2016-01-01 13:51:46 -0500 |
---|---|---|
committer | Kamal Marhubi <kamal@marhubi.com> | 2016-01-27 21:37:25 -0500 |
commit | 25ea210ef927c9cdb8fa1c9c6658a0659796e3f4 (patch) | |
tree | b3bc11ad68e77aef5a3eb5dd619c329ef1bdb089 /src | |
parent | 9f7c9fb25ba3173972e3c2a6ffa59431f26e1bc5 (diff) | |
download | nix-25ea210ef927c9cdb8fa1c9c6658a0659796e3f4.zip |
Implement NixPath for Option<&T> where T: NixPath
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 17 |
1 files changed, 17 insertions, 0 deletions
@@ -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 { |