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