hyper_server/accept.rs
1//! Module `accept` provides utilities for asynchronously processing and modifying IO streams and services.
2//!
3//! The primary trait exposed by this module is [`Accept`], which allows for asynchronous transformations
4//! of input streams and services. The module also provides a default implementation, [`DefaultAcceptor`],
5//! that performs no modifications and directly passes through the input stream and service.
6
7use std::{
8 future::{Future, Ready},
9 io,
10};
11
12/// An asynchronous trait for processing and modifying IO streams and services.
13///
14/// Implementations of this trait can be used to modify or transform the input stream and service before
15/// further processing. For instance, this trait could be used to perform initial authentication, logging,
16/// or other setup operations on new connections.
17pub trait Accept<I, S> {
18 /// The modified or transformed IO stream produced by `accept`.
19 type Stream;
20
21 /// The modified or transformed service produced by `accept`.
22 type Service;
23
24 /// The Future type that is returned by `accept`.
25 type Future: Future<Output = io::Result<(Self::Stream, Self::Service)>>;
26
27 /// Asynchronously process and possibly modify the given IO stream and service.
28 ///
29 /// # Parameters:
30 /// * `stream`: The incoming IO stream, typically a connection.
31 /// * `service`: The associated service with the stream.
32 ///
33 /// # Returns:
34 /// A future resolving to the modified stream and service, or an error.
35 fn accept(&self, stream: I, service: S) -> Self::Future;
36}
37
38/// A default implementation of the [`Accept`] trait that performs no modifications.
39///
40/// This is a no-op acceptor that simply passes the provided stream and service through without any transformations.
41#[derive(Clone, Copy, Debug, Default)]
42pub struct DefaultAcceptor;
43
44impl DefaultAcceptor {
45 /// Create a new default acceptor instance.
46 ///
47 /// # Returns:
48 /// An instance of [`DefaultAcceptor`].
49 pub fn new() -> Self {
50 Self
51 }
52}
53
54impl<I, S> Accept<I, S> for DefaultAcceptor {
55 type Stream = I;
56 type Service = S;
57 type Future = Ready<io::Result<(Self::Stream, Self::Service)>>;
58
59 fn accept(&self, stream: I, service: S) -> Self::Future {
60 std::future::ready(Ok((stream, service)))
61 }
62}