Skip to main content

trillium/
transport.rs

1use futures_lite::{AsyncRead, AsyncWrite};
2use std::{any::Any, io::Result, net::SocketAddr, time::Duration};
3
4/// # The interface that the http protocol is communicated over.
5///
6/// This trait supports several common interfaces supported by tcp
7/// streams, but also can be implemented for other stream types. All trait
8/// functions are currently optional.
9///
10/// **Note:** `Transport` uses the [`AsyncWrite`](futures_lite::AsyncWrite) and
11/// [`AsyncRead`](futures_lite::AsyncRead) traits from [`futures-lite`](futures_lite), which differ
12/// from the tokio `AsyncRead` / `AsyncWrite` traits. Runtime adapters handle bridging these at the
13/// boundary.
14#[allow(unused)]
15pub trait Transport: Any + AsyncRead + AsyncWrite + Unpin + Send + Sync + 'static {
16    /// # Sets the linger duration of this transport by setting the `SO_LINGER` option
17    ///
18    /// See [`std::net::TcpStream::set_linger`]
19    /// Optional to implement.
20    ///
21    /// # Errors
22    ///
23    /// Return an error if this transport supports setting linger and
24    /// attempting to do so is unsuccessful.
25    fn set_linger(&mut self, linger: Option<Duration>) -> Result<()> {
26        Ok(())
27    }
28
29    /// # Sets the value of the `TCP_NODELAY` option on this transport.
30    ///
31    /// See [`std::net::TcpStream::set_nodelay`].
32    /// Optional to implement.
33    ///
34    /// # Errors
35    ///
36    /// Return an error if this transport supports setting nodelay and
37    /// attempting to do so is unsuccessful.
38    fn set_nodelay(&mut self, nodelay: bool) -> Result<()> {
39        Ok(())
40    }
41
42    /// # Sets the value for the `IP_TTL` option on this transport.
43    ///
44    /// See [`std::net::TcpStream::set_ttl`]
45    /// Optional to implement
46    ///
47    /// # Errors
48    ///
49    /// Return an error if this transport supports setting ttl and
50    /// attempting to do so is unsuccessful.
51    fn set_ip_ttl(&mut self, ttl: u32) -> Result<()> {
52        Ok(())
53    }
54
55    /// # Returns the socket address of the remote peer of this transport.
56    ///
57    /// # Errors
58    ///
59    /// Return an error if this transport supports retrieving the
60    /// remote peer but attempting to do so is unsuccessful.
61    fn peer_addr(&self) -> Result<Option<SocketAddr>> {
62        Ok(None)
63    }
64}
65
66impl Transport for Box<dyn Transport> {
67    fn set_linger(&mut self, linger: Option<Duration>) -> Result<()> {
68        (**self).set_linger(linger)
69    }
70
71    fn set_nodelay(&mut self, nodelay: bool) -> Result<()> {
72        (**self).set_nodelay(nodelay)
73    }
74
75    fn set_ip_ttl(&mut self, ttl: u32) -> Result<()> {
76        (**self).set_ip_ttl(ttl)
77    }
78
79    fn peer_addr(&self) -> Result<Option<SocketAddr>> {
80        (**self).peer_addr()
81    }
82}
83
84impl Transport for trillium_http::Synthetic {}