Skip to main content

base_endpoint/tls/rustls/
rustls_acceptor.rs

1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4use super::{RustlsConnection, establish, tls_error};
5use crate::tls::TlsAcceptor;
6use ::rustls::{ServerConfig, ServerConnection};
7use base_scheduler::{
8    Cancellation, OperationResult, SocketClose, SocketStreamReading, SocketStreamWriting,
9};
10use std::sync::Arc;
11
12/// A rustls server provider using application-supplied certificates, client-authentication policy and ALPN configuration.
13#[derive(Clone, Debug)]
14pub struct RustlsAcceptor {
15    config: Arc<ServerConfig>,
16}
17
18impl RustlsAcceptor {
19    /// Share an explicitly configured rustls server configuration.
20    pub fn new(config: Arc<ServerConfig>) -> Self {
21        Self { config }
22    }
23}
24
25impl<S> TlsAcceptor<S, S::Socket> for RustlsAcceptor
26where
27    S: SocketStreamReading + SocketStreamWriting + SocketClose + ?Sized,
28    S::Socket: 'static,
29{
30    type Connection = RustlsConnection<S::Socket>;
31
32    async fn accept(
33        &self,
34        scheduler: &S,
35        connection: S::Socket,
36        cancellation: &Cancellation,
37    ) -> OperationResult<Self::Connection> {
38        let state = (|| {
39            cancellation.check()?;
40            Ok(ServerConnection::new(self.config.clone())
41                .map_err(tls_error)?
42                .into())
43        })();
44        establish(scheduler, connection, state, cancellation).await
45    }
46}