13
13
use fs;
14
14
use os:: windows:: raw;
15
15
use net;
16
- use sys_common:: { self , AsInner , FromInner } ;
16
+ use sys_common:: { self , AsInner , FromInner , IntoInner } ;
17
17
use sys;
18
18
19
19
/// Raw HANDLEs.
@@ -50,6 +50,18 @@ pub trait FromRawHandle {
50
50
unsafe fn from_raw_handle ( handle : RawHandle ) -> Self ;
51
51
}
52
52
53
+ /// A trait to express the ability to consume an object and acquire ownership of
54
+ /// its raw `HANDLE`.
55
+ #[ unstable( feature = "into_raw_os" , reason = "recently added API" ) ]
56
+ pub trait IntoRawHandle {
57
+ /// Consumes this object, returning the raw underlying handle.
58
+ ///
59
+ /// This function **transfers ownership** of the underlying handle to the
60
+ /// caller. Callers are then the unique owners of the handle and must close
61
+ /// it once it's no longer needed.
62
+ fn into_raw_handle ( self ) -> RawHandle ;
63
+ }
64
+
53
65
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
54
66
impl AsRawHandle for fs:: File {
55
67
fn as_raw_handle ( & self ) -> RawHandle {
@@ -65,6 +77,12 @@ impl FromRawHandle for fs::File {
65
77
}
66
78
}
67
79
80
+ impl IntoRawHandle for fs:: File {
81
+ fn into_raw_handle ( self ) -> RawHandle {
82
+ self . into_inner ( ) . into_handle ( ) . into_raw ( ) as * mut _
83
+ }
84
+ }
85
+
68
86
/// Extract raw sockets.
69
87
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
70
88
pub trait AsRawSocket {
@@ -90,6 +108,18 @@ pub trait FromRawSocket {
90
108
unsafe fn from_raw_socket ( sock : RawSocket ) -> Self ;
91
109
}
92
110
111
+ /// A trait to express the ability to consume an object and acquire ownership of
112
+ /// its raw `SOCKET`.
113
+ #[ unstable( feature = "into_raw_os" , reason = "recently added API" ) ]
114
+ pub trait IntoRawSocket {
115
+ /// Consumes this object, returning the raw underlying socket.
116
+ ///
117
+ /// This function **transfers ownership** of the underlying socket to the
118
+ /// caller. Callers are then the unique owners of the socket and must close
119
+ /// it once it's no longer needed.
120
+ fn into_raw_socket ( self ) -> RawSocket ;
121
+ }
122
+
93
123
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
94
124
impl AsRawSocket for net:: TcpStream {
95
125
fn as_raw_socket ( & self ) -> RawSocket {
@@ -130,3 +160,21 @@ impl FromRawSocket for net::UdpSocket {
130
160
net:: UdpSocket :: from_inner ( sys_common:: net:: UdpSocket :: from_inner ( sock) )
131
161
}
132
162
}
163
+
164
+ impl IntoRawSocket for net:: TcpStream {
165
+ fn into_raw_socket ( self ) -> RawSocket {
166
+ self . into_inner ( ) . into_socket ( ) . into_inner ( )
167
+ }
168
+ }
169
+
170
+ impl IntoRawSocket for net:: TcpListener {
171
+ fn into_raw_socket ( self ) -> RawSocket {
172
+ self . into_inner ( ) . into_socket ( ) . into_inner ( )
173
+ }
174
+ }
175
+
176
+ impl IntoRawSocket for net:: UdpSocket {
177
+ fn into_raw_socket ( self ) -> RawSocket {
178
+ self . into_inner ( ) . into_socket ( ) . into_inner ( )
179
+ }
180
+ }
0 commit comments