Skip to content

Commit

Permalink
async: Add support for embedded-nal-async 0.7
Browse files Browse the repository at this point in the history
This is done by means of a macro that duplicates the code, for none of
the breaking changes from 0.6 to 0.7 affect this crate's code, and we
can't just drop 0.6 support without a breaking change.
  • Loading branch information
chrysn committed Jan 30, 2024
1 parent 0cefea1 commit 343383f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ coap-handler-0-2 = { package = "coap-handler", version = "^0.2.0" }
embedded-nal = { version = "0.6.0", optional = true }
embedded-nal-tcpextensions = { version = "0.1", optional = true }
embedded-nal-async = { version = "0.6", optional = true }
embedded-nal-async-0-7 = { package = "embedded-nal-async", version = "0.7.1", optional = true }
embedded-io-async = { version = "0.6", optional = true }
pin-utils = "0.1"
pin-project = "1.0.11"
Expand Down Expand Up @@ -84,7 +85,7 @@ with_coap_message = []
with_coap_handler = []

with_embedded_nal = ["embedded-nal", "embedded-nal-tcpextensions"]
with_embedded_nal_async = [ "embedded-nal-async", "embedded-io-async" ]
with_embedded_nal_async = [ "embedded-nal-async", "embedded-io-async", "embedded-nal-async-0-7" ]

with_embedded_hal_async = [ "embedded-hal-async" ]

Expand Down
43 changes: 27 additions & 16 deletions src/socket_embedded_nal_async_udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ pub struct UnconnectedUdpSocket {
socket: &'static mut sock_udp_t,
}

mod implementation {
macro_rules! implementation_module {
($ena_crate:ident) => {

use super::*;

impl embedded_nal_async::UdpStack for UdpStack {
impl $ena_crate::UdpStack for UdpStack {
type Error = NumericError;
type Connected = ConnectedUdpSocket;
// This could be done more efficiently (in particular, the send of UniquelyBound wouldn't need
Expand All @@ -90,9 +92,9 @@ mod implementation {

async fn connect_from(
&self,
local: embedded_nal_async::SocketAddr,
remote: embedded_nal_async::SocketAddr,
) -> Result<(embedded_nal_async::SocketAddr, Self::Connected), Self::Error> {
local: $ena_crate::SocketAddr,
remote: $ena_crate::SocketAddr,
) -> Result<($ena_crate::SocketAddr, Self::Connected), Self::Error> {
let mut socket = self.create(Some(local.into()), Some(remote.into()), 0)?;

let final_local = get_local(&mut socket)?;
Expand All @@ -107,8 +109,8 @@ mod implementation {

async fn bind_single(
&self,
local: embedded_nal_async::SocketAddr,
) -> Result<(embedded_nal_async::SocketAddr, Self::UniquelyBound), Self::Error> {
local: $ena_crate::SocketAddr,
) -> Result<($ena_crate::SocketAddr, Self::UniquelyBound), Self::Error> {
let mut socket = self.create(Some(local.into()), None, 0)?;

let final_local = get_local(&mut socket)?;
Expand All @@ -118,15 +120,15 @@ mod implementation {

async fn bind_multiple(
&self,
local: embedded_nal_async::SocketAddr,
local: $ena_crate::SocketAddr,
) -> Result<Self::MultiplyBound, Self::Error> {
let mut socket = self.create(Some(local.into()), None, 0)?;

Ok(UnconnectedUdpSocket { socket })
}
}

impl embedded_nal_async::ConnectedUdp for ConnectedUdpSocket {
impl $ena_crate::ConnectedUdp for ConnectedUdpSocket {
type Error = NumericError;

async fn send(&mut self, data: &[u8]) -> Result<(), Self::Error> {
Expand Down Expand Up @@ -154,13 +156,13 @@ mod implementation {
}
}

impl embedded_nal_async::UnconnectedUdp for UnconnectedUdpSocket {
impl $ena_crate::UnconnectedUdp for UnconnectedUdpSocket {
type Error = NumericError;

async fn send(
&mut self,
local: embedded_nal_async::SocketAddr,
remote: embedded_nal_async::SocketAddr,
local: $ena_crate::SocketAddr,
remote: $ena_crate::SocketAddr,
data: &[u8],
) -> Result<(), Self::Error> {
let remote: UdpEp = remote.into();
Expand Down Expand Up @@ -188,8 +190,8 @@ mod implementation {
) -> Result<
(
usize,
embedded_nal_async::SocketAddr,
embedded_nal_async::SocketAddr,
$ena_crate::SocketAddr,
$ena_crate::SocketAddr,
),
Self::Error,
> {
Expand All @@ -209,8 +211,8 @@ mod implementation {
type Output = Result<
(
usize,
embedded_nal_async::SocketAddr,
embedded_nal_async::SocketAddr,
$ena_crate::SocketAddr,
$ena_crate::SocketAddr,
),
NumericError,
>;
Expand Down Expand Up @@ -340,4 +342,13 @@ mod implementation {
// any more.
}
}

}
}

mod implementation {
implementation_module! {embedded_nal_async}
}
mod implementation_0_7 {
implementation_module! {embedded_nal_async_0_7}
}

0 comments on commit 343383f

Please sign in to comment.