Skip to content

Commit a088870

Browse files
authored
Merge pull request #69 from lulf/shared-stack
TcpClient trait for creating shared TCP/IP stack
2 parents e08c8d4 + be1b46a commit a088870

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

embedded-nal-async/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
TcpClient trait for creating shared async TCP/IP stack implementations.
11+
1012
## [0.1.0] - 2022-05-04
1113

1214
Initial release to crates.io.

embedded-nal-async/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ categories = ["embedded", "hardware-support", "no-std", "network-programming", "
1818
no-std-net = "0.5"
1919
heapless = "^0.7"
2020
embedded-nal = { version = "0.6.0", path = "../" }
21+
embedded-io = { version = "0.3.0", default-features = false, features = ["async"] }

embedded-nal-async/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ pub use no_std_net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, Socke
1414

1515
pub use dns::Dns;
1616
pub use embedded_nal::AddrType;
17-
pub use stack::{TcpClientStack, TcpFullStack, UdpClientStack, UdpFullStack};
17+
pub use stack::{TcpClientStack, TcpConnect, TcpFullStack, UdpClientStack, UdpFullStack};

embedded-nal-async/src/stack/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod tcp;
22
mod udp;
33

4-
pub use tcp::{TcpClientStack, TcpFullStack};
4+
pub use tcp::{TcpClientStack, TcpConnect, TcpFullStack};
55
pub use udp::{UdpClientStack, UdpFullStack};

embedded-nal-async/src/stack/tcp.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,39 @@ impl<T: TcpClientStack> TcpClientStack for &mut T {
170170
T::close(self, socket)
171171
}
172172
}
173+
174+
/// This trait is implemented by TCP/IP stacks. The trait allows the underlying driver to
175+
/// construct multiple connections that implement the I/O traits from embedded-io.
176+
///
177+
/// The associated connection type should close the connection when dropped.
178+
pub trait TcpConnect {
179+
/// Error type returned on connect failure.
180+
type Error: embedded_io::Error;
181+
182+
/// Type holding state of a TCP connection. Should close the connection when dropped.
183+
type Connection<'m>: embedded_io::asynch::Read<Error = Self::Error>
184+
+ embedded_io::asynch::Write<Error = Self::Error>
185+
where
186+
Self: 'm;
187+
/// Future returned by `connect` function.
188+
type ConnectFuture<'m>: Future<Output = Result<Self::Connection<'m>, Self::Error>> + 'm
189+
where
190+
Self: 'm;
191+
192+
/// Connect to the given remote host and port.
193+
///
194+
/// Returns `Ok` if the connection was successful.
195+
fn connect<'m>(&'m self, remote: SocketAddr) -> Self::ConnectFuture<'m>;
196+
}
197+
198+
impl<T: TcpConnect> TcpConnect for &T {
199+
type Error = T::Error;
200+
201+
type Connection<'m> = T::Connection<'m> where Self: 'm;
202+
203+
type ConnectFuture<'m> = T::ConnectFuture<'m> where Self: 'm;
204+
205+
fn connect<'m>(&'m self, remote: SocketAddr) -> Self::ConnectFuture<'m> {
206+
T::connect(self, remote)
207+
}
208+
}

0 commit comments

Comments
 (0)