5
5
use crate :: fs;
6
6
use crate :: io;
7
7
use crate :: net;
8
+ #[ cfg( doc) ]
9
+ use crate :: os:: windows:: io:: { AsHandle , AsSocket } ;
8
10
use crate :: os:: windows:: io:: { OwnedHandle , OwnedSocket } ;
9
11
use crate :: os:: windows:: raw;
10
12
use crate :: sys;
@@ -22,7 +24,15 @@ pub type RawSocket = raw::SOCKET;
22
24
/// Extracts raw handles.
23
25
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
24
26
pub trait AsRawHandle {
25
- /// Extracts the raw handle, without taking any ownership.
27
+ /// Extracts the raw handle.
28
+ ///
29
+ /// This function is typically used to **borrow** an owned handle.
30
+ /// When used in this way, this method does **not** pass ownership of the
31
+ /// raw handle to the caller, and the handle is only guaranteed
32
+ /// to be valid while the original object has not yet been destroyed.
33
+ ///
34
+ /// However, borrowing is not strictly required. See [`AsHandle::as_handle`]
35
+ /// for an API which strictly borrows a handle.
26
36
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
27
37
fn as_raw_handle ( & self ) -> RawHandle ;
28
38
}
@@ -32,15 +42,28 @@ pub trait AsRawHandle {
32
42
pub trait FromRawHandle {
33
43
/// Constructs a new I/O object from the specified raw handle.
34
44
///
35
- /// This function will **consume ownership** of the handle given,
36
- /// passing responsibility for closing the handle to the returned
37
- /// object.
45
+ /// This function is typically used to **consume ownership** of the handle
46
+ /// given, passing responsibility for closing the handle to the returned
47
+ /// object. When used in this way, the returned object
48
+ /// will take responsibility for closing it when the object goes out of
49
+ /// scope.
50
+ ///
51
+ /// However, consuming ownership is not strictly required. Use a
52
+ /// `From<OwnedHandle>::from` implementation for an API which strictly
53
+ /// consumes ownership.
54
+ ///
55
+ /// # Safety
38
56
///
39
- /// This function is also unsafe as the primitives currently returned
40
- /// have the contract that they are the sole owner of the file
41
- /// descriptor they are wrapping. Usage of this function could
42
- /// accidentally allow violating this contract which can cause memory
43
- /// unsafety in code that relies on it being true.
57
+ /// The `handle` passed in must:
58
+ /// - be a valid an open handle,
59
+ /// - be a handle for a resource that may be freed via [`CloseHandle`]
60
+ /// (as opposed to `RegCloseKey` or other close functions).
61
+ ///
62
+ /// Note that the handle *may* have the value `INVALID_HANDLE_VALUE` (-1),
63
+ /// which is sometimes a valid handle value. See [here] for the full story.
64
+ ///
65
+ /// [`CloseHandle`]: https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
66
+ /// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
44
67
#[ stable( feature = "from_raw_os" , since = "1.1.0" ) ]
45
68
unsafe fn from_raw_handle ( handle : RawHandle ) -> Self ;
46
69
}
@@ -51,9 +74,13 @@ pub trait FromRawHandle {
51
74
pub trait IntoRawHandle {
52
75
/// Consumes this object, returning the raw underlying handle.
53
76
///
54
- /// This function **transfers ownership** of the underlying handle to the
55
- /// caller. Callers are then the unique owners of the handle and must close
56
- /// it once it's no longer needed.
77
+ /// This function is typically used to **transfer ownership** of the underlying
78
+ /// handle to the caller. When used in this way, callers are then the unique
79
+ /// owners of the handle and must close it once it's no longer needed.
80
+ ///
81
+ /// However, transferring ownership is not strictly required. Use a
82
+ /// `Into<OwnedHandle>::into` implementation for an API which strictly
83
+ /// transfers ownership.
57
84
#[ stable( feature = "into_raw_os" , since = "1.4.0" ) ]
58
85
fn into_raw_handle ( self ) -> RawHandle ;
59
86
}
@@ -130,24 +157,41 @@ impl IntoRawHandle for fs::File {
130
157
/// Extracts raw sockets.
131
158
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
132
159
pub trait AsRawSocket {
133
- /// Extracts the underlying raw socket from this object.
160
+ /// Extracts the raw socket.
161
+ ///
162
+ /// This function is typically used to **borrow** an owned socket.
163
+ /// When used in this way, this method does **not** pass ownership of the
164
+ /// raw socket to the caller, and the socket is only guaranteed
165
+ /// to be valid while the original object has not yet been destroyed.
166
+ ///
167
+ /// However, borrowing is not strictly required. See [`AsSocket::as_socket`]
168
+ /// for an API which strictly borrows a socket.
134
169
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
135
170
fn as_raw_socket ( & self ) -> RawSocket ;
136
171
}
137
172
138
173
/// Creates I/O objects from raw sockets.
139
174
#[ stable( feature = "from_raw_os" , since = "1.1.0" ) ]
140
175
pub trait FromRawSocket {
141
- /// Creates a new I/O object from the given raw socket.
176
+ /// Constructs a new I/O object from the specified raw socket.
177
+ ///
178
+ /// This function is typically used to **consume ownership** of the socket
179
+ /// given, passing responsibility for closing the socket to the returned
180
+ /// object. When used in this way, the returned object
181
+ /// will take responsibility for closing it when the object goes out of
182
+ /// scope.
142
183
///
143
- /// This function will **consume ownership** of the socket provided and
144
- /// it will be closed when the returned object goes out of scope.
184
+ /// However, consuming ownership is not strictly required. Use a
185
+ /// `From<OwnedSocket>::from` implementation for an API which strictly
186
+ /// consumes ownership.
145
187
///
146
- /// This function is also unsafe as the primitives currently returned
147
- /// have the contract that they are the sole owner of the file
148
- /// descriptor they are wrapping. Usage of this function could
149
- /// accidentally allow violating this contract which can cause memory
150
- /// unsafety in code that relies on it being true.
188
+ /// # Safety
189
+ ///
190
+ /// The `socket` passed in must:
191
+ /// - be a valid an open socket,
192
+ /// - be a socket that may be freed via [`closesocket`].
193
+ ///
194
+ /// [`closesocket`]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-closesocket
151
195
#[ stable( feature = "from_raw_os" , since = "1.1.0" ) ]
152
196
unsafe fn from_raw_socket ( sock : RawSocket ) -> Self ;
153
197
}
@@ -158,9 +202,13 @@ pub trait FromRawSocket {
158
202
pub trait IntoRawSocket {
159
203
/// Consumes this object, returning the raw underlying socket.
160
204
///
161
- /// This function **transfers ownership** of the underlying socket to the
162
- /// caller. Callers are then the unique owners of the socket and must close
163
- /// it once it's no longer needed.
205
+ /// This function is typically used to **transfer ownership** of the underlying
206
+ /// socket to the caller. When used in this way, callers are then the unique
207
+ /// owners of the socket and must close it once it's no longer needed.
208
+ ///
209
+ /// However, transferring ownership is not strictly required. Use a
210
+ /// `Into<OwnedSocket>::into` implementation for an API which strictly
211
+ /// transfers ownership.
164
212
#[ stable( feature = "into_raw_os" , since = "1.4.0" ) ]
165
213
fn into_raw_socket ( self ) -> RawSocket ;
166
214
}
0 commit comments