intrepid_core/action/candidate/
candidate_action.rsuse tower::Service;
use crate::{ActionService, Actionable, Frame, FrameFuture, Handler};
use super::{Action, Active, Ready, Stateless};
pub type CandidateAction<ActionHandler, Args, State> =
Action<super::Candidate<ActionHandler, Args, State>>;
impl<ActionHandler, Args, State> CandidateAction<ActionHandler, Args, State>
where
ActionHandler: Handler<Args, State> + Clone + Send + 'static,
Args: Clone + Send + 'static,
State: Clone + Send + 'static,
{
pub fn new(action: ActionHandler) -> Self {
super::Candidate::new(action).into()
}
pub fn with_state(self, state: State) -> Action<Ready<ActionHandler, Args, State>> {
Ready::new(self.action_state.action, state).into()
}
}
impl<ActionHandler, Args> CandidateAction<ActionHandler, Args, ()>
where
ActionHandler: Handler<Args, ()> + Clone + Send + 'static,
Args: Clone + Send + 'static,
{
pub fn stateless(self) -> Action<Stateless<ActionHandler, Args>> {
Stateless::new(self.action_state.action).into()
}
}
impl<ActionHandler, Args, State> Actionable<State> for CandidateAction<ActionHandler, Args, State>
where
ActionHandler: Handler<Args, State> + Clone + Send + Sync + 'static,
Args: Clone + Send + Sync + 'static,
State: Clone + Send + Sync + 'static,
{
fn into_actionable(self: Box<Self>, state: State) -> Action<Active> {
let inner = self.with_state(state);
let service = ActionService::new(inner);
Action::active(service)
}
}
impl<IntoFrame, ActionHandler, Args> Service<IntoFrame> for CandidateAction<ActionHandler, Args, ()>
where
IntoFrame: Into<Frame> + Clone + Send + 'static,
ActionHandler: Handler<Args, ()> + Clone + Send + 'static,
Args: Clone + Send + 'static,
{
type Response = Frame;
type Error = crate::Error;
type Future = FrameFuture;
fn poll_ready(
&mut self,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
let mut action = self.clone().stateless();
Service::<IntoFrame>::poll_ready(&mut action, cx)
}
fn call(&mut self, frame: IntoFrame) -> Self::Future {
let mut action = self.clone().stateless();
Service::<IntoFrame>::call(&mut action, frame)
}
}