@@ -18,8 +18,13 @@ extern crate libc;
1818#[ cfg( test) ]
1919extern crate nix_test as nixtest;
2020
21- // Re-export some libc constants
21+ // Re-exports
2222pub use libc:: { c_int, c_void} ;
23+ pub use errno:: { Errno , Result } ;
24+ pub use nix_string:: NixString ;
25+
26+ #[ macro_use]
27+ mod nix_string;
2328
2429pub mod errno;
2530pub mod features;
@@ -36,142 +41,3 @@ pub mod sched;
3641
3742pub mod sys;
3843pub mod unistd;
39-
40- /*
41- *
42- * ===== Result / Error =====
43- *
44- */
45-
46- use libc:: c_char;
47- use std:: { ptr, result} ;
48- use std:: ffi:: CStr ;
49- use std:: path:: { Path , PathBuf } ;
50- use std:: os:: unix:: ffi:: OsStrExt ;
51- use std:: io;
52- use std:: fmt;
53- use std:: error;
54- use libc:: PATH_MAX ;
55-
56- pub type Result < T > = result:: Result < T , Error > ;
57-
58- #[ derive( Clone , Copy , Debug , PartialEq ) ]
59- pub enum Error {
60- Sys ( errno:: Errno ) ,
61- InvalidPath ,
62- }
63-
64- impl Error {
65- pub fn from_errno ( errno : errno:: Errno ) -> Error {
66- Error :: Sys ( errno)
67- }
68-
69- pub fn last ( ) -> Error {
70- Error :: Sys ( errno:: Errno :: last ( ) )
71- }
72-
73- pub fn invalid_argument ( ) -> Error {
74- Error :: Sys ( errno:: EINVAL )
75- }
76-
77- pub fn errno ( & self ) -> errno:: Errno {
78- match * self {
79- Error :: Sys ( errno) => errno,
80- Error :: InvalidPath => errno:: Errno :: EINVAL ,
81- }
82- }
83- }
84-
85- impl From < errno:: Errno > for Error {
86- fn from ( errno : errno:: Errno ) -> Error { Error :: from_errno ( errno) }
87- }
88-
89- impl error:: Error for Error {
90- fn description ( & self ) -> & str {
91- match self {
92- & Error :: InvalidPath => "Invalid path" ,
93- & Error :: Sys ( ref errno) => errno. desc ( ) ,
94- }
95- }
96- }
97-
98- impl fmt:: Display for Error {
99- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
100- match self {
101- & Error :: InvalidPath => write ! ( f, "Invalid path" ) ,
102- & Error :: Sys ( errno) => write ! ( f, "{:?}: {}" , errno, errno. desc( ) ) ,
103- }
104- }
105- }
106-
107- impl From < Error > for io:: Error {
108- fn from ( err : Error ) -> Self {
109- match err {
110- Error :: InvalidPath => io:: Error :: new ( io:: ErrorKind :: InvalidInput , err) ,
111- Error :: Sys ( errno) => io:: Error :: from_raw_os_error ( errno as i32 ) ,
112- }
113- }
114- }
115-
116- pub trait NixPath {
117- fn len ( & self ) -> usize ;
118-
119- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T >
120- where F : FnOnce ( & CStr ) -> T ;
121- }
122-
123- impl NixPath for [ u8 ] {
124- fn len ( & self ) -> usize {
125- self . len ( )
126- }
127-
128- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T >
129- where F : FnOnce ( & CStr ) -> T {
130- let mut buf = [ 0u8 ; PATH_MAX as usize ] ;
131-
132- if self . len ( ) >= PATH_MAX as usize {
133- return Err ( Error :: InvalidPath ) ;
134- }
135-
136- match self . iter ( ) . position ( |b| * b == 0 ) {
137- Some ( _) => Err ( Error :: InvalidPath ) ,
138- None => {
139- unsafe {
140- // TODO: Replace with bytes::copy_memory. rust-lang/rust#24028
141- ptr:: copy_nonoverlapping ( self . as_ptr ( ) , buf. as_mut_ptr ( ) , self . len ( ) ) ;
142- Ok ( f ( CStr :: from_ptr ( buf. as_ptr ( ) as * const c_char ) ) )
143- }
144-
145- }
146- }
147- }
148- }
149-
150- impl NixPath for Path {
151- fn len ( & self ) -> usize {
152- self . as_os_str ( ) . as_bytes ( ) . len ( )
153- }
154-
155- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T > where F : FnOnce ( & CStr ) -> T {
156- self . as_os_str ( ) . as_bytes ( ) . with_nix_path ( f)
157- }
158- }
159-
160- impl NixPath for PathBuf {
161- fn len ( & self ) -> usize {
162- self . as_os_str ( ) . as_bytes ( ) . len ( )
163- }
164-
165- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T > where F : FnOnce ( & CStr ) -> T {
166- self . as_os_str ( ) . as_bytes ( ) . with_nix_path ( f)
167- }
168- }
169-
170- #[ inline]
171- pub fn from_ffi ( res : libc:: c_int ) -> Result < ( ) > {
172- if res != 0 {
173- return Err ( Error :: Sys ( errno:: Errno :: last ( ) ) ) ;
174- }
175-
176- Ok ( ( ) )
177- }
0 commit comments