Skip to content

Commit 5c535c3

Browse files
committed
Implement NixPath for CStr
This makes NixPath more versatile while waiting to see if there will be a NixPath overhaul. Refs nix-rust#221
1 parent 3154b7d commit 5c535c3

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/lib.rs

+16
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,22 @@ pub trait NixPath {
119119
where F: FnOnce(&CStr) -> T;
120120
}
121121

122+
impl NixPath for CStr {
123+
fn len(&self) -> usize {
124+
self.to_bytes().len()
125+
}
126+
127+
fn with_nix_path<T, F>(&self, f: F) -> Result<T>
128+
where F: FnOnce(&CStr) -> T {
129+
// Equivalence with the [u8] impl.
130+
if self.len() >= PATH_MAX as usize {
131+
return Err(Error::InvalidPath);
132+
}
133+
134+
Ok(f(self))
135+
}
136+
}
137+
122138
impl NixPath for [u8] {
123139
fn len(&self) -> usize {
124140
self.len()

0 commit comments

Comments
 (0)