std/os/fd/
net.rs

1use crate::os::fd::owned::OwnedFd;
2use crate::os::fd::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
3use crate::sys_common::{AsInner, FromInner, IntoInner};
4use crate::{net, sys};
5
6macro_rules! impl_as_raw_fd {
7    ($($t:ident)*) => {$(
8        #[stable(feature = "rust1", since = "1.0.0")]
9        impl AsRawFd for net::$t {
10            #[inline]
11            fn as_raw_fd(&self) -> RawFd {
12                self.as_inner().socket().as_raw_fd()
13            }
14        }
15    )*};
16}
17impl_as_raw_fd! { TcpStream TcpListener UdpSocket }
18
19macro_rules! impl_from_raw_fd {
20    ($($t:ident)*) => {$(
21        #[stable(feature = "from_raw_os", since = "1.1.0")]
22        impl FromRawFd for net::$t {
23            #[inline]
24            unsafe fn from_raw_fd(fd: RawFd) -> net::$t {
25                unsafe {
26                    let socket = sys::net::Socket::from_inner(FromInner::from_inner(OwnedFd::from_raw_fd(fd)));
27                    net::$t::from_inner(sys::net::$t::from_inner(socket))
28                }
29            }
30        }
31    )*};
32}
33impl_from_raw_fd! { TcpStream TcpListener UdpSocket }
34
35macro_rules! impl_into_raw_fd {
36    ($($t:ident)*) => {$(
37        #[stable(feature = "into_raw_os", since = "1.4.0")]
38        impl IntoRawFd for net::$t {
39            #[inline]
40            fn into_raw_fd(self) -> RawFd {
41                self.into_inner().into_socket().into_inner().into_inner().into_raw_fd()
42            }
43        }
44    )*};
45}
46impl_into_raw_fd! { TcpStream TcpListener UdpSocket }