embedded_nal_async/stack/
tcp.rs

1use core::net::SocketAddr;
2
3/// This trait is implemented by TCP/IP stacks. The trait allows the underlying driver to
4/// construct multiple connections that implement the I/O traits from embedded-io-async.
5///
6/// The associated connection type should close the connection when dropped.
7pub trait TcpConnect {
8	/// Error type returned on connect failure.
9	type Error: embedded_io_async::Error;
10
11	/// Type holding state of a TCP connection. Should close the connection when dropped.
12	type Connection<'a>: embedded_io_async::Read<Error = Self::Error>
13		+ embedded_io_async::Write<Error = Self::Error>
14	where
15		Self: 'a;
16
17	/// Connect to the given remote host and port.
18	///
19	/// Returns `Ok` if the connection was successful.
20	async fn connect<'a>(&'a self, remote: SocketAddr)
21		-> Result<Self::Connection<'a>, Self::Error>;
22}
23
24impl<T: TcpConnect> TcpConnect for &T {
25	type Error = T::Error;
26
27	type Connection<'a> = T::Connection<'a> where Self: 'a;
28
29	async fn connect<'a>(
30		&'a self,
31		remote: SocketAddr,
32	) -> Result<Self::Connection<'a>, Self::Error> {
33		T::connect(self, remote).await
34	}
35}