From 6e68353a931cc268f61b4606c3a99bf8f5e96f6e Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Fri, 10 Feb 2023 18:30:17 +0100 Subject: [PATCH] attempt removing option --- embassy-net/src/lib.rs | 76 ++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 48 deletions(-) diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs index 5b6ab0e3a..627c0a0f7 100644 --- a/embassy-net/src/lib.rs +++ b/embassy-net/src/lib.rs @@ -53,7 +53,7 @@ const MAX_QUERIES: usize = 2; pub struct StackResources { sockets: [SocketStorage<'static>; SOCK], #[cfg(feature = "dns")] - queries: Option<[Option; MAX_QUERIES]>, + queries: [Option; MAX_QUERIES], } impl StackResources { @@ -63,7 +63,7 @@ impl StackResources { Self { sockets: [SocketStorage::EMPTY; SOCK], #[cfg(feature = "dns")] - queries: Some([INIT; MAX_QUERIES]), + queries: [INIT; MAX_QUERIES], } } } @@ -117,7 +117,7 @@ struct Inner { #[cfg(feature = "dhcpv4")] dhcp_socket: Option, #[cfg(feature = "dns")] - dns_socket: Option, + dns_socket: SocketHandle, } pub(crate) struct SocketStack { @@ -156,15 +156,7 @@ impl Stack { let next_local_port = (random_seed % (LOCAL_PORT_MAX - LOCAL_PORT_MIN) as u64) as u16 + LOCAL_PORT_MIN; - let mut inner = Inner { - device, - link_up: false, - config: None, - #[cfg(feature = "dhcpv4")] - dhcp_socket: None, - #[cfg(feature = "dns")] - dns_socket: None, - }; + let mut socket = SocketStack { sockets, iface, @@ -172,12 +164,15 @@ impl Stack { next_local_port, }; - #[cfg(feature = "dns")] - { - if let Some(queries) = resources.queries.take() { - inner.dns_socket = Some(socket.sockets.add(dns::Socket::new(&[], queries))); - } - } + let mut inner = Inner { + device, + link_up: false, + config: None, + #[cfg(feature = "dhcpv4")] + dhcp_socket: None, + #[cfg(feature = "dns")] + dns_socket: socket.sockets.add(dns::Socket::new(&[], &mut resources.queries)), + }; match config { Config::Static(config) => { @@ -235,42 +230,29 @@ impl Stack { #[cfg(feature = "dns")] pub async fn dns_query(&self, name: &str, qtype: dns::DnsQueryType) -> Result, dns::Error> { let query = self.with_mut(|s, i| { - if let Some(dns_handle) = i.dns_socket { - let socket = s.sockets.get_mut::(dns_handle); - match socket.start_query(s.iface.context(), name, qtype) { - Ok(handle) => Ok(handle), - Err(e) => Err(e.into()), - } - } else { - Err(dns::Error::Failed) - } + let socket = s.sockets.get_mut::(i.dns_socket); + socket.start_query(s.iface.context(), name, qtype) })?; use embassy_hal_common::drop::OnDrop; let drop = OnDrop::new(|| { self.with_mut(|s, i| { - if let Some(dns_handle) = i.dns_socket { - let socket = s.sockets.get_mut::(dns_handle); - socket.cancel_query(query); - s.waker.wake(); - } + let socket = s.sockets.get_mut::(i.dns_socket); + socket.cancel_query(query); + s.waker.wake(); }) }); let res = poll_fn(|cx| { self.with_mut(|s, i| { - if let Some(dns_handle) = i.dns_socket { - let socket = s.sockets.get_mut::(dns_handle); - match socket.get_query_result(query) { - Ok(addrs) => Poll::Ready(Ok(addrs)), - Err(dns::GetQueryResultError::Pending) => { - socket.register_query_waker(query, cx.waker()); - Poll::Pending - } - Err(e) => Poll::Ready(Err(e.into())), + let socket = s.sockets.get_mut::(i.dns_socket); + match socket.get_query_result(query) { + Ok(addrs) => Poll::Ready(Ok(addrs)), + Err(dns::GetQueryResultError::Pending) => { + socket.register_query_waker(query, cx.waker()); + Poll::Pending } - } else { - Poll::Ready(Err(dns::Error::Failed)) + Err(e) => Poll::Ready(Err(e.into())), } }) }) @@ -322,11 +304,9 @@ impl Inner { } #[cfg(feature = "dns")] - if let Some(dns_socket) = self.dns_socket { - let socket = s.sockets.get_mut::(dns_socket); - let servers: Vec = config.dns_servers.iter().map(|c| IpAddress::Ipv4(*c)).collect(); - socket.update_servers(&servers[..]); - } + let socket = s.sockets.get_mut::(self.dns_socket); + let servers: Vec = config.dns_servers.iter().map(|c| IpAddress::Ipv4(*c)).collect(); + socket.update_servers(&servers[..]); self.config = Some(config) }