@@ -157,16 +157,17 @@ Rust's control, so I/O can be thought of as using [interior mutability].
157
157
158
158
[ interior mutability ] : https://doc.rust-lang.org/reference/interior-mutability.html
159
159
160
- ## ` AsFd ` , ` IntoFd ` , and ` FromFd `
160
+ ## ` AsFd ` , ` Into<OwnedFd> ` , and ` From<OwnedFd> `
161
161
162
- These three traits are conceptual replacements for ` AsRawFd ` , ` IntoRawFd ` , and
163
- ` FromRawFd ` for most use cases. They work in terms of ` OwnedFd ` and
164
- ` BorrowedFd ` , so they automatically enforce their I/O safety invariants.
162
+ These three are conceptual replacements for ` AsRawFd::as_raw_fd ` ,
163
+ ` IntoRawFd::into_raw_fd ` , and ` FromRawFd::from_raw_fd ` , respectively,
164
+ for most use cases. They work in terms of ` OwnedFd ` and ` BorrowedFd ` , so
165
+ they automatically enforce their I/O safety invariants.
165
166
166
- Using these traits , the ` do_some_io ` example in the [ motivation] can avoid
167
- the original problems. Since ` AsFd ` is only implemented for types which
168
- properly own or borrow their file descriptors, this version of ` do_some_io `
169
- doesn't have to worry about being passed bogus or dangling file descriptors:
167
+ Using these, the ` do_some_io ` example in the [ motivation] can avoid the
168
+ original problems. Since ` AsFd ` is only implemented for types which properly
169
+ own or borrow their file descriptors, this version of ` do_some_io ` doesn't
170
+ have to worry about being passed bogus or dangling file descriptors:
170
171
171
172
``` rust
172
173
pub fn do_some_io <FD : AsFd >(input : & FD ) -> io :: Result <()> {
@@ -226,10 +227,11 @@ These types also implement the existing `AsRawFd`, `IntoRawFd`, and `FromRawFd`
226
227
traits, so they can interoperate with existing code that works with ` RawFd `
227
228
types.
228
229
229
- ## ` AsFd ` , ` IntoFd ` , and ` FromFd `
230
+ ## ` AsFd ` , ` Into<OwnedFd> ` , and ` From<OwnedFd> `
230
231
231
- These types provide ` as_fd ` , ` into_fd ` , and ` from_fd ` functions similar to
232
- their ` Raw ` counterparts.
232
+ These types provide ` as_fd ` , ` into ` , and ` from ` functions similar to
233
+ ` AsRawFd::as_raw_fd ` , ` IntoRawFd::into_raw_fd ` , and ` FromRawFd::from_raw_fd ` ,
234
+ respectively.
233
235
234
236
## Prototype implementation
235
237
@@ -249,7 +251,7 @@ unsafe.
249
251
250
252
Crates using ` AsRawFd ` or ` IntoRawFd ` to accept "any file-like type" or "any
251
253
socket-like type", such as [ ` socket2 ` ] 's [ ` SockRef::from ` ] , would need to
252
- either switch to ` AsFd ` or ` IntoFd ` , or make these functions unsafe.
254
+ either switch to ` AsFd ` or ` Into<OwnedFd> ` , or make these functions unsafe.
253
255
254
256
# Rationale and alternatives
255
257
[ rationale-and-alternatives ] : #rationale-and-alternatives
@@ -385,11 +387,11 @@ Some possible future ideas that could build on this RFC include:
385
387
that, with this new guarantee, the high-level wrappers around raw handles
386
388
are unforgeable in safe Rust.
387
389
388
- - There are a few convenience features which can be implemented on top
389
- of the basic ` As ` / ` Into ` / ` From ` traits :
390
- - A ` from_into_fd ` function which takes a ` IntoFd ` and converts it into a
391
- ` FromFd ` , allowing users to perform this common sequence in a single
392
- step.
390
+ - There are a few convenience features which can be implemented for types
391
+ that implement ` AsFd ` , ` Into<OwnedFd> ` , and/or ` From<OwnedFd> ` :
392
+ - A ` from_into_fd ` function which takes a ` Into<OwnedFd> ` and converts it
393
+ into a ` From<OwnedFd> ` , allowing users to perform this common sequence
394
+ in a single step.
393
395
- A ` as_filelike_view::<T>() ` function returns a ` View ` , which contains a
394
396
temporary instance of T constructed from the contained file descriptor,
395
397
allowing users to "view" a raw file descriptor as a ` File ` , ` TcpStream ` ,
@@ -402,7 +404,8 @@ Some possible future ideas that could build on this RFC include:
402
404
` Filelike ` and ` Socketlike ` abstractions could allow code which works in
403
405
this way to be generic over Unix and Windows.
404
406
405
- Similar portability abstractions could apply to the ` From* ` and ` Into* ` traits.
407
+ Similar portability abstractions could apply to ` From<OwnedFd> ` and
408
+ ` Into<OwnedFd> ` .
406
409
407
410
# Thanks
408
411
[ thanks ] : #thanks
0 commit comments