use tower::Service;
use crate::{Action, Frame, FrameFuture, Handler};
pub type ReadyAction<ActionHandler, Args, State> = Action<super::Ready<ActionHandler, Args, State>>;
impl<ActionHandler, Args, State> From<super::Ready<ActionHandler, Args, State>>
for ReadyAction<ActionHandler, Args, State>
where
ActionHandler: Handler<Args, State> + Clone + Send + 'static,
Args: Clone + Send + 'static,
State: Clone + Send + 'static,
{
fn from(ready: super::Ready<ActionHandler, Args, State>) -> Self {
Self { action_state: ready }
}
}
impl<ActionHandler, Args, State> super::Ready<ActionHandler, Args, State>
where
ActionHandler: Handler<Args, State> + Clone + Send + 'static,
Args: Clone + Send + 'static,
State: Clone + Send + 'static,
{
pub fn new(action: ActionHandler, state: State) -> Self {
Self {
action,
state,
_args: std::marker::PhantomData,
}
}
}
impl<ActionHandler, Args, State, IntoFrame> Service<IntoFrame>
for ReadyAction<ActionHandler, Args, State>
where
IntoFrame: Into<Frame>,
ActionHandler: Handler<Args, State> + Clone + 'static,
ActionHandler::Future: 'static,
State: Clone,
Args: Clone,
{
type Response = Frame;
type Error = crate::Error;
type Future = FrameFuture;
fn poll_ready(
&mut self,
_: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
std::task::Poll::Ready(Ok(()))
}
fn call(&mut self, frame: IntoFrame) -> Self::Future {
let handler = self.action_state.action.clone();
let future = Handler::invoke(&handler, frame, self.action_state.state.clone());
FrameFuture::from_async_block(future)
}
}