channel_server/
add_data.rs

1use crate::{Endpoint, ChannelError, Middleware, Request};
2/// Middleware for add any data to request.
3pub struct AddData<T> {
4    value: T,
5}
6
7impl<T: Clone + Send + Sync + 'static> AddData<T> {
8    /// Create new `AddData` middleware with any value.
9    pub fn new(value: T) -> Self {
10        AddData { value }
11    }
12}
13
14impl<E, T> Middleware<E> for AddData<T>
15where
16    E: Endpoint,
17    T: Clone + Send + Sync + 'static,
18{
19    type Output = AddDataEndpoint<E, T>;
20
21    fn transform(&self, ep: E) -> Self::Output {
22        AddDataEndpoint {
23            inner: ep,
24            value: self.value.clone(),
25        }
26    }
27}
28
29#[derive(Clone)]
30pub struct AddDataEndpoint<E, T> {
31    inner: E,
32    value: T,
33}
34
35impl<E, T> Endpoint for AddDataEndpoint<E, T>
36where
37    E: Endpoint,
38    T: Clone + Send + Sync + 'static,
39{
40    type Output = E::Output;
41
42    fn call(&self, mut req: Request) -> Result<Self::Output, ChannelError> {
43        req.extensions_mut().insert(self.value.clone());
44        self.inner.call(req)
45    }
46}