intrepid_core/action/ready/
ready_action.rs1use tower::Service;
2
3use crate::{Action, Frame, FrameFuture, Handler};
4
5pub type ReadyAction<ActionHandler, Args, State> = Action<super::Ready<ActionHandler, Args, State>>;
7
8impl<ActionHandler, Args, State> From<super::Ready<ActionHandler, Args, State>>
9 for ReadyAction<ActionHandler, Args, State>
10where
11 ActionHandler: Handler<Args, State> + Clone + Send + 'static,
12 Args: Clone + Send + 'static,
13 State: Clone + Send + 'static,
14{
15 fn from(ready: super::Ready<ActionHandler, Args, State>) -> Self {
16 Self { action_state: ready }
17 }
18}
19
20impl<ActionHandler, Args, State> super::Ready<ActionHandler, Args, State>
21where
22 ActionHandler: Handler<Args, State> + Clone + Send + 'static,
23 Args: Clone + Send + 'static,
24 State: Clone + Send + 'static,
25{
26 pub fn new(action: ActionHandler, state: State) -> Self {
28 Self {
29 action,
30 state,
31 _args: std::marker::PhantomData,
32 }
33 }
34}
35
36impl<ActionHandler, Args, State, IntoFrame> Service<IntoFrame>
37 for ReadyAction<ActionHandler, Args, State>
38where
39 IntoFrame: Into<Frame>,
40 ActionHandler: Handler<Args, State> + Clone + 'static,
41 ActionHandler::Future: 'static,
42 State: Clone,
43 Args: Clone,
44{
45 type Response = Frame;
46 type Error = crate::Error;
47 type Future = FrameFuture;
48
49 fn poll_ready(
50 &mut self,
51 _: &mut std::task::Context<'_>,
52 ) -> std::task::Poll<Result<(), Self::Error>> {
53 std::task::Poll::Ready(Ok(()))
54 }
55
56 fn call(&mut self, frame: IntoFrame) -> Self::Future {
57 let handler = self.action_state.action.clone();
58 let future = Handler::invoke(&handler, frame, self.action_state.state.clone());
59
60 FrameFuture::from_async_block(future)
61 }
62}