Skip to main content

trillium_server_common/
acceptor.rs

1use crate::{async_trait, Transport};
2use std::{convert::Infallible, fmt::Debug};
3
4/**
5This trait provides the common interface for server-side tls
6acceptors, abstracting over various implementations
7
8The only implementation provided by this crate is `()`, which is
9a noop acceptor, and passes through the `Input` type.
10
11Implementing this trait looks like:
12```rust,ignore
13use trillium_server_common::{AsyncRead, AsyncWrite, async_trait, Transport};
14#[async_trait]
15impl<Input> Acceptor<Input> for my_tls_impl::Acceptor
16where
17    Input: Transport,
18{
19    type Output = my_tls_impl::TlsStream<Input>;
20    type Error = my_tls_impl::Error;
21    async fn accept(&self, input: Input) -> Result<Self::Output, Self::Error> {
22        self.accept(input).await
23    }
24}
25```
26*/
27#[async_trait]
28pub trait Acceptor<Input>: Clone + Send + Sync + 'static
29where
30    Input: Transport,
31{
32    /// The stream type. For example, `TlsStream<Input>`
33    type Output: Transport;
34
35    /// An error type that [`Acceptor::accept`] may return
36    type Error: Debug + Send + Sync;
37
38    /**
39    Transform an Input (`AsyncRead + AsyncWrite + Send + Sync + Unpin + 'static`) into Self::Output
40
41    Async trait signature:
42    ```rust,ignore
43    async fn accept(&self, input: Input) -> Result<Self::Output, Self::Error>;
44    ```
45    */
46    async fn accept(&self, input: Input) -> Result<Self::Output, Self::Error>;
47}
48
49#[async_trait]
50impl<Input> Acceptor<Input> for ()
51where
52    Input: Transport,
53{
54    type Output = Input;
55    type Error = Infallible;
56    async fn accept(&self, input: Input) -> Result<Self::Output, Self::Error> {
57        Ok(input)
58    }
59}