1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-02-25 21:52:49 +01:00
actix-net/actix-service/src/fn_transform.rs

67 lines
1.5 KiB
Rust
Raw Normal View History

use std::marker::PhantomData;
use futures::future::{ok, FutureResult};
2019-03-04 19:38:11 -08:00
use futures::IntoFuture;
2019-03-04 19:38:11 -08:00
use crate::{Apply, IntoTransform, Service, Transform};
2019-03-09 06:36:23 -08:00
pub struct FnTransform<F, S, In, Out, Err>
where
2019-03-04 19:38:11 -08:00
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
{
f: F,
2019-03-09 06:36:23 -08:00
_t: PhantomData<(S, In, Out, Err)>,
}
2019-03-09 06:36:23 -08:00
impl<F, S, In, Out, Err> FnTransform<F, S, In, Out, Err>
where
2019-03-04 19:38:11 -08:00
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
{
pub fn new(f: F) -> Self {
FnTransform { f, _t: PhantomData }
}
}
2019-03-09 06:36:23 -08:00
impl<F, S, In, Out, Err> Transform<S> for FnTransform<F, S, In, Out, Err>
where
2019-03-09 06:36:23 -08:00
S: Service,
2019-03-04 19:38:11 -08:00
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
2019-03-04 19:38:11 -08:00
Out::Error: From<S::Error>,
{
2019-03-09 06:36:23 -08:00
type Request = In;
2019-03-04 19:38:11 -08:00
type Response = Out::Item;
type Error = Out::Error;
2019-03-09 06:36:23 -08:00
type Transform = Apply<S, F, In, Out>;
type InitError = Err;
type Future = FutureResult<Self::Transform, Self::InitError>;
2019-03-04 19:38:11 -08:00
fn new_transform(&self, service: S) -> Self::Future {
ok(Apply::new(service, self.f.clone()))
}
}
2019-03-09 06:36:23 -08:00
impl<F, S, In, Out, Err> IntoTransform<FnTransform<F, S, In, Out, Err>, S> for F
where
2019-03-09 06:36:23 -08:00
S: Service,
2019-03-04 19:38:11 -08:00
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<S::Error>,
{
2019-03-09 06:36:23 -08:00
fn into_transform(self) -> FnTransform<F, S, In, Out, Err> {
2019-03-04 19:38:11 -08:00
FnTransform::new(self)
}
}
2019-03-09 06:36:23 -08:00
impl<F, S, In, Out, Err> Clone for FnTransform<F, S, In, Out, Err>
where
2019-03-04 19:38:11 -08:00
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
{
fn clone(&self) -> Self {
Self::new(self.f.clone())
}
}