|
| 1 | +- Feature Name: into-raw-fd-socket-handle-traits |
| 2 | +- Start Date: 2015-06-24 |
| 3 | +- RFC PR: |
| 4 | +- Rust Issue: |
| 5 | + |
| 6 | +# Summary |
| 7 | + |
| 8 | +Introduce and implement `IntoRaw{Fd, Socket, Handle}` traits to complement the |
| 9 | +existing `AsRaw{Fd, Socket, Handle}` traits already in the standard library. |
| 10 | + |
| 11 | +# Motivation |
| 12 | + |
| 13 | +The `FromRaw{Fd, Socket, Handle}` traits each take ownership of the provided |
| 14 | +handle, however, the `AsRaw{Fd, Socket, Handle}` traits do not give up |
| 15 | +ownership. Thus, converting from one handle wrapper to another (for example |
| 16 | +converting an open `fs::File` to a `process::Stdio`) requires the caller to |
| 17 | +either manually `dup` the handle, or `mem::forget` the wrapper, which |
| 18 | +is unergonomic and can be prone to mistakes. |
| 19 | + |
| 20 | +Traits such as `IntoRaw{Fd, Socket, Handle}` will allow for easily transferring |
| 21 | +ownership of OS handles, and it will allow wrappers to perform any |
| 22 | +cleanup/setup as they find necessary. |
| 23 | + |
| 24 | +# Detailed design |
| 25 | + |
| 26 | +The `IntoRaw{Fd, Socket, Handle}` traits will behave exactly like their |
| 27 | +`AsRaw{Fd, Socket, Handle}` counterparts, except they will consume the wrapper |
| 28 | +before transferring ownership of the handle. |
| 29 | + |
| 30 | +Note that these traits should **not** have a blanket implementation over `T: |
| 31 | +AsRaw{Fd, Socket, Handle}`: these traits should be opt-in so that implementors |
| 32 | +can decide if leaking through `mem::forget` is acceptable or another course of |
| 33 | +action is required. |
| 34 | + |
| 35 | +```rust |
| 36 | +// Unix |
| 37 | +pub trait IntoRawFd { |
| 38 | + fn into_raw_fd(self) -> RawFd; |
| 39 | +} |
| 40 | + |
| 41 | +// Windows |
| 42 | +pub trait IntoRawSocket { |
| 43 | + fn into_raw_socket(self) -> RawSocket; |
| 44 | +} |
| 45 | + |
| 46 | +// Windows |
| 47 | +pub trait IntoRawHandle { |
| 48 | + fn into_raw_handle(self) -> RawHandle; |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +# Drawbacks |
| 53 | + |
| 54 | +This adds three new traits and methods which would have to be maintained. |
| 55 | + |
| 56 | +# Alternatives |
| 57 | + |
| 58 | +Instead of defining three new traits we could instead use the |
| 59 | +`std::convert::Into<T>` trait over the different OS handles. However, this |
| 60 | +approach will not offer a duality between methods such as |
| 61 | +`as_raw_fd()`/`into_raw_fd()`, but will instead be `as_raw_fd()`/`into()`. |
| 62 | + |
| 63 | +Another possibility is defining both the newly proposed traits as well as the |
| 64 | +`Into<T>` trait over the OS handles letting the caller choose what they prefer. |
| 65 | + |
| 66 | +# Unresolved questions |
| 67 | + |
| 68 | +None at the moment. |
0 commit comments