@@ -16,7 +16,7 @@ use fs;
16
16
use net;
17
17
use os:: raw;
18
18
use sys;
19
- use sys_common:: { self , AsInner , FromInner } ;
19
+ use sys_common:: { self , AsInner , FromInner , IntoInner } ;
20
20
21
21
/// Raw file descriptors.
22
22
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -59,6 +59,18 @@ pub trait FromRawFd {
59
59
unsafe fn from_raw_fd ( fd : RawFd ) -> Self ;
60
60
}
61
61
62
+ /// A trait to express the ability to consume an object and acquire ownership of
63
+ /// its raw file descriptor.
64
+ #[ unstable( feature = "into_raw_os" , reason = "recently added API" ) ]
65
+ pub trait IntoRawFd {
66
+ /// Consumes this object, returning the raw underlying file descriptor.
67
+ ///
68
+ /// This function **transfers ownership** of the underlying file descriptor
69
+ /// to the caller. Callers are then the unique owners of the file descriptor
70
+ /// and must close the descriptor once it's no longer needed.
71
+ fn into_raw_fd ( self ) -> RawFd ;
72
+ }
73
+
62
74
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
63
75
impl AsRawFd for fs:: File {
64
76
fn as_raw_fd ( & self ) -> RawFd {
@@ -71,6 +83,11 @@ impl FromRawFd for fs::File {
71
83
fs:: File :: from_inner ( sys:: fs:: File :: from_inner ( fd) )
72
84
}
73
85
}
86
+ impl IntoRawFd for fs:: File {
87
+ fn into_raw_fd ( self ) -> RawFd {
88
+ self . into_inner ( ) . into_fd ( ) . into_raw ( )
89
+ }
90
+ }
74
91
75
92
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
76
93
impl AsRawFd for net:: TcpStream {
@@ -106,3 +123,19 @@ impl FromRawFd for net::UdpSocket {
106
123
net:: UdpSocket :: from_inner ( sys_common:: net:: UdpSocket :: from_inner ( socket) )
107
124
}
108
125
}
126
+
127
+ impl IntoRawFd for net:: TcpStream {
128
+ fn into_raw_fd ( self ) -> RawFd {
129
+ self . into_inner ( ) . into_socket ( ) . into_inner ( )
130
+ }
131
+ }
132
+ impl IntoRawFd for net:: TcpListener {
133
+ fn into_raw_fd ( self ) -> RawFd {
134
+ self . into_inner ( ) . into_socket ( ) . into_inner ( )
135
+ }
136
+ }
137
+ impl IntoRawFd for net:: UdpSocket {
138
+ fn into_raw_fd ( self ) -> RawFd {
139
+ self . into_inner ( ) . into_socket ( ) . into_inner ( )
140
+ }
141
+ }
0 commit comments