summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorKamal Marhubi <kamal@marhubi.com>2016-01-01 12:09:58 -0500
committerKamal Marhubi <kamal@marhubi.com>2016-01-25 17:53:29 -0500
commit7dddd91a5796eddf4361569ee816924d30d2d7ed (patch)
treea083d091d44939321a3cbeb915f93c2c891b7e6a /src/lib.rs
parentcec5c6157b37c924b8a0139d5439fbc87d7becce (diff)
downloadnix-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.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 8714550b..512b5403 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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()