@@ -22,7 +22,15 @@ pub type RawSocket = raw::SOCKET;
22
22
/// Extracts raw handles.
23
23
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
24
24
pub trait AsRawHandle {
25
- /// Extracts the raw handle, without taking any ownership.
25
+ /// Extracts the raw handle.
26
+ ///
27
+ /// This function is typically used to **borrow** an owned handle.
28
+ /// When used in this way, this method does **not** pass ownership of the
29
+ /// raw handle to the caller, and the handle is only guaranteed
30
+ /// to be valid while the original object has not yet been destroyed.
31
+ ///
32
+ /// However, borrowing is not strictly required. See [`AsHandle::as_handle`]
33
+ /// for an API which strictly borrows a handle.
26
34
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
27
35
fn as_raw_handle ( & self ) -> RawHandle ;
28
36
}
@@ -32,15 +40,29 @@ pub trait AsRawHandle {
32
40
pub trait FromRawHandle {
33
41
/// Constructs a new I/O object from the specified raw handle.
34
42
///
35
- /// This function will **consume ownership** of the handle given,
36
- /// passing responsibility for closing the handle to the returned
37
- /// object.
43
+ /// This function is typically used to **consume ownership** of the handle
44
+ /// given, passing responsibility for closing the handle to the returned
45
+ /// object. When used in this way, the returned object
46
+ /// will take responsibility for closing it when the object goes out of
47
+ /// scope.
48
+ ///
49
+ /// However, consuming ownership is not strictly required. See
50
+ /// [`FromHandle::from_handle`] for an API which strictly consumes ownership.
51
+ ///
52
+ /// # Safety
38
53
///
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.
54
+ /// The `handle` passed in must:
55
+ /// - be a valid an open handle,
56
+ /// - be a handle opened for synchronous I/O, *without* the
57
+ /// `FILE_FLAG_OVERLAPPED` flag, and
58
+ /// - be a handle for a resource that may be freed via [`CloseHandle`]
59
+ /// (as opposed to `RegCloseKey` or other close functions).
60
+ ///
61
+ /// Note that the handle *may* have the value `INVALID_HANDLE_VALUE` (-1),
62
+ /// which is sometimes a valid handle value. See [here] for the full story.
63
+ ///
64
+ /// [`CloseHandle`]: https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
65
+ /// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
44
66
#[ stable( feature = "from_raw_os" , since = "1.1.0" ) ]
45
67
unsafe fn from_raw_handle ( handle : RawHandle ) -> Self ;
46
68
}
@@ -51,9 +73,12 @@ pub trait FromRawHandle {
51
73
pub trait IntoRawHandle {
52
74
/// Consumes this object, returning the raw underlying handle.
53
75
///
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.
76
+ /// This function is typically used to **transfer ownership** of the underlying
77
+ /// handle to the caller. When used in this way, callers are then the unique
78
+ /// owners of the handle and must close it once it's no longer needed.
79
+ ///
80
+ /// However, transferring ownership is not strictly required. See
81
+ /// [`IntoHandle::into_handle`] for an API which strictly transfers ownership.
57
82
#[ stable( feature = "into_raw_os" , since = "1.4.0" ) ]
58
83
fn into_raw_handle ( self ) -> RawHandle ;
59
84
}
@@ -130,24 +155,40 @@ impl IntoRawHandle for fs::File {
130
155
/// Extracts raw sockets.
131
156
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
132
157
pub trait AsRawSocket {
133
- /// Extracts the underlying raw socket from this object.
158
+ /// Extracts the raw socket.
159
+ ///
160
+ /// This function is typically used to **borrow** an owned socket.
161
+ /// When used in this way, this method does **not** pass ownership of the
162
+ /// raw socket to the caller, and the socket is only guaranteed
163
+ /// to be valid while the original object has not yet been destroyed.
164
+ ///
165
+ /// However, borrowing is not strictly required. See [`AsSocket::as_socket`]
166
+ /// for an API which strictly borrows a socket.
134
167
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
135
168
fn as_raw_socket ( & self ) -> RawSocket ;
136
169
}
137
170
138
171
/// Creates I/O objects from raw sockets.
139
172
#[ stable( feature = "from_raw_os" , since = "1.1.0" ) ]
140
173
pub trait FromRawSocket {
141
- /// Creates a new I/O object from the given raw socket.
174
+ /// Constructs a new I/O object from the specified raw socket.
175
+ ///
176
+ /// This function is typically used to **consume ownership** of the socket
177
+ /// given, passing responsibility for closing the socket to the returned
178
+ /// object. When used in this way, the returned object
179
+ /// will take responsibility for closing it when the object goes out of
180
+ /// scope.
142
181
///
143
- /// This function will **consume ownership** of the socket provided and
144
- /// it will be closed when the returned object goes out of scope .
182
+ /// However, consuming ownership is not strictly required. See
183
+ /// [`FromSocket::from_socket`] for an API which strictly consumes ownership .
145
184
///
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.
185
+ /// # Safety
186
+ ///
187
+ /// The `socket` passed in must:
188
+ /// - be a valid an open socket,
189
+ /// - be a handle for a resource that may be freed via [`closesocket`].
190
+ ///
191
+ /// [`closesocket`]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-closesocket
151
192
#[ stable( feature = "from_raw_os" , since = "1.1.0" ) ]
152
193
unsafe fn from_raw_socket ( sock : RawSocket ) -> Self ;
153
194
}
@@ -158,9 +199,12 @@ pub trait FromRawSocket {
158
199
pub trait IntoRawSocket {
159
200
/// Consumes this object, returning the raw underlying socket.
160
201
///
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.
202
+ /// This function is typically used to **transfer ownership** of the underlying
203
+ /// socket to the caller. When used in this way, callers are then the unique
204
+ /// owners of the socket and must close it once it's no longer needed.
205
+ ///
206
+ /// However, transferring ownership is not strictly required. See
207
+ /// [`IntoSocket::into_socket`] for an API which strictly transfers ownership.
164
208
#[ stable( feature = "into_raw_os" , since = "1.4.0" ) ]
165
209
fn into_raw_socket ( self ) -> RawSocket ;
166
210
}
0 commit comments