@@ -48,7 +48,7 @@ pub mod unistd;
48
48
49
49
use libc:: c_char;
50
50
use std:: { ptr, result} ;
51
- use std:: ffi:: CStr ;
51
+ use std:: ffi:: { CStr , OsStr } ;
52
52
use std:: path:: { Path , PathBuf } ;
53
53
use std:: os:: unix:: ffi:: OsStrExt ;
54
54
use std:: io;
@@ -123,6 +123,28 @@ pub trait NixPath {
123
123
where F : FnOnce ( & CStr ) -> T ;
124
124
}
125
125
126
+ impl NixPath for str {
127
+ fn len ( & self ) -> usize {
128
+ NixPath :: len ( OsStr :: new ( self ) )
129
+ }
130
+
131
+ fn with_nix_path < T , F > ( & self , f : F ) -> Result < T >
132
+ where F : FnOnce ( & CStr ) -> T {
133
+ OsStr :: new ( self ) . with_nix_path ( f)
134
+ }
135
+ }
136
+
137
+ impl NixPath for OsStr {
138
+ fn len ( & self ) -> usize {
139
+ self . as_bytes ( ) . len ( )
140
+ }
141
+
142
+ fn with_nix_path < T , F > ( & self , f : F ) -> Result < T >
143
+ where F : FnOnce ( & CStr ) -> T {
144
+ self . as_bytes ( ) . with_nix_path ( f)
145
+ }
146
+ }
147
+
126
148
impl NixPath for CStr {
127
149
fn len ( & self ) -> usize {
128
150
self . to_bytes ( ) . len ( )
@@ -168,21 +190,21 @@ impl NixPath for [u8] {
168
190
169
191
impl NixPath for Path {
170
192
fn len ( & self ) -> usize {
171
- self . as_os_str ( ) . as_bytes ( ) . len ( )
193
+ NixPath :: len ( self . as_os_str ( ) )
172
194
}
173
195
174
196
fn with_nix_path < T , F > ( & self , f : F ) -> Result < T > where F : FnOnce ( & CStr ) -> T {
175
- self . as_os_str ( ) . as_bytes ( ) . with_nix_path ( f)
197
+ self . as_os_str ( ) . with_nix_path ( f)
176
198
}
177
199
}
178
200
179
201
impl NixPath for PathBuf {
180
202
fn len ( & self ) -> usize {
181
- self . as_os_str ( ) . as_bytes ( ) . len ( )
203
+ NixPath :: len ( self . as_os_str ( ) )
182
204
}
183
205
184
206
fn with_nix_path < T , F > ( & self , f : F ) -> Result < T > where F : FnOnce ( & CStr ) -> T {
185
- self . as_os_str ( ) . as_bytes ( ) . with_nix_path ( f)
207
+ self . as_os_str ( ) . with_nix_path ( f)
186
208
}
187
209
}
188
210
0 commit comments