std_embedded_nal_async/
tcp.rs1use futures_lite::{AsyncReadExt, AsyncWriteExt};
4use std::io::Error;
5
6impl embedded_nal_async::TcpConnect for crate::Stack {
7 type Error = Error;
8
9 type Connection<'a> = TcpConnection;
10
11 async fn connect(&self, addr: core::net::SocketAddr) -> Result<Self::Connection<'_>, Error> {
12 async_io::Async::<std::net::TcpStream>::connect(addr)
13 .await
14 .map(TcpConnection)
15 }
16}
17
18pub struct TcpConnection(async_io::Async<std::net::TcpStream>);
19
20impl embedded_io_async::ErrorType for TcpConnection {
21 type Error = Error;
22}
23
24impl embedded_io_async::Read for TcpConnection {
25 async fn read(&mut self, buffer: &mut [u8]) -> Result<usize, Error> {
26 self.0.read(buffer).await
27 }
28
29 async fn read_exact(
30 &mut self,
31 buffer: &mut [u8],
32 ) -> Result<(), embedded_io_async::ReadExactError<Error>> {
33 self.0.read_exact(buffer).await.map_err(|e| match e.kind() {
34 std::io::ErrorKind::UnexpectedEof => embedded_io_async::ReadExactError::UnexpectedEof,
35 _ => embedded_io_async::ReadExactError::Other(e),
36 })
37 }
38}
39impl embedded_io_async::Write for TcpConnection {
40 async fn write(&mut self, buffer: &[u8]) -> Result<usize, Error> {
41 self.0.write(buffer).await
42 }
43
44 async fn flush(&mut self) -> Result<(), Error> {
45 self.0.flush().await
46 }
47
48 async fn write_all(&mut self, buffer: &[u8]) -> Result<(), Error> {
49 self.0.write_all(buffer).await
50 }
51}