From 5185340e9583040c590415d5e157c67ba706b045 Mon Sep 17 00:00:00 2001 From: chrysn Date: Mon, 19 Aug 2024 11:00:07 +0200 Subject: [PATCH] Fix building with the various embedded-nal This makes implicit what caused breakage in b99e8085: embedded-nal{,-async}'s socket types are re-exports of no_std_net (and thus public dependencies). Conversions are now implemented explicitly for the relevant no_std_net versions. --- Cargo.toml | 9 +++- src/socket.rs | 129 +++++++++++++++++++++++++++----------------------- 2 files changed, 77 insertions(+), 61 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2af6f8f9..718a5c8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,11 @@ embedded-io-async = { version = "0.6", optional = true } pin-utils = "0.1" pin-project = "1.0.11" +# as used in embedded-nal 0.6 +no-std-net-0-5 = { package = "no-std-net", version = "0.5", optional = true } +# as used in embedded-nal-async +no-std-net-0-6 = { package = "no-std-net", version = "0.6", optional = true } + embedded-hal-async = { version = "1", optional = true } critical-section = { version = "1.0", optional = true } @@ -81,8 +86,8 @@ provide_critical_section_1_0 = ["critical-section/restore-state-u32"] with_coap_message = [] with_coap_handler = [] -with_embedded_nal = ["embedded-nal", "embedded-nal-tcpextensions"] -with_embedded_nal_async = [ "embedded-io-async", "embedded-nal-async-0-7" ] +with_embedded_nal = ["embedded-nal", "embedded-nal-tcpextensions", "no-std-net-0-5"] +with_embedded_nal_async = [ "embedded-io-async", "embedded-nal-async-0-7", "no-std-net-0-6" ] with_embedded_hal_async = [ "embedded-hal-async" ] diff --git a/src/socket.rs b/src/socket.rs index 16d871d7..a1207e65 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -64,73 +64,84 @@ impl Into for UdpEp { } } -#[cfg(feature = "with_embedded_nal")] -mod nal_impls { - use super::*; - use embedded_nal::SocketAddr; - - impl Into for &SocketAddr { - fn into(self) -> UdpEp { - use SocketAddr::*; - - // Constructing via default avoids using the volatile names of the union types - let mut ep: riot_sys::sock_udp_ep_t = Default::default(); - - ep.family = match self { - V4(_) => riot_sys::AF_INET as _, - V6(_) => riot_sys::AF_INET6 as _, - }; - ep.netif = match self { - V4(_) => 0, - V6(a) => a.scope_id() as _, - }; - ep.port = self.port(); - match self { - V4(a) => { - ep.addr.ipv4 = a.ip().octets(); - } - V6(a) => { - ep.addr.ipv6 = a.ip().octets(); +macro_rules! implementation_no_std_net { + ($nsn_crate:ident) => { + use super::*; + use $nsn_crate::SocketAddr; + + impl Into for &SocketAddr { + fn into(self) -> UdpEp { + use SocketAddr::*; + + // Constructing via default avoids using the volatile names of the union types + let mut ep: riot_sys::sock_udp_ep_t = Default::default(); + + ep.family = match self { + V4(_) => riot_sys::AF_INET as _, + V6(_) => riot_sys::AF_INET6 as _, + }; + ep.netif = match self { + V4(_) => 0, + V6(a) => a.scope_id() as _, + }; + ep.port = self.port(); + match self { + V4(a) => { + ep.addr.ipv4 = a.ip().octets(); + } + V6(a) => { + ep.addr.ipv6 = a.ip().octets(); + } } - } - UdpEp(ep) + UdpEp(ep) + } } - } - impl Into for SocketAddr { - fn into(self) -> UdpEp { - (&self).into() + impl Into for SocketAddr { + fn into(self) -> UdpEp { + (&self).into() + } } - } - impl Into for &UdpEp { - fn into(self) -> SocketAddr { - match self.0.family as _ { - riot_sys::AF_INET6 => embedded_nal::SocketAddrV6::new( - // unsafe: Access to C union whose type was just checked - unsafe { self.0.addr.ipv6.into() }, - self.0.port, - 0, - self.0.netif.into(), - ) - .into(), - - riot_sys::AF_INET => embedded_nal::SocketAddrV4::new( - // unsafe: Access to C union whose type was just checked - unsafe { self.0.addr.ipv4.into() }, - self.0.port, - ) - .into(), - - _ => panic!("Endpoint not expressible in embedded_nal"), + impl Into for &UdpEp { + fn into(self) -> SocketAddr { + match self.0.family as _ { + riot_sys::AF_INET6 => $nsn_crate::SocketAddrV6::new( + // unsafe: Access to C union whose type was just checked + unsafe { self.0.addr.ipv6.into() }, + self.0.port, + 0, + self.0.netif.into(), + ) + .into(), + + riot_sys::AF_INET => $nsn_crate::SocketAddrV4::new( + // unsafe: Access to C union whose type was just checked + unsafe { self.0.addr.ipv4.into() }, + self.0.port, + ) + .into(), + + _ => panic!("Endpoint not expressible in no_std_net"), + } } } - } - impl Into for UdpEp { - fn into(self) -> SocketAddr { - (&self).into() + impl Into for UdpEp { + fn into(self) -> SocketAddr { + (&self).into() + } } - } + }; +} + +#[cfg(feature = "with_embedded_nal")] +mod implementation_no_std_net_0_5 { + implementation_no_std_net! {no_std_net_0_5} +} + +#[cfg(feature = "with_embedded_nal_async")] +mod implementation_no_std_net_0_6 { + implementation_no_std_net! {no_std_net_0_6} }