indicator/context/layer/
inspect.rs

1use crate::context::{Context, ContextOperator, Value, ValueRef};
2
3use super::Layer;
4
5/// Layer that used to inspect the context.
6pub struct Inspect<F>(pub F);
7
8impl<T, P, F> Layer<T, P> for Inspect<F>
9where
10    P: ContextOperator<T>,
11    F: Fn(&T, &Context) + Clone,
12{
13    type Operator = InspectOperator<P, F>;
14    type Out = P::Out;
15
16    #[inline]
17    fn layer(&self, operator: P) -> Self::Operator {
18        InspectOperator {
19            inner: operator,
20            inspect: self.0.clone(),
21        }
22    }
23}
24
25/// Operator that used to inspect the context.
26pub struct InspectOperator<P, F> {
27    inner: P,
28    inspect: F,
29}
30
31impl<T, P, F> ContextOperator<T> for InspectOperator<P, F>
32where
33    P: ContextOperator<T>,
34    F: Fn(&T, &Context),
35{
36    type Out = P::Out;
37
38    #[inline]
39    fn next(&mut self, input: Value<T>) -> Value<Self::Out> {
40        let ValueRef { value, context } = input.as_ref();
41        (self.inspect)(value, context);
42        self.inner.next(input)
43    }
44}