1 | use crate::os::fd::owned::OwnedFd; |
2 | use crate::os::fd::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; |
3 | use crate::sys_common::{self, AsInner, FromInner, IntoInner}; |
4 | use crate::{net, sys}; |
5 | |
6 | macro_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 | } |
17 | impl_as_raw_fd! { TcpStream TcpListener UdpSocket } |
18 | |
19 | macro_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_common::net::$t::from_inner(socket)) |
28 | } |
29 | } |
30 | } |
31 | )*}; |
32 | } |
33 | impl_from_raw_fd! { TcpStream TcpListener UdpSocket } |
34 | |
35 | macro_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 | } |
46 | impl_into_raw_fd! { TcpStream TcpListener UdpSocket } |
47 | |