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