trillium_async_std/
transport.rs

1use async_std::net::{TcpStream, ToSocketAddrs};
2use std::{io::Result, net::SocketAddr};
3use trillium_macros::{AsyncRead, AsyncWrite};
4use trillium_server_common::{AsyncRead, AsyncWrite, Transport};
5
6/// A transport newtype for async-std
7#[derive(Debug, Clone, AsyncRead, AsyncWrite)]
8pub struct AsyncStdTransport<T>(T);
9
10impl<T> AsyncStdTransport<T> {
11    /// returns the contained type
12    pub fn into_inner(self) -> T {
13        self.0
14    }
15}
16
17impl AsyncStdTransport<TcpStream> {
18    /// initiates an outbound http connection
19    pub async fn connect(socket: impl ToSocketAddrs) -> Result<Self> {
20        TcpStream::connect(socket).await.map(Self)
21    }
22}
23
24impl<T> From<T> for AsyncStdTransport<T> {
25    fn from(value: T) -> Self {
26        Self(value)
27    }
28}
29
30impl Transport for AsyncStdTransport<TcpStream> {
31    fn peer_addr(&self) -> Result<Option<SocketAddr>> {
32        self.0.peer_addr().map(Some)
33    }
34
35    fn set_ip_ttl(&mut self, ttl: u32) -> Result<()> {
36        self.0.set_ttl(ttl)
37    }
38
39    fn set_nodelay(&mut self, nodelay: bool) -> Result<()> {
40        self.0.set_nodelay(nodelay)
41    }
42}
43
44#[cfg(unix)]
45impl Transport for AsyncStdTransport<async_std::os::unix::net::UnixStream> {}