trillium_http/transport/
mod.rs

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