@@ -20,7 +20,13 @@ extern crate nix_test as nixtest;
2020
2121// Re-exports
2222pub use libc:: { c_int, c_void} ;
23- pub use errno:: Errno ;
23+ pub use errno:: { Errno , Result } ;
24+ pub use nix_string:: NixString ;
25+
26+ mod nix_string;
27+
28+ #[ macro_use]
29+ pub mod cstr;
2430
2531pub mod errno;
2632pub mod features;
@@ -42,186 +48,3 @@ pub mod sched;
4248
4349pub mod sys;
4450pub mod unistd;
45-
46- /*
47- *
48- * ===== Result / Error =====
49- *
50- */
51-
52- use libc:: c_char;
53- use std:: { ptr, result} ;
54- use std:: ffi:: { CStr , OsStr } ;
55- use std:: path:: { Path , PathBuf } ;
56- use std:: os:: unix:: ffi:: OsStrExt ;
57- use std:: io;
58- use std:: fmt;
59- use std:: error;
60- use libc:: PATH_MAX ;
61-
62- pub type Result < T > = result:: Result < T , Error > ;
63-
64- #[ derive( Clone , Copy , Debug , PartialEq ) ]
65- pub enum Error {
66- Sys ( errno:: Errno ) ,
67- InvalidPath ,
68- }
69-
70- impl Error {
71- pub fn from_errno ( errno : errno:: Errno ) -> Error {
72- Error :: Sys ( errno)
73- }
74-
75- pub fn last ( ) -> Error {
76- Error :: Sys ( errno:: Errno :: last ( ) )
77- }
78-
79- pub fn invalid_argument ( ) -> Error {
80- Error :: Sys ( errno:: EINVAL )
81- }
82-
83- pub fn errno ( & self ) -> errno:: Errno {
84- match * self {
85- Error :: Sys ( errno) => errno,
86- Error :: InvalidPath => errno:: Errno :: EINVAL ,
87- }
88- }
89- }
90-
91- impl From < errno:: Errno > for Error {
92- fn from ( errno : errno:: Errno ) -> Error { Error :: from_errno ( errno) }
93- }
94-
95- impl error:: Error for Error {
96- fn description ( & self ) -> & str {
97- match self {
98- & Error :: InvalidPath => "Invalid path" ,
99- & Error :: Sys ( ref errno) => errno. desc ( ) ,
100- }
101- }
102- }
103-
104- impl fmt:: Display for Error {
105- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
106- match self {
107- & Error :: InvalidPath => write ! ( f, "Invalid path" ) ,
108- & Error :: Sys ( errno) => write ! ( f, "{:?}: {}" , errno, errno. desc( ) ) ,
109- }
110- }
111- }
112-
113- impl From < Error > for io:: Error {
114- fn from ( err : Error ) -> Self {
115- match err {
116- Error :: InvalidPath => io:: Error :: new ( io:: ErrorKind :: InvalidInput , err) ,
117- Error :: Sys ( errno) => io:: Error :: from_raw_os_error ( errno as i32 ) ,
118- }
119- }
120- }
121-
122- pub trait NixPath {
123- fn len ( & self ) -> usize ;
124-
125- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T >
126- where F : FnOnce ( & CStr ) -> T ;
127- }
128-
129- impl NixPath for str {
130- fn len ( & self ) -> usize {
131- NixPath :: len ( OsStr :: new ( self ) )
132- }
133-
134- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T >
135- where F : FnOnce ( & CStr ) -> T {
136- OsStr :: new ( self ) . with_nix_path ( f)
137- }
138- }
139-
140- impl NixPath for OsStr {
141- fn len ( & self ) -> usize {
142- self . as_bytes ( ) . len ( )
143- }
144-
145- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T >
146- where F : FnOnce ( & CStr ) -> T {
147- self . as_bytes ( ) . with_nix_path ( f)
148- }
149- }
150-
151- impl NixPath for CStr {
152- fn len ( & self ) -> usize {
153- self . to_bytes ( ) . len ( )
154- }
155-
156- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T >
157- where F : FnOnce ( & CStr ) -> T {
158- // Equivalence with the [u8] impl.
159- if self . len ( ) >= PATH_MAX as usize {
160- return Err ( Error :: InvalidPath ) ;
161- }
162-
163- Ok ( f ( self ) )
164- }
165- }
166-
167- impl NixPath for [ u8 ] {
168- fn len ( & self ) -> usize {
169- self . len ( )
170- }
171-
172- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T >
173- where F : FnOnce ( & CStr ) -> T {
174- let mut buf = [ 0u8 ; PATH_MAX as usize ] ;
175-
176- if self . len ( ) >= PATH_MAX as usize {
177- return Err ( Error :: InvalidPath ) ;
178- }
179-
180- match self . iter ( ) . position ( |b| * b == 0 ) {
181- Some ( _) => Err ( Error :: InvalidPath ) ,
182- None => {
183- unsafe {
184- // TODO: Replace with bytes::copy_memory. rust-lang/rust#24028
185- ptr:: copy_nonoverlapping ( self . as_ptr ( ) , buf. as_mut_ptr ( ) , self . len ( ) ) ;
186- Ok ( f ( CStr :: from_ptr ( buf. as_ptr ( ) as * const c_char ) ) )
187- }
188-
189- }
190- }
191- }
192- }
193-
194- impl NixPath for Path {
195- fn len ( & self ) -> usize {
196- NixPath :: len ( self . as_os_str ( ) )
197- }
198-
199- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T > where F : FnOnce ( & CStr ) -> T {
200- self . as_os_str ( ) . with_nix_path ( f)
201- }
202- }
203-
204- impl NixPath for PathBuf {
205- fn len ( & self ) -> usize {
206- NixPath :: len ( self . as_os_str ( ) )
207- }
208-
209- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T > where F : FnOnce ( & CStr ) -> T {
210- self . as_os_str ( ) . with_nix_path ( f)
211- }
212- }
213-
214- /// Treats `None` as an empty string.
215- impl < ' a , NP : ?Sized + NixPath > NixPath for Option < & ' a NP > {
216- fn len ( & self ) -> usize {
217- self . map_or ( 0 , NixPath :: len)
218- }
219-
220- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T > where F : FnOnce ( & CStr ) -> T {
221- if let Some ( nix_path) = * self {
222- nix_path. with_nix_path ( f)
223- } else {
224- unsafe { CStr :: from_ptr ( "\0 " . as_ptr ( ) as * const _ ) . with_nix_path ( f) }
225- }
226- }
227- }
0 commit comments