Skip to content

Commit bcf704e

Browse files
committed
udp_async: Implement async traits of upcoming async-embedded-nal > 0.3
See-Also: rust-embedded-community/embedded-nal#73
1 parent 40bf6aa commit bcf704e

File tree

6 files changed

+221
-130
lines changed

6 files changed

+221
-130
lines changed

Cargo.toml

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "std-embedded-nal"
33
version = "0.1.3"
44
authors = ["chrysn <[email protected]>"]
5-
edition = "2018"
5+
edition = "2021"
66

77
description = "Implementation of the `embedded-nal` traits for large devices that support the standard library"
88
categories = ["embedded", "hardware-support"]
@@ -12,15 +12,19 @@ repository = "https://gitlab.com/chrysn/std-embedded-nal"
1212

1313
[dependencies]
1414
embedded-nal = "0.6"
15-
embedded-nal-async = { version = "0.1", optional = true }
15+
embedded-nal-async = { git = "https://github.com/rust-embedded-community/embedded-nal", optional = true }
16+
embedded-io = { version = "0.4", optional = true, features = [ "std" ] }
1617
async-std = { version = "1.12", optional = true }
1718
# If enabled, these traits are implemented as well; they're experimental and
1819
# will hopefully wind up in alter embedded-nal versions, so enabling this has
1920
# no stability guarantees.
2021
embedded-nal-tcpextensions = { version = "0.1", optional = true }
2122

23+
async-io = "^1.9"
24+
nix = { version = "0.25", features = [ "socket", "net", "uio" ] }
25+
2226
[features]
23-
async = [ "embedded-nal-async", "async-std" ]
27+
async = [ "embedded-nal-async", "async-std", "embedded-io" ]
2428

2529
[dev-dependencies]
2630
mio = { version = "0.8", features = [ "os-ext" ] }

examples/coapclient_async.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
//! A brutally oversimplified CoAP client that GETs /.well-known/core from localhost:5683
22
3-
use embedded_nal_async::UdpClientStack;
3+
use embedded_nal_async::{UdpStack, ConnectedUdp};
44

55
async fn run<S, E>(stack: &mut S) -> Result<(), E>
66
where
77
E: core::fmt::Debug, // Might go away when MSRV goes up to 1.49, see https://github.com/rust-lang/rust/issues/80821
8-
S: UdpClientStack<Error = E>,
8+
S: UdpStack<Error = E>,
9+
S::Connected: ConnectedUdp<Error = E>,
910
{
1011
let target = embedded_nal::SocketAddr::new(
1112
"::1".parse().unwrap(),
1213
5683,
1314
);
1415

15-
let mut sock = stack.socket().await?;
16-
stack.connect(&mut sock, target).await?;
16+
let (_local, mut sock) = stack.connect(target).await?;
1717
// Data, V1 NON no token, GET, message ID 0x0000, 2x Uri-Path
18-
stack.send(&mut sock, b"\x50\x01\0\0\xbb.well-known\x04core").await?;
18+
sock.send(b"\x50\x01\0\0\xbb.well-known\x04core").await?;
1919

2020
let mut respbuf = [0; 1500];
21-
let (resplen, _) = stack.receive(&mut sock, &mut respbuf).await?;
21+
let resplen = sock.receive_into(&mut respbuf).await?;
2222
let response = &respbuf[..resplen];
2323

2424
println!("Response: {}", String::from_utf8_lossy(response));

src/conversion.rs

+25
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,31 @@ impl From<embedded_nal::IpAddr> for IpAddr {
2626
}
2727
}
2828

29+
// That this is missing zone info on IPv6 probably just makes std::net::IpAddr a bad intermediate
30+
// type.
31+
impl From<nix::libc::in6_pktinfo> for IpAddr {
32+
fn from(input: nix::libc::in6_pktinfo) -> Self {
33+
// FIXME why isn't this having zone infos??
34+
Self(input.ipi6_addr.s6_addr.into())
35+
}
36+
}
37+
38+
impl From<IpAddr> for nix::libc::in6_pktinfo {
39+
fn from(input: IpAddr) -> nix::libc::in6_pktinfo {
40+
let input = match input.0 {
41+
std::net::IpAddr::V6(a) => a,
42+
_ => panic!("IPv6 only so far"),
43+
};
44+
nix::libc::in6_pktinfo {
45+
ipi6_addr: nix::libc::in6_addr {
46+
s6_addr: input.octets(),
47+
},
48+
// FIXME and here it really hurts
49+
ipi6_ifindex: 0,
50+
}
51+
}
52+
}
53+
2954
impl From<IpAddr> for embedded_nal::IpAddr {
3055
fn from(s: IpAddr) -> embedded_nal::IpAddr {
3156
match s.0 {

src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
//! All implementations use `std::io::Error` as their error type.
1010
//!
1111
//! [embedded-nal]: https://crates.io/crates/embedded-nal
12-
#![cfg_attr(feature = "async", feature(generic_associated_types))] // for implementing -async
13-
#![cfg_attr(feature = "async", feature(type_alias_impl_trait))] // as async blocks
14-
// don't provide types
12+
#![cfg_attr(feature = "async", feature(async_fn_in_trait))]
1513

1614
mod conversion;
1715
mod dns;

0 commit comments

Comments
 (0)