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