1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-31 15:00:07 +01:00
actix-net/actix-service/src/fn_transform.rs

124 lines
2.8 KiB
Rust
Raw Normal View History

use std::marker::PhantomData;
use futures::future::{ok, FutureResult};
use futures::{Async, IntoFuture, Poll};
use super::{IntoNewTransform, IntoTransform, NewTransform, Transform};
pub struct FnTransform<F, S, Req, Res>
where
F: FnMut(Req, &mut S) -> Res,
Res: IntoFuture,
{
f: F,
_t: PhantomData<(S, Req, Res)>,
}
impl<F, S, Req, Res> FnTransform<F, S, Req, Res>
where
F: FnMut(Req, &mut S) -> Res,
Res: IntoFuture,
{
pub fn new(f: F) -> Self {
FnTransform { f, _t: PhantomData }
}
}
impl<F, S, Req, Res> Clone for FnTransform<F, S, Req, Res>
where
F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture,
{
fn clone(&self) -> Self {
FnTransform {
f: self.f.clone(),
_t: PhantomData,
}
}
}
impl<F, S, Req, Res> Transform<S> for FnTransform<F, S, Req, Res>
where
F: FnMut(Req, &mut S) -> Res,
Res: IntoFuture,
{
type Request = Req;
type Response = Res::Item;
type Error = Res::Error;
type Future = Res::Future;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
fn call(&mut self, request: Req, service: &mut S) -> Self::Future {
(self.f)(request, service).into_future()
}
}
impl<F, S, Req, Res> IntoTransform<FnTransform<F, S, Req, Res>, S> for F
where
F: FnMut(Req, &mut S) -> Res,
Res: IntoFuture,
{
fn into_transform(self) -> FnTransform<F, S, Req, Res> {
FnTransform::new(self)
}
}
pub struct FnNewTransform<F, S, Req, Out, Err>
where
F: FnMut(Req, &mut S) -> Out + Clone,
Out: IntoFuture,
{
f: F,
_t: PhantomData<(S, Req, Out, Err)>,
}
impl<F, S, Req, Res, Err> FnNewTransform<F, S, Req, Res, Err>
where
F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture,
{
pub fn new(f: F) -> Self {
FnNewTransform { f, _t: PhantomData }
}
}
impl<F, S, Req, Res, Err> NewTransform<S> for FnNewTransform<F, S, Req, Res, Err>
where
F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture,
{
type Request = Req;
type Response = Res::Item;
type Error = Res::Error;
type Transform = FnTransform<F, S, Req, Res>;
type InitError = Err;
type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self) -> Self::Future {
ok(FnTransform::new(self.f.clone()))
}
}
impl<F, S, Req, Res, Err> IntoNewTransform<FnNewTransform<F, S, Req, Res, Err>, S> for F
where
F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture,
{
fn into_new_transform(self) -> FnNewTransform<F, S, Req, Res, Err> {
FnNewTransform::new(self)
}
}
impl<F, S, Req, Res, Err> Clone for FnNewTransform<F, S, Req, Res, Err>
where
F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture,
{
fn clone(&self) -> Self {
Self::new(self.f.clone())
}
}