Skip to content

Commit

Permalink
Merge pull request #93 from Dirbaio/no-heapless
Browse files Browse the repository at this point in the history
async: Do not use heapless on public API.
  • Loading branch information
ryan-summers authored Nov 9, 2023
2 parents e0492ef + fb218be commit 65ba5a3
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Bump MSRV to 1.60.0 (required for Edition 2021)
- Switch to Edition 2021
- [breaking] `Dns::get_host_by_address` now uses `&mut [u8]` instead of `heapless::String`.

## [0.7.0] - 2023-06-21

Expand Down
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
2 changes: 1 addition & 1 deletion embedded-nal-async/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

No unreleased changes yet.
- [breaking] `Dns::get_host_by_address` now uses `&mut [u8]` instead of `heapless::String`.

## [0.6.0] - 2023-10-03

Expand Down
1 change: 0 additions & 1 deletion embedded-nal-async/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
24 changes: 18 additions & 6 deletions embedded-nal-async/src/dns.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,13 +23,22 @@ pub trait Dns {
addr_type: AddrType,
) -> Result<IpAddr, Self::Error>;

/// 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<String<256>, Self::Error>;
async fn get_host_by_address(
&self,
addr: IpAddr,
result: &mut [u8],
) -> Result<usize, Self::Error>;
}

impl<T: Dns> Dns for &T {
Expand All @@ -44,7 +52,11 @@ impl<T: Dns> Dns for &T {
T::get_host_by_name(self, host, addr_type).await
}

async fn get_host_by_address(&self, addr: IpAddr) -> Result<String<256>, Self::Error> {
T::get_host_by_address(self, addr).await
async fn get_host_by_address(
&self,
addr: IpAddr,
result: &mut [u8],
) -> Result<usize, Self::Error> {
T::get_host_by_address(self, addr, result).await
}
}
3 changes: 0 additions & 3 deletions embedded-nal-async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
16 changes: 10 additions & 6 deletions src/dns.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::IpAddr;
use heapless::String;

/// This is the host address type to be returned by `gethostbyname`.
///
Expand Down Expand Up @@ -37,13 +36,18 @@ pub trait Dns {
addr_type: AddrType,
) -> nb::Result<IpAddr, Self::Error>;

/// 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<String<256>, Self::Error>;
fn get_host_by_address(&self, addr: IpAddr, result: &mut [u8]) -> Result<usize, Self::Error>;
}

impl<T: Dns> Dns for &mut T {
Expand All @@ -57,7 +61,7 @@ impl<T: Dns> Dns for &mut T {
T::get_host_by_name(self, hostname, addr_type)
}

fn get_host_by_address(&mut self, addr: IpAddr) -> nb::Result<String<256>, Self::Error> {
T::get_host_by_address(self, addr)
fn get_host_by_address(&self, addr: IpAddr, result: &mut [u8]) -> Result<usize, Self::Error> {
T::get_host_by_address(self, addr, result)
}
}
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 65ba5a3

Please sign in to comment.