Skip to content

Commit

Permalink
Fix building with the various embedded-nal
Browse files Browse the repository at this point in the history
This makes implicit what caused breakage in b99e808:
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.
  • Loading branch information
chrysn committed Aug 19, 2024
1 parent 5e06b9a commit 5185340
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 61 deletions.
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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" ]

Expand Down
129 changes: 70 additions & 59 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,73 +64,84 @@ impl Into<riot_sys::sock_udp_ep_t> for UdpEp {
}
}

#[cfg(feature = "with_embedded_nal")]
mod nal_impls {
use super::*;
use embedded_nal::SocketAddr;

impl Into<UdpEp> 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<UdpEp> 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<UdpEp> for SocketAddr {
fn into(self) -> UdpEp {
(&self).into()
impl Into<UdpEp> for SocketAddr {
fn into(self) -> UdpEp {
(&self).into()
}
}
}

impl Into<SocketAddr> 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<SocketAddr> 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<SocketAddr> for UdpEp {
fn into(self) -> SocketAddr {
(&self).into()
impl Into<SocketAddr> 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}
}

0 comments on commit 5185340

Please sign in to comment.