tower_async/make/make_service/
shared.rs1use std::convert::Infallible;
2use tower_async_service::Service;
3
4#[derive(Debug, Clone, Copy)]
8pub struct Shared<S> {
9 service: S,
10}
11
12impl<S> Shared<S> {
13 pub fn new(service: S) -> Self {
15 Self { service }
16 }
17}
18
19impl<S, T> Service<T> for Shared<S>
20where
21 S: Clone,
22{
23 type Response = S;
24 type Error = Infallible;
25
26 async fn call(&self, _target: T) -> Result<Self::Response, Self::Error> {
27 Ok(self.service.clone())
28 }
29}
30
31#[cfg(test)]
32mod tests {
33 use super::*;
34 use crate::make::MakeService;
35 use crate::service_fn;
36
37 async fn echo<R>(req: R) -> Result<R, Infallible> {
38 Ok(req)
39 }
40
41 #[tokio::test]
42 async fn as_make_service() {
43 let shared = Shared::new(service_fn(echo::<&'static str>));
44
45 let svc = shared.make_service(()).await.unwrap();
46
47 let res = svc.call("foo").await.unwrap();
48
49 assert_eq!(res, "foo");
50 }
51
52 #[tokio::test]
53 async fn as_make_service_into_service() {
54 let shared = Shared::new(service_fn(echo::<&'static str>));
55 let shared = MakeService::<(), _>::into_service(shared);
56
57 let svc = shared.call(()).await.unwrap();
58
59 let res = svc.call("foo").await.unwrap();
60
61 assert_eq!(res, "foo");
62 }
63}