composable_tower_http/extension/modify/
layer.rs

1use std::marker::PhantomData;
2
3use tower::Layer;
4
5use super::service::ModificationService;
6
7#[derive(Debug, Clone)]
8pub struct ModificationLayer<M, T> {
9    modifier: M,
10    _phantom: PhantomData<T>,
11}
12
13impl<M, T> ModificationLayer<M, T> {
14    pub const fn new(modifier: M) -> Self {
15        Self {
16            modifier,
17            _phantom: PhantomData,
18        }
19    }
20}
21
22impl<S, M, T> Layer<S> for ModificationLayer<M, T>
23where
24    M: Clone,
25{
26    type Service = ModificationService<S, M, T>;
27
28    fn layer(&self, service: S) -> Self::Service {
29        ModificationService::new(service, self.modifier.clone())
30    }
31}
32
33pub trait ModificationLayerExt: Sized {
34    fn modification_layer<T>(self) -> ModificationLayer<Self, T>;
35}
36
37impl<T> ModificationLayerExt for T
38where
39    T: Sized + Clone,
40{
41    fn modification_layer<M>(self) -> ModificationLayer<Self, M> {
42        ModificationLayer::new(self)
43    }
44}