iota_network_stack/callback/
layer.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use tower::Layer;
6
7use super::{Callback, MakeCallbackHandler};
8
9/// [`Layer`] that adds callbacks to a [`Service`].
10///
11/// See the [module docs](crate::callback) for more details.
12///
13/// [`Layer`]: tower::layer::Layer
14/// [`Service`]: tower::Service
15#[derive(Debug, Copy, Clone)]
16pub struct CallbackLayer<M> {
17    pub(crate) make_handler: M,
18}
19
20impl<M> CallbackLayer<M> {
21    /// Create a new [`CallbackLayer`] using the given [`MakeCallbackHandler`].
22    pub fn new(make_handler: M) -> Self
23    where
24        M: MakeCallbackHandler,
25    {
26        Self { make_handler }
27    }
28}
29
30impl<S, M> Layer<S> for CallbackLayer<M>
31where
32    M: Clone,
33{
34    type Service = Callback<S, M>;
35
36    fn layer(&self, inner: S) -> Self::Service {
37        Callback {
38            inner,
39            make_callback_handler: self.make_handler.clone(),
40        }
41    }
42}