intrepid_core/routing/
endpoint.rs

1use tower::Service;
2
3use crate::{BoxedAction, Frame, FrameFuture, Handler};
4
5/// An action that can be invoked as a service.
6#[derive(Clone)]
7pub struct Endpoint<State>(BoxedAction<State>);
8
9impl<State> Endpoint<State> {
10    /// Create a new endpoint.
11    pub fn new<Args>(action: impl Handler<Args, State> + Send + Sync + 'static) -> Self
12    where
13        State: Clone + Send + Sync + 'static,
14        Args: Clone + Send + Sync + 'static,
15    {
16        let actionable = action.as_into_actionable();
17
18        Self(actionable)
19    }
20
21    /// Convert this endpoint into the inner action, discarding the endpoint wrapper.
22    pub fn into_inner(self) -> BoxedAction<State> {
23        self.0
24    }
25}
26
27impl<Args, State> Handler<Args, State> for Endpoint<State>
28where
29    State: Clone + Send + 'static,
30    Args: Clone + Send + 'static,
31{
32    type Future = FrameFuture;
33
34    fn invoke(&self, frame: impl Into<Frame>, state: State) -> Self::Future {
35        let frame = frame.into();
36        let mut actionable = self.0.clone().into_actionable(state);
37
38        actionable.call(frame.clone())
39    }
40}