Skip to content

Commit 7dddd91

Browse files
committed
Implement NixPath for CStr
This makes NixPath more versatile while waiting to see if there will be a NixPath overhaul. Refs #221
1 parent cec5c61 commit 7dddd91

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
@@ -123,6 +123,22 @@ pub trait NixPath {
123123
where F: FnOnce(&CStr) -> T;
124124
}
125125

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

0 commit comments

Comments
 (0)