@@ -81,7 +81,13 @@ pub(crate) enum PollAt {
8181/// [AnySocket]: trait.AnySocket.html
8282/// [SocketSet::get]: struct.SocketSet.html#method.get
8383#[ derive( Debug ) ]
84- pub enum Socket < ' a > {
84+ pub struct Socket < ' a > {
85+ meta : SocketMeta ,
86+ pub ( crate ) socket : SocketEnum < ' a > ,
87+ }
88+
89+ #[ derive( Debug ) ]
90+ pub ( crate ) enum SocketEnum < ' a > {
8591 #[ cfg( feature = "socket-raw" ) ]
8692 Raw ( RawSocket < ' a > ) ,
8793 #[ cfg( all(
@@ -97,29 +103,6 @@ pub enum Socket<'a> {
97103 Dhcpv4 ( Dhcpv4Socket ) ,
98104}
99105
100- macro_rules! dispatch_socket {
101- ( $self_: expr, |$socket: ident| $code: expr) => {
102- dispatch_socket!( @inner $self_, |$socket| $code)
103- } ;
104- ( mut $self_: expr, |$socket: ident| $code: expr) => {
105- dispatch_socket!( @inner mut $self_, |$socket| $code)
106- } ;
107- ( @inner $( $mut_: ident ) * $self_: expr, |$socket: ident| $code: expr) => {
108- match $self_ {
109- #[ cfg( feature = "socket-raw" ) ]
110- & $( $mut_ ) * Socket :: Raw ( ref $( $mut_ ) * $socket) => $code,
111- #[ cfg( all( feature = "socket-icmp" , any( feature = "proto-ipv4" , feature = "proto-ipv6" ) ) ) ]
112- & $( $mut_ ) * Socket :: Icmp ( ref $( $mut_ ) * $socket) => $code,
113- #[ cfg( feature = "socket-udp" ) ]
114- & $( $mut_ ) * Socket :: Udp ( ref $( $mut_ ) * $socket) => $code,
115- #[ cfg( feature = "socket-tcp" ) ]
116- & $( $mut_ ) * Socket :: Tcp ( ref $( $mut_ ) * $socket) => $code,
117- #[ cfg( feature = "socket-dhcpv4" ) ]
118- & $( $mut_ ) * Socket :: Dhcpv4 ( ref $( $mut_ ) * $socket) => $code,
119- }
120- } ;
121- }
122-
123106impl < ' a > Socket < ' a > {
124107 /// Return the socket handle.
125108 #[ inline]
@@ -128,30 +111,52 @@ impl<'a> Socket<'a> {
128111 }
129112
130113 pub ( crate ) fn meta ( & self ) -> & SocketMeta {
131- dispatch_socket ! ( self , |socket| & socket . meta)
114+ & self . meta
132115 }
133116
134117 pub ( crate ) fn meta_mut ( & mut self ) -> & mut SocketMeta {
135- dispatch_socket ! ( mut self , |socket| & mut socket . meta)
118+ & mut self . meta
136119 }
137120
138121 pub ( crate ) fn poll_at ( & self , cx : & Context ) -> PollAt {
139- dispatch_socket ! ( self , |socket| socket. poll_at( cx) )
122+ match & self . socket {
123+ #[ cfg( feature = "socket-raw" ) ]
124+ SocketEnum :: Raw ( s) => s. poll_at ( cx) ,
125+ #[ cfg( all(
126+ feature = "socket-icmp" ,
127+ any( feature = "proto-ipv4" , feature = "proto-ipv6" )
128+ ) ) ]
129+ SocketEnum :: Icmp ( s) => s. poll_at ( cx) ,
130+ #[ cfg( feature = "socket-udp" ) ]
131+ SocketEnum :: Udp ( s) => s. poll_at ( cx) ,
132+ #[ cfg( feature = "socket-tcp" ) ]
133+ SocketEnum :: Tcp ( s) => s. poll_at ( cx) ,
134+ #[ cfg( feature = "socket-dhcpv4" ) ]
135+ SocketEnum :: Dhcpv4 ( s) => s. poll_at ( cx) ,
136+ }
140137 }
141138}
142139
143140/// A conversion trait for network sockets.
144141pub trait AnySocket < ' a > : Sized {
142+ fn upcast ( self ) -> Socket < ' a > ;
145143 fn downcast < ' c > ( socket : & ' c mut Socket < ' a > ) -> Option < & ' c mut Self > ;
146144}
147145
148146macro_rules! from_socket {
149147 ( $socket: ty, $variant: ident) => {
150148 impl <' a> AnySocket <' a> for $socket {
149+ fn upcast( self ) -> Socket <' a> {
150+ Socket {
151+ meta: SocketMeta :: default ( ) ,
152+ socket: SocketEnum :: $variant( self ) ,
153+ }
154+ }
155+
151156 fn downcast<' c>( socket: & ' c mut Socket <' a>) -> Option <& ' c mut Self > {
152157 #[ allow( unreachable_patterns) ]
153- match socket {
154- Socket :: $variant( socket) => Some ( socket) ,
158+ match & mut socket . socket {
159+ SocketEnum :: $variant( socket) => Some ( socket) ,
155160 _ => None ,
156161 }
157162 }
0 commit comments