Skip to main content

dispatch_core/
after.rs

1use core::marker::PhantomData;
2
3use crate::{StageError, SystemParam, param::ParamSet};
4
5/// Adapts a typed function that runs after a successful command.
6pub trait AfterHandler<C, R, O, E, Params> {
7    /// Validates resource access, extracts arguments, and invokes the function.
8    fn run(&self, context: &mut C, output: &mut O, resources: &mut R) -> Result<(), StageError<E>>;
9}
10
11impl<C, R, O, E, F> AfterHandler<C, R, O, E, ()> for F
12where
13    F: Fn(&mut C, &mut O) -> Result<(), E>,
14{
15    fn run(
16        &self,
17        context: &mut C,
18        output: &mut O,
19        _resources: &mut R,
20    ) -> Result<(), StageError<E>> {
21        self(context, output).map_err(StageError::User)
22    }
23}
24
25macro_rules! impl_after_handler {
26    ($($param:ident => $value:ident @ $index:expr),+ $(,)?) => {
27        impl<C, R, O, E, F, $($param),+> AfterHandler<C, R, O, E, ($($param,)+)> for F
28        where
29            $($param: SystemParam<R>,)+
30            F: Fn(&mut C, &mut O, $($param),+) -> Result<(), E>,
31            for<'context, 'output, 'resources> F:
32                Fn(
33                    &'context mut C,
34                    &'output mut O,
35                    $($param::Item<'resources>),+
36                ) -> Result<(), E>,
37        {
38            fn run(
39                &self,
40                context: &mut C,
41                output: &mut O,
42                resources: &mut R,
43            ) -> Result<(), StageError<E>> {
44                let ($($value,)+) =
45                    <($($param,)+) as ParamSet<R>>::fetch(resources)
46                        .map_err(StageError::Param)?;
47
48                self(context, output, $($value),+).map_err(StageError::User)
49            }
50        }
51    };
52}
53
54impl_after_handler!(P0 => p0 @ 0);
55impl_after_handler!(P0 => p0 @ 0, P1 => p1 @ 1);
56impl_after_handler!(P0 => p0 @ 0, P1 => p1 @ 1, P2 => p2 @ 2);
57impl_after_handler!(P0 => p0 @ 0, P1 => p1 @ 1, P2 => p2 @ 2, P3 => p3 @ 3);
58impl_after_handler!(
59    P0 => p0 @ 0,
60    P1 => p1 @ 1,
61    P2 => p2 @ 2,
62    P3 => p3 @ 3,
63    P4 => p4 @ 4,
64);
65impl_after_handler!(
66    P0 => p0 @ 0,
67    P1 => p1 @ 1,
68    P2 => p2 @ 2,
69    P3 => p3 @ 3,
70    P4 => p4 @ 4,
71    P5 => p5 @ 5,
72);
73impl_after_handler!(
74    P0 => p0 @ 0,
75    P1 => p1 @ 1,
76    P2 => p2 @ 2,
77    P3 => p3 @ 3,
78    P4 => p4 @ 4,
79    P5 => p5 @ 5,
80    P6 => p6 @ 6,
81);
82impl_after_handler!(
83    P0 => p0 @ 0,
84    P1 => p1 @ 1,
85    P2 => p2 @ 2,
86    P3 => p3 @ 3,
87    P4 => p4 @ 4,
88    P5 => p5 @ 5,
89    P6 => p6 @ 6,
90    P7 => p7 @ 7,
91);
92
93/// Runs optional logic after a command succeeds.
94pub trait After<C, R, O, E> {
95    /// Runs the after stage with mutable access to the command output.
96    fn run_after(
97        &self,
98        context: &mut C,
99        output: &mut O,
100        resources: &mut R,
101    ) -> Result<(), StageError<E>>;
102}
103
104/// An omitted after stage.
105#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
106pub struct NoAfter;
107
108impl<C, R, O, E> After<C, R, O, E> for NoAfter {
109    fn run_after(
110        &self,
111        _context: &mut C,
112        _output: &mut O,
113        _resources: &mut R,
114    ) -> Result<(), StageError<E>> {
115        Ok(())
116    }
117}
118
119/// Adapts one resource-injected function into an [`After`] stage.
120pub struct AfterFn<F, Params> {
121    function: F,
122    marker: PhantomData<fn() -> Params>,
123}
124
125impl<F, Params> AfterFn<F, Params> {
126    pub(crate) const fn new(function: F) -> Self {
127        Self {
128            function,
129            marker: PhantomData,
130        }
131    }
132
133    /// Returns the wrapped function.
134    pub const fn function(&self) -> &F {
135        &self.function
136    }
137
138    /// Returns a mutable reference to the wrapped function.
139    pub fn function_mut(&mut self) -> &mut F {
140        &mut self.function
141    }
142
143    /// Consumes the adapter and returns the wrapped function.
144    pub fn into_inner(self) -> F {
145        self.function
146    }
147}
148
149impl<C, R, O, E, F, Params> After<C, R, O, E> for AfterFn<F, Params>
150where
151    F: AfterHandler<C, R, O, E, Params>,
152{
153    fn run_after(
154        &self,
155        context: &mut C,
156        output: &mut O,
157        resources: &mut R,
158    ) -> Result<(), StageError<E>> {
159        self.function.run(context, output, resources)
160    }
161}