From 1c8f924ee7db06a5044a33591e1027211bab8db4 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 8 Nov 2023 21:27:09 +0100 Subject: [PATCH] Do not use heapless on public API. It's better to not use heapless on the public API. - It can get major bumps from time to time (for example v0.8 was just relased), and upgrading it is a breaking change of embedded-nal-async, which we should try to avoid. - It allows users to use any version of heapless, or something else entirely. --- Cargo.toml | 3 +-- embedded-nal-async/Cargo.toml | 1 - embedded-nal-async/src/dns.rs | 24 ++++++++++++++++++------ embedded-nal-async/src/lib.rs | 3 --- src/dns.rs | 16 ++++++++++------ src/lib.rs | 3 --- 6 files changed, 29 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c64acae..1526a9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,5 +23,4 @@ ip_in_core = [] [dependencies] nb = "1" -no-std-net = { version = "0.6", optional = true } -heapless = "^0.7" +no-std-net = { version = "0.6", optional = true } \ No newline at end of file diff --git a/embedded-nal-async/Cargo.toml b/embedded-nal-async/Cargo.toml index e5c0769..554f479 100644 --- a/embedded-nal-async/Cargo.toml +++ b/embedded-nal-async/Cargo.toml @@ -16,6 +16,5 @@ ip_in_core = [] [dependencies] no-std-net = "0.6" -heapless = "^0.7" embedded-nal = { version = "0.7.0", path = "../" } embedded-io-async = { version = "0.6.0" } diff --git a/embedded-nal-async/src/dns.rs b/embedded-nal-async/src/dns.rs index accb0bc..bbfdf2f 100644 --- a/embedded-nal-async/src/dns.rs +++ b/embedded-nal-async/src/dns.rs @@ -1,6 +1,5 @@ use crate::IpAddr; use embedded_nal::AddrType; -use heapless::String; /// This trait is an extension trait for [`TcpStack`] and [`UdpStack`] for dns /// resolutions. It does not handle every DNS record type, but is meant as an @@ -24,13 +23,22 @@ pub trait Dns { addr_type: AddrType, ) -> Result; - /// Resolve the hostname of a host, given its ip address + /// Resolve the hostname of a host, given its ip address. + /// + /// The hostname is stored at the beginning of `result`, the length is returned. + /// + /// If the buffer is too small to hold the domain name, an error should be returned. /// /// **Note**: A fully qualified domain name (FQDN), has a maximum length of - /// 255 bytes [`rfc1035`] + /// 255 bytes according to [`rfc1035`]. Therefore, you can pass a 255-byte long + /// buffer to guarantee it'll always be large enough. /// /// [`rfc1035`]: https://tools.ietf.org/html/rfc1035 - async fn get_host_by_address(&self, addr: IpAddr) -> Result, Self::Error>; + async fn get_host_by_address( + &self, + addr: IpAddr, + result: &mut [u8], + ) -> Result; } impl Dns for &T { @@ -44,7 +52,11 @@ impl Dns for &T { T::get_host_by_name(self, host, addr_type).await } - async fn get_host_by_address(&self, addr: IpAddr) -> Result, Self::Error> { - T::get_host_by_address(self, addr).await + async fn get_host_by_address( + &self, + addr: IpAddr, + result: &mut [u8], + ) -> Result { + T::get_host_by_address(self, addr, result).await } } diff --git a/embedded-nal-async/src/lib.rs b/embedded-nal-async/src/lib.rs index 5a33375..34beda3 100644 --- a/embedded-nal-async/src/lib.rs +++ b/embedded-nal-async/src/lib.rs @@ -9,9 +9,6 @@ mod dns; mod stack; -// Needed by embedded-nal trait implementers who build get_host_by_address results, or by trait -// users who pass the results on. -pub use heapless; #[cfg(feature = "ip_in_core")] pub use core::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; diff --git a/src/dns.rs b/src/dns.rs index b212656..2e7ff4d 100644 --- a/src/dns.rs +++ b/src/dns.rs @@ -1,5 +1,4 @@ use crate::IpAddr; -use heapless::String; /// This is the host address type to be returned by `gethostbyname`. /// @@ -37,13 +36,18 @@ pub trait Dns { addr_type: AddrType, ) -> nb::Result; - /// Resolve the hostname of a host, given its ip address + /// Resolve the hostname of a host, given its ip address. + /// + /// The hostname is stored at the beginning of `result`, the length is returned. + /// + /// If the buffer is too small to hold the domain name, an error should be returned. /// /// **Note**: A fully qualified domain name (FQDN), has a maximum length of - /// 255 bytes [`rfc1035`] + /// 255 bytes according to [`rfc1035`]. Therefore, you can pass a 255-byte long + /// buffer to guarantee it'll always be large enough. /// /// [`rfc1035`]: https://tools.ietf.org/html/rfc1035 - fn get_host_by_address(&mut self, addr: IpAddr) -> nb::Result, Self::Error>; + fn get_host_by_address(&self, addr: IpAddr, result: &mut [u8]) -> Result; } impl Dns for &mut T { @@ -57,7 +61,7 @@ impl Dns for &mut T { T::get_host_by_name(self, hostname, addr_type) } - fn get_host_by_address(&mut self, addr: IpAddr) -> nb::Result, Self::Error> { - T::get_host_by_address(self, addr) + fn get_host_by_address(&self, addr: IpAddr, result: &mut [u8]) -> Result { + T::get_host_by_address(self, addr, result) } } diff --git a/src/lib.rs b/src/lib.rs index fa48593..2114588 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,9 +8,6 @@ mod dns; mod stack; pub use nb; -// Needed by embedded-nal trait implementers who build get_host_by_address results, or by trait -// users who pass the results on. -pub use heapless; #[cfg(not(any(feature = "ip_in_core", feature = "no-std-net")))] compile_error!("You must select the ip_in_core feature or the no-std-net feature");