intrepid_core/action/
stateless.rs1use tower::Service;
2
3use crate::{Frame, FrameFuture, Handler};
4
5use super::Action;
6
7#[derive(Clone)]
10pub struct Stateless<ActionHandler: Clone, Args: Clone> {
11 pub action: ActionHandler,
13 _args: std::marker::PhantomData<Args>,
14}
15
16impl<ActionHandler, Args> From<Stateless<ActionHandler, Args>>
17 for Action<Stateless<ActionHandler, Args>>
18where
19 ActionHandler: Handler<Args, ()> + Clone + Send + 'static,
20 Args: Clone + Send + 'static,
21{
22 fn from(status: Stateless<ActionHandler, Args>) -> Self {
23 Self { action_state: status }
24 }
25}
26
27impl<ActionHandler, Args> Stateless<ActionHandler, Args>
28where
29 ActionHandler: Handler<Args, ()> + Clone + Send + 'static,
30 Args: Clone + Send + 'static,
31{
32 pub fn new(action: ActionHandler) -> Self {
34 Self {
35 action,
36 _args: std::marker::PhantomData,
37 }
38 }
39}
40
41impl<ActionHandler, Args> Stateless<ActionHandler, Args>
42where
43 ActionHandler: Handler<Args, ()> + Clone + Send + 'static,
44 Args: Clone + Send + 'static,
45{
46 pub fn new_stateless(action: ActionHandler) -> Self {
48 Self::new(action)
49 }
50}
51
52impl<ActionHandler, Args, IntoFrame> Service<IntoFrame> for Action<Stateless<ActionHandler, Args>>
53where
54 IntoFrame: Into<Frame>,
55 ActionHandler: Handler<Args, ()> + Clone + 'static,
56 ActionHandler::Future: 'static,
57 Args: Clone,
58{
59 type Response = Frame;
60 type Error = crate::Error;
61 type Future = FrameFuture;
62
63 fn poll_ready(
64 &mut self,
65 _: &mut std::task::Context<'_>,
66 ) -> std::task::Poll<Result<(), Self::Error>> {
67 std::task::Poll::Ready(Ok(()))
68 }
69
70 fn call(&mut self, frame: IntoFrame) -> Self::Future {
71 let handler = self.action_state.action.clone();
72
73 FrameFuture::from_async_block(Handler::invoke(&handler, frame, ()))
74 }
75}
76
77#[tokio::test]
78async fn invoking_stateless_actions_directly() -> Result<(), tower::BoxError> {
79 let action = || async {};
80 let result = action.invoke((), ()).await?;
81
82 assert_eq!(result, ().into());
83
84 Ok(())
85}