|
| 1 | +//! SOLID-specific extensions to general I/O primitives |
| 2 | +
|
| 3 | +#![deny(unsafe_op_in_unsafe_fn)] |
| 4 | +#![unstable(feature = "solid_ext", issue = "none")] |
| 5 | + |
| 6 | +use crate::net; |
| 7 | +use crate::sys; |
| 8 | +use crate::sys_common::{self, AsInner, FromInner, IntoInner}; |
| 9 | + |
| 10 | +/// Raw file descriptors. |
| 11 | +pub type RawFd = i32; |
| 12 | + |
| 13 | +/// A trait to extract the raw SOLID Sockets file descriptor from an underlying |
| 14 | +/// object. |
| 15 | +pub trait AsRawFd { |
| 16 | + /// Extracts the raw file descriptor. |
| 17 | + /// |
| 18 | + /// This method does **not** pass ownership of the raw file descriptor |
| 19 | + /// to the caller. The descriptor is only guaranteed to be valid while |
| 20 | + /// the original object has not yet been destroyed. |
| 21 | + fn as_raw_fd(&self) -> RawFd; |
| 22 | +} |
| 23 | + |
| 24 | +/// A trait to express the ability to construct an object from a raw file |
| 25 | +/// descriptor. |
| 26 | +pub trait FromRawFd { |
| 27 | + /// Constructs a new instance of `Self` from the given raw file |
| 28 | + /// descriptor. |
| 29 | + /// |
| 30 | + /// This function **consumes ownership** of the specified file |
| 31 | + /// descriptor. The returned object will take responsibility for closing |
| 32 | + /// it when the object goes out of scope. |
| 33 | + /// |
| 34 | + /// This function is also unsafe as the primitives currently returned |
| 35 | + /// have the contract that they are the sole owner of the file |
| 36 | + /// descriptor they are wrapping. Usage of this function could |
| 37 | + /// accidentally allow violating this contract which can cause memory |
| 38 | + /// unsafety in code that relies on it being true. |
| 39 | + unsafe fn from_raw_fd(fd: RawFd) -> Self; |
| 40 | +} |
| 41 | + |
| 42 | +/// A trait to express the ability to consume an object and acquire ownership of |
| 43 | +/// its raw file descriptor. |
| 44 | +pub trait IntoRawFd { |
| 45 | + /// Consumes this object, returning the raw underlying file descriptor. |
| 46 | + /// |
| 47 | + /// This function **transfers ownership** of the underlying file descriptor |
| 48 | + /// to the caller. Callers are then the unique owners of the file descriptor |
| 49 | + /// and must close the descriptor once it's no longer needed. |
| 50 | + fn into_raw_fd(self) -> RawFd; |
| 51 | +} |
| 52 | + |
| 53 | +#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")] |
| 54 | +impl AsRawFd for RawFd { |
| 55 | + #[inline] |
| 56 | + fn as_raw_fd(&self) -> RawFd { |
| 57 | + *self |
| 58 | + } |
| 59 | +} |
| 60 | +#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")] |
| 61 | +impl IntoRawFd for RawFd { |
| 62 | + #[inline] |
| 63 | + fn into_raw_fd(self) -> RawFd { |
| 64 | + self |
| 65 | + } |
| 66 | +} |
| 67 | +#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")] |
| 68 | +impl FromRawFd for RawFd { |
| 69 | + #[inline] |
| 70 | + unsafe fn from_raw_fd(fd: RawFd) -> RawFd { |
| 71 | + fd |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +macro_rules! impl_as_raw_fd { |
| 76 | + ($($t:ident)*) => {$( |
| 77 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 78 | + impl AsRawFd for net::$t { |
| 79 | + #[inline] |
| 80 | + fn as_raw_fd(&self) -> RawFd { |
| 81 | + *self.as_inner().socket().as_inner() |
| 82 | + } |
| 83 | + } |
| 84 | + )*}; |
| 85 | +} |
| 86 | +impl_as_raw_fd! { TcpStream TcpListener UdpSocket } |
| 87 | + |
| 88 | +macro_rules! impl_from_raw_fd { |
| 89 | + ($($t:ident)*) => {$( |
| 90 | + #[stable(feature = "from_raw_os", since = "1.1.0")] |
| 91 | + impl FromRawFd for net::$t { |
| 92 | + #[inline] |
| 93 | + unsafe fn from_raw_fd(fd: RawFd) -> net::$t { |
| 94 | + let socket = sys::net::Socket::from_inner(fd); |
| 95 | + net::$t::from_inner(sys_common::net::$t::from_inner(socket)) |
| 96 | + } |
| 97 | + } |
| 98 | + )*}; |
| 99 | +} |
| 100 | +impl_from_raw_fd! { TcpStream TcpListener UdpSocket } |
| 101 | + |
| 102 | +macro_rules! impl_into_raw_fd { |
| 103 | + ($($t:ident)*) => {$( |
| 104 | + #[stable(feature = "into_raw_os", since = "1.4.0")] |
| 105 | + impl IntoRawFd for net::$t { |
| 106 | + #[inline] |
| 107 | + fn into_raw_fd(self) -> RawFd { |
| 108 | + self.into_inner().into_socket().into_inner() |
| 109 | + } |
| 110 | + } |
| 111 | + )*}; |
| 112 | +} |
| 113 | +impl_into_raw_fd! { TcpStream TcpListener UdpSocket } |
0 commit comments