1use crate::{
2 BindCtx, Ctx, IntoService, IntoServiceFactory, ReadyBinding, Service, ServiceChain,
3 ServiceChainFactory, ServiceFactory,
4};
5
6pub fn ready<S, St, Req>(service: impl IntoService<S, St, Req>) -> ServiceChain<Ready<S>, St, Req>
28where
29 S: Service<St, Req>,
30{
31 crate::service(Ready {
32 service: ReadyBinding::new(service.into_service()),
33 })
34}
35
36pub fn ready_factory<Sf, St, Req>(
41 factory: impl IntoServiceFactory<Sf, St, Req>,
42) -> ServiceChainFactory<ReadyFactory<Sf>, St, Req>
43where
44 Sf: ServiceFactory<St, Req>,
45{
46 crate::factory(ReadyFactory {
47 factory: factory.into_factory(),
48 })
49}
50
51#[derive(Debug, Clone)]
56pub struct Ready<S> {
57 service: ReadyBinding<S>,
58}
59
60impl<S, St, Req> Service<St, Req> for Ready<S>
61where
62 S: Service<St, Req>,
63{
64 type Res = S::Res;
65 type Error = S::Error;
66
67 #[inline]
68 fn bind(&self, ctx: BindCtx<'_>) {
69 ctx.bind(&self.service);
70 }
71
72 #[inline]
73 fn ready(&self, ctx: Ctx<'_, Self, St>) -> impl Future<Output = Result<(), Self::Error>> {
74 self.service.ready(ctx.cast())
75 }
76
77 #[inline]
78 fn call(
79 &self,
80 req: Req,
81 ctx: Ctx<'_, Self, St>,
82 ) -> impl Future<Output = Result<Self::Res, Self::Error>> {
83 self.service.call(req, ctx.cast())
84 }
85
86 #[inline]
87 fn shutdown(&self, ctx: Ctx<'_, Self, St>) -> impl Future<Output = ()> {
88 self.service.shutdown(ctx.cast())
89 }
90}
91
92#[derive(Debug, Clone)]
94pub struct ReadyFactory<Sf> {
95 factory: Sf,
96}
97
98impl<Sf, St, Req> ServiceFactory<St, Req> for ReadyFactory<Sf>
99where
100 Sf: ServiceFactory<St, Req>,
101{
102 type Res = Sf::Res;
103 type Error = Sf::Error;
104 type InitError = Sf::InitError;
105 type Service = Ready<Sf::Service>;
106
107 #[inline]
108 async fn create(&self, st: &St) -> Result<Self::Service, Self::InitError> {
109 Ok(Ready {
110 service: ReadyBinding::new(self.factory.create(st).await?),
111 })
112 }
113}