Skip to main content

ntex_service/
state.rs

1/// A request that carries state for a service call.
2pub trait RequestState<Req> {
3    /// State extracted from the request.
4    type State: 'static;
5
6    /// Splits this value into its state and request components.
7    fn unpack(self) -> (Self::State, Req);
8}
9
10/// A request paired with state for the duration of a service call.
11#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub struct State<St, Req> {
13    /// Request passed to the service.
14    pub req: Req,
15    /// State made available while processing the request.
16    pub state: St,
17}
18
19impl<Req, St: 'static> RequestState<Req> for State<St, Req> {
20    type State = St;
21
22    #[inline]
23    fn unpack(self) -> (St, Req) {
24        let State { state, req } = self;
25        (state, req)
26    }
27}
28
29impl<Req, St: 'static> RequestState<Req> for (St, Req) {
30    type State = St;
31
32    #[inline]
33    fn unpack(self) -> (St, Req) {
34        (self.0, self.1)
35    }
36}