@@ -88,213 +88,10 @@ forward_io_write_traits!(PipeWriter);
8888forward_io_write_traits ! ( & PipeWriter ) ;
8989
9090#[ cfg( unix) ]
91- mod unix {
92- use super :: * ;
93-
94- use crate :: {
95- fs:: File ,
96- os:: {
97- fd:: { AsFd , AsRawFd , BorrowedFd , FromRawFd , IntoRawFd , OwnedFd , RawFd } ,
98- unix:: fs:: FileTypeExt ,
99- } ,
100- sys:: {
101- fd:: FileDesc ,
102- pipe:: { anon_pipe, AnonPipe } ,
103- } ,
104- sys_common:: { FromInner , IntoInner } ,
105- } ;
106-
107- #[ inline]
108- pub ( super ) fn pipe ( ) -> io:: Result < ( PipeReader , PipeWriter ) > {
109- anon_pipe ( ) . map ( |( rx, tx) | ( PipeReader ( rx) , PipeWriter ( tx) ) )
110- }
111-
112- macro_rules! impl_traits {
113- ( $name: ty) => {
114- #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
115- impl AsFd for $name {
116- fn as_fd( & self ) -> BorrowedFd <' _> {
117- self . 0 . as_fd( )
118- }
119- }
120- #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
121- impl AsRawFd for $name {
122- fn as_raw_fd( & self ) -> RawFd {
123- self . 0 . as_raw_fd( )
124- }
125- }
126- #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
127- impl From <$name> for OwnedFd {
128- fn from( pipe: $name) -> Self {
129- FileDesc :: into_inner( AnonPipe :: into_inner( pipe. 0 ) )
130- }
131- }
132- #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
133- impl FromRawFd for $name {
134- unsafe fn from_raw_fd( raw_fd: RawFd ) -> Self {
135- Self ( AnonPipe :: from_raw_fd( raw_fd) )
136- }
137- }
138- #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
139- impl IntoRawFd for $name {
140- fn into_raw_fd( self ) -> RawFd {
141- self . 0 . into_raw_fd( )
142- }
143- }
144- #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
145- impl From <$name> for Stdio {
146- fn from( pipe: $name) -> Self {
147- Self :: from( OwnedFd :: from( pipe) )
148- }
149- }
150- } ;
151- }
152- impl_traits ! ( PipeReader ) ;
153- impl_traits ! ( PipeWriter ) ;
154-
155- fn convert_to_pipe ( owned_fd : OwnedFd ) -> io:: Result < AnonPipe > {
156- let file = File :: from ( owned_fd) ;
157- if file. metadata ( ) ?. file_type ( ) . is_fifo ( ) {
158- Ok ( AnonPipe :: from_inner ( FileDesc :: from_inner ( OwnedFd :: from ( file) ) ) )
159- } else {
160- Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput , "Not a pipe" ) )
161- }
162- }
163-
164- #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
165- impl TryFrom < OwnedFd > for PipeReader {
166- type Error = io:: Error ;
167-
168- fn try_from ( owned_fd : OwnedFd ) -> Result < Self , Self :: Error > {
169- convert_to_pipe ( owned_fd)
170- . and_then ( |pipe| {
171- if pipe. as_file_desc ( ) . get_access_mode ( ) ?. readable {
172- Ok ( pipe)
173- } else {
174- Err ( io:: Error :: new (
175- io:: ErrorKind :: InvalidInput ,
176- format ! ( "Pipe {} is not readable" , pipe. as_raw_fd( ) ) ,
177- ) )
178- }
179- } )
180- . map ( Self )
181- }
182- }
183-
184- #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
185- impl TryFrom < OwnedFd > for PipeWriter {
186- type Error = io:: Error ;
187-
188- fn try_from ( owned_fd : OwnedFd ) -> Result < Self , Self :: Error > {
189- convert_to_pipe ( owned_fd)
190- . and_then ( |pipe| {
191- if pipe. as_file_desc ( ) . get_access_mode ( ) ?. writable {
192- Ok ( pipe)
193- } else {
194- Err ( io:: Error :: new (
195- io:: ErrorKind :: InvalidInput ,
196- format ! ( "Pipe {} is not writable" , pipe. as_raw_fd( ) ) ,
197- ) )
198- }
199- } )
200- . map ( Self )
201- }
202- }
203- }
91+ mod unix;
20492
20593#[ cfg( windows) ]
206- mod windows {
207- use super :: * ;
208-
209- use crate :: {
210- os:: windows:: io:: {
211- AsHandle , AsRawHandle , BorrowedHandle , FromRawHandle , IntoRawHandle , OwnedHandle ,
212- RawHandle ,
213- } ,
214- sys:: {
215- c:: { GetFileType , FILE_TYPE_PIPE } ,
216- handle:: Handle ,
217- pipe:: { anon_pipe, AnonPipe , Pipes } ,
218- } ,
219- sys_common:: { FromInner , IntoInner } ,
220- } ;
221-
222- #[ inline]
223- pub ( super ) fn pipe ( ) -> io:: Result < ( PipeReader , PipeWriter ) > {
224- anon_pipe ( true , false ) . map ( |Pipes { ours, theirs } | ( PipeReader ( ours) , PipeWriter ( theirs) ) )
225- }
226-
227- macro_rules! impl_traits {
228- ( $name: ty) => {
229- #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
230- impl AsHandle for $name {
231- fn as_handle( & self ) -> BorrowedHandle <' _> {
232- self . 0 . handle( ) . as_handle( )
233- }
234- }
235- #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
236- impl AsRawHandle for $name {
237- fn as_raw_handle( & self ) -> RawHandle {
238- self . 0 . handle( ) . as_raw_handle( )
239- }
240- }
241-
242- #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
243- impl FromRawHandle for $name {
244- unsafe fn from_raw_handle( raw_handle: RawHandle ) -> Self {
245- Self ( AnonPipe :: from_inner( Handle :: from_raw_handle( raw_handle) ) )
246- }
247- }
248- #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
249- impl IntoRawHandle for $name {
250- fn into_raw_handle( self ) -> RawHandle {
251- self . 0 . into_handle( ) . into_raw_handle( )
252- }
253- }
254-
255- #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
256- impl From <$name> for OwnedHandle {
257- fn from( pipe: $name) -> Self {
258- Handle :: into_inner( AnonPipe :: into_inner( pipe. 0 ) )
259- }
260- }
261- #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
262- impl From <$name> for Stdio {
263- fn from( pipe: $name) -> Self {
264- Self :: from( OwnedHandle :: from( pipe) )
265- }
266- }
267- } ;
268- }
269- impl_traits ! ( PipeReader ) ;
270- impl_traits ! ( PipeWriter ) ;
271-
272- fn convert_to_pipe ( owned_handle : OwnedHandle ) -> io:: Result < AnonPipe > {
273- if unsafe { GetFileType ( owned_handle. as_raw_handle ( ) ) } == FILE_TYPE_PIPE {
274- Ok ( AnonPipe :: from_inner ( Handle :: from_inner ( owned_handle) ) )
275- } else {
276- Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput , "Not a pipe" ) )
277- }
278- }
279-
280- #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
281- impl TryFrom < OwnedHandle > for PipeReader {
282- type Error = io:: Error ;
283-
284- fn try_from ( owned_handle : OwnedHandle ) -> Result < Self , Self :: Error > {
285- convert_to_pipe ( owned_handle) . map ( Self )
286- }
287- }
288-
289- #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
290- impl TryFrom < OwnedHandle > for PipeWriter {
291- type Error = io:: Error ;
292-
293- fn try_from ( owned_handle : OwnedHandle ) -> Result < Self , Self :: Error > {
294- convert_to_pipe ( owned_handle) . map ( Self )
295- }
296- }
297- }
94+ mod windows;
29895
29996#[ cfg( test) ]
30097mod tests;
0 commit comments