scuffle_http/service/
clone_factory.rs

1use std::net::SocketAddr;
2
3use super::{HttpService, HttpServiceFactory};
4
5/// A [`HttpServiceFactory`] that simply clones the given service for each new connection.
6///
7/// Create by calling [`service_clone_factory`].
8#[derive(Clone, Debug)]
9pub struct ServiceCloneFactory<S>(S);
10
11/// Create a [`ServiceCloneFactory`] from a given service.
12///
13/// See [`ServiceCloneFactory`] for details.
14pub fn service_clone_factory<S>(service: S) -> ServiceCloneFactory<S>
15where
16    S: HttpService + Clone + Send,
17{
18    ServiceCloneFactory(service)
19}
20
21impl<S> HttpServiceFactory for ServiceCloneFactory<S>
22where
23    S: HttpService + Clone + Send,
24{
25    type Error = std::convert::Infallible;
26    type Service = S;
27
28    async fn new_service(&mut self, _remote_addr: SocketAddr) -> Result<Self::Service, Self::Error> {
29        Ok(self.0.clone())
30    }
31}