@@ -3,7 +3,7 @@ use crate::{stdio::StdioImpl, Error, Stdio};
3
3
use std:: {
4
4
fs:: { File , OpenOptions } ,
5
5
io,
6
- os:: unix:: io:: { AsRawFd , RawFd } ,
6
+ os:: unix:: io:: { AsRawFd , FromRawFd , IntoRawFd , OwnedFd , RawFd } ,
7
7
} ;
8
8
9
9
use libc:: { c_int, fcntl, F_GETFL , F_SETFL , O_NONBLOCK } ;
@@ -29,9 +29,7 @@ fn get_null_fd() -> Result<RawFd, Error> {
29
29
}
30
30
31
31
pub ( crate ) enum Fd {
32
- PipeReadEnd ( PipeRead ) ,
33
- PipeWriteEnd ( PipeWrite ) ,
34
-
32
+ Owned ( OwnedFd ) ,
35
33
Borrowed ( RawFd ) ,
36
34
Null ,
37
35
}
@@ -56,9 +54,7 @@ impl Fd {
56
54
use Fd :: * ;
57
55
58
56
let fd = match self {
59
- PipeReadEnd ( fd) => Some ( AsRawFd :: as_raw_fd ( fd) ) ,
60
- PipeWriteEnd ( fd) => Some ( AsRawFd :: as_raw_fd ( fd) ) ,
61
-
57
+ Owned ( owned_fd) => Some ( owned_fd. as_raw_fd ( ) ) ,
62
58
Borrowed ( rawfd) => Some ( * rawfd) ,
63
59
Null => None ,
64
60
} ;
@@ -71,6 +67,12 @@ impl Fd {
71
67
get_null_fd ( )
72
68
}
73
69
}
70
+
71
+ fn new_owned < T : IntoRawFd > ( fd : T ) -> Self {
72
+ let raw_fd = fd. into_raw_fd ( ) ;
73
+ // Safety: IntoRawFd::into_raw_fd must return a valid raw fd.
74
+ unsafe { Fd :: Owned ( OwnedFd :: from_raw_fd ( raw_fd) ) }
75
+ }
74
76
}
75
77
76
78
impl Stdio {
@@ -80,7 +82,7 @@ impl Stdio {
80
82
StdioImpl :: Null => Ok ( ( Fd :: Null , None ) ) ,
81
83
StdioImpl :: Pipe => {
82
84
let ( read, write) = create_pipe ( ) ?;
83
- Ok ( ( Fd :: PipeReadEnd ( read) , Some ( write) ) )
85
+ Ok ( ( Fd :: new_owned ( read) , Some ( write) ) )
84
86
}
85
87
StdioImpl :: Fd ( fd) => Ok ( ( Fd :: Borrowed ( fd. as_raw_fd ( ) ) , None ) ) ,
86
88
}
@@ -92,7 +94,7 @@ impl Stdio {
92
94
StdioImpl :: Null => Ok ( ( Fd :: Null , None ) ) ,
93
95
StdioImpl :: Pipe => {
94
96
let ( read, write) = create_pipe ( ) ?;
95
- Ok ( ( Fd :: PipeWriteEnd ( write) , Some ( read) ) )
97
+ Ok ( ( Fd :: new_owned ( write) , Some ( read) ) )
96
98
}
97
99
StdioImpl :: Fd ( fd) => Ok ( ( Fd :: Borrowed ( fd. as_raw_fd ( ) ) , None ) ) ,
98
100
}
0 commit comments