dfdi/
cached_service.rs

1use dfdi_core::{Context, Provider, Service};
2
3/// Cached service
4///
5/// A provider that returns the same reference on every call
6pub struct CachedService<'cx, S: Service>(pub S::Output<'cx>);
7
8impl<'cx, S: Service> CachedService<'cx, S> {
9    /// Create a new cached service
10    #[inline(always)]
11    pub fn new(value: S::Output<'cx>) -> Self {
12        Self(value)
13    }
14}
15
16impl<'cx, S> Provider<'cx, &'static S> for CachedService<'cx, S>
17where
18    S: Service,
19    S::Output<'cx>: Send + Sync,
20{
21    #[inline(always)]
22    fn provide(
23        &'cx self,
24        _cx: &'cx Context,
25        _arg: S::Argument<'_>,
26    ) -> &'cx <S as Service>::Output<'cx> {
27        &self.0
28    }
29}