Skip to content

Commit 4b2f6dc

Browse files
committed
socket: move meta from XxxSocket to Socket.
1 parent 0dfe66b commit 4b2f6dc

File tree

9 files changed

+118
-263
lines changed

9 files changed

+118
-263
lines changed

src/iface/interface.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,7 @@ where
473473
///
474474
/// # Panics
475475
/// This function panics if the storage is fixed-size (not a `Vec`) and is full.
476-
pub fn add_socket<T>(&mut self, socket: T) -> SocketHandle
477-
where
478-
T: Into<Socket<'a>>,
479-
{
476+
pub fn add_socket<T: AnySocket<'a>>(&mut self, socket: T) -> SocketHandle {
480477
self.sockets.add(socket)
481478
}
482479

@@ -861,16 +858,16 @@ where
861858
}};
862859
}
863860

864-
let socket_result = match *socket {
861+
let socket_result = match &mut socket.socket {
865862
#[cfg(feature = "socket-raw")]
866-
Socket::Raw(ref mut socket) => {
863+
SocketEnum::Raw(socket) => {
867864
socket.dispatch(cx, |response| respond!(IpPacket::Raw(response)))
868865
}
869866
#[cfg(all(
870867
feature = "socket-icmp",
871868
any(feature = "proto-ipv4", feature = "proto-ipv6")
872869
))]
873-
Socket::Icmp(ref mut socket) => socket.dispatch(cx, |response| match response {
870+
SocketEnum::Icmp(socket) => socket.dispatch(cx, |response| match response {
874871
#[cfg(feature = "proto-ipv4")]
875872
(IpRepr::Ipv4(ipv4_repr), IcmpRepr::Ipv4(icmpv4_repr)) => {
876873
respond!(IpPacket::Icmpv4((ipv4_repr, icmpv4_repr)))
@@ -882,15 +879,15 @@ where
882879
_ => Err(Error::Unaddressable),
883880
}),
884881
#[cfg(feature = "socket-udp")]
885-
Socket::Udp(ref mut socket) => {
882+
SocketEnum::Udp(socket) => {
886883
socket.dispatch(cx, |response| respond!(IpPacket::Udp(response)))
887884
}
888885
#[cfg(feature = "socket-tcp")]
889-
Socket::Tcp(ref mut socket) => {
886+
SocketEnum::Tcp(socket) => {
890887
socket.dispatch(cx, |response| respond!(IpPacket::Tcp(response)))
891888
}
892889
#[cfg(feature = "socket-dhcpv4")]
893-
Socket::Dhcpv4(ref mut socket) => {
890+
SocketEnum::Dhcpv4(socket) => {
894891
socket.dispatch(cx, |response| respond!(IpPacket::Dhcpv4(response)))
895892
}
896893
};

src/socket/dhcpv4.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::socket::SocketHandle;
2-
use crate::socket::{Context, SocketMeta};
1+
use crate::socket::Context;
32
use crate::time::{Duration, Instant};
43
use crate::wire::dhcpv4::field as dhcpv4_field;
54
use crate::wire::HardwareAddress;
@@ -9,7 +8,7 @@ use crate::wire::{
98
};
109
use crate::{Error, Result};
1110

12-
use super::{PollAt, Socket};
11+
use super::PollAt;
1312

1413
const DISCOVER_TIMEOUT: Duration = Duration::from_secs(10);
1514

@@ -112,7 +111,6 @@ pub enum Event {
112111

113112
#[derive(Debug)]
114113
pub struct Dhcpv4Socket {
115-
pub(crate) meta: SocketMeta,
116114
/// State of the DHCP client.
117115
state: ClientState,
118116
/// Set to true on config/state change, cleared back to false by the `config` function.
@@ -138,7 +136,6 @@ impl Dhcpv4Socket {
138136
#[allow(clippy::new_without_default)]
139137
pub fn new() -> Self {
140138
Dhcpv4Socket {
141-
meta: SocketMeta::default(),
142139
state: ClientState::Discovering(DiscoverState {
143140
retry_at: Instant::from_millis(0),
144141
}),
@@ -520,12 +517,6 @@ impl Dhcpv4Socket {
520517
}
521518
}
522519

523-
/// Return the socket handle.
524-
#[inline]
525-
pub fn handle(&self) -> SocketHandle {
526-
self.meta.handle
527-
}
528-
529520
/// Reset state and restart discovery phase.
530521
///
531522
/// Use this to speed up acquisition of an address in a new
@@ -557,12 +548,6 @@ impl Dhcpv4Socket {
557548
}
558549
}
559550

560-
impl<'a> From<Dhcpv4Socket> for Socket<'a> {
561-
fn from(val: Dhcpv4Socket) -> Self {
562-
Socket::Dhcpv4(val)
563-
}
564-
}
565-
566551
#[cfg(test)]
567552
mod test {
568553

src/socket/icmp.rs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::task::Waker;
55
use crate::phy::ChecksumCapabilities;
66
#[cfg(feature = "async")]
77
use crate::socket::WakerRegistration;
8-
use crate::socket::{Context, PollAt, Socket, SocketHandle, SocketMeta};
8+
use crate::socket::{Context, PollAt};
99
use crate::storage::{PacketBuffer, PacketMetadata};
1010
use crate::{Error, Result};
1111

@@ -62,7 +62,6 @@ pub type IcmpSocketBuffer<'a> = PacketBuffer<'a, IpAddress>;
6262
/// [bind]: #method.bind
6363
#[derive(Debug)]
6464
pub struct IcmpSocket<'a> {
65-
pub(crate) meta: SocketMeta,
6665
rx_buffer: IcmpSocketBuffer<'a>,
6766
tx_buffer: IcmpSocketBuffer<'a>,
6867
/// The endpoint this socket is communicating with
@@ -79,7 +78,6 @@ impl<'a> IcmpSocket<'a> {
7978
/// Create an ICMP socket with the given buffers.
8079
pub fn new(rx_buffer: IcmpSocketBuffer<'a>, tx_buffer: IcmpSocketBuffer<'a>) -> IcmpSocket<'a> {
8180
IcmpSocket {
82-
meta: SocketMeta::default(),
8381
rx_buffer: rx_buffer,
8482
tx_buffer: tx_buffer,
8583
endpoint: Endpoint::default(),
@@ -126,12 +124,6 @@ impl<'a> IcmpSocket<'a> {
126124
self.tx_waker.register(waker)
127125
}
128126

129-
/// Return the socket handle.
130-
#[inline]
131-
pub fn handle(&self) -> SocketHandle {
132-
self.meta.handle
133-
}
134-
135127
/// Return the time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
136128
///
137129
/// See also the [set_hop_limit](#method.set_hop_limit) method
@@ -290,12 +282,7 @@ impl<'a> IcmpSocket<'a> {
290282

291283
let packet_buf = self.tx_buffer.enqueue(size, endpoint)?;
292284

293-
net_trace!(
294-
"{}:{}: buffer to send {} octets",
295-
self.meta.handle,
296-
endpoint,
297-
size
298-
);
285+
net_trace!("icmp:{}: buffer to send {} octets", endpoint, size);
299286
Ok(packet_buf)
300287
}
301288

@@ -316,8 +303,7 @@ impl<'a> IcmpSocket<'a> {
316303
let (endpoint, packet_buf) = self.rx_buffer.dequeue()?;
317304

318305
net_trace!(
319-
"{}:{}: receive {} buffered octets",
320-
self.meta.handle,
306+
"icmp:{}: receive {} buffered octets",
321307
endpoint,
322308
packet_buf.len()
323309
);
@@ -417,8 +403,7 @@ impl<'a> IcmpSocket<'a> {
417403
);
418404

419405
net_trace!(
420-
"{}:{}: receiving {} octets",
421-
self.meta.handle,
406+
"icmp:{}: receiving {} octets",
422407
icmp_repr.buffer_len(),
423408
packet_buf.len()
424409
);
@@ -436,8 +421,7 @@ impl<'a> IcmpSocket<'a> {
436421
);
437422

438423
net_trace!(
439-
"{}:{}: receiving {} octets",
440-
self.meta.handle,
424+
"icmp:{}: receiving {} octets",
441425
icmp_repr.buffer_len(),
442426
packet_buf.len()
443427
);
@@ -454,12 +438,10 @@ impl<'a> IcmpSocket<'a> {
454438
where
455439
F: FnOnce((IpRepr, IcmpRepr)) -> Result<()>,
456440
{
457-
let handle = self.meta.handle;
458441
let hop_limit = self.hop_limit.unwrap_or(64);
459442
self.tx_buffer.dequeue_with(|remote_endpoint, packet_buf| {
460443
net_trace!(
461-
"{}:{}: sending {} octets",
462-
handle,
444+
"icmp:{}: sending {} octets",
463445
remote_endpoint,
464446
packet_buf.len()
465447
);
@@ -515,12 +497,6 @@ impl<'a> IcmpSocket<'a> {
515497
}
516498
}
517499

518-
impl<'a> From<IcmpSocket<'a>> for Socket<'a> {
519-
fn from(val: IcmpSocket<'a>) -> Self {
520-
Socket::Icmp(val)
521-
}
522-
}
523-
524500
#[cfg(test)]
525501
mod tests_common {
526502
pub use super::*;

src/socket/meta.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl Default for NeighborState {
3131
/// is interested in, but which are more conveniently stored inside the socket itself.
3232
#[derive(Debug, Default)]
3333
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
34-
pub struct Meta {
34+
pub(crate) struct Meta {
3535
/// Handle of this socket within its enclosing `SocketSet`.
3636
/// Mainly useful for debug output.
3737
pub(crate) handle: SocketHandle,

src/socket/mod.rs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
123106
impl<'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.
144141
pub 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

148146
macro_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

Comments
 (0)