diff options
author | Kamal Marhubi <kamal@marhubi.com> | 2016-01-01 12:09:58 -0500 |
---|---|---|
committer | Kamal Marhubi <kamal@marhubi.com> | 2016-01-25 17:53:29 -0500 |
commit | 7dddd91a5796eddf4361569ee816924d30d2d7ed (patch) | |
tree | a083d091d44939321a3cbeb915f93c2c891b7e6a /src/lib.rs | |
parent | cec5c6157b37c924b8a0139d5439fbc87d7becce (diff) | |
download | nix-7dddd91a5796eddf4361569ee816924d30d2d7ed.zip |
Implement NixPath for CStr
This makes NixPath more versatile while waiting to see if there will be
a NixPath overhaul.
Refs https://github.com/carllerche/nix-rust/issues/221
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 16 |
1 files changed, 16 insertions, 0 deletions
@@ -123,6 +123,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() |