mirror of
https://github.com/fafhrd91/actix-net
synced 2025-01-31 13:52:08 +01:00
Restore Transform::map_init_err() combinator
This commit is contained in:
parent
eb33f0ecbe
commit
db0bc1e156
@ -4,6 +4,9 @@
|
|||||||
|
|
||||||
### Add missing Clone impls
|
### Add missing Clone impls
|
||||||
|
|
||||||
|
### Restore `Transform::map_init_err()` combinator
|
||||||
|
|
||||||
|
|
||||||
## [1.0.0-alpha.2] - 2019-12-02
|
## [1.0.0-alpha.2] - 2019-12-02
|
||||||
|
|
||||||
### Use owned config value for service factory
|
### Use owned config value for service factory
|
||||||
|
@ -126,7 +126,13 @@ where
|
|||||||
pub struct AndThenServiceFactory<A, B>
|
pub struct AndThenServiceFactory<A, B>
|
||||||
where
|
where
|
||||||
A: ServiceFactory,
|
A: ServiceFactory,
|
||||||
B: ServiceFactory,
|
A::Config: Clone,
|
||||||
|
B: ServiceFactory<
|
||||||
|
Config = A::Config,
|
||||||
|
Request = A::Response,
|
||||||
|
Error = A::Error,
|
||||||
|
InitError = A::InitError,
|
||||||
|
>,
|
||||||
{
|
{
|
||||||
a: A,
|
a: A,
|
||||||
b: B,
|
b: B,
|
||||||
@ -180,7 +186,13 @@ where
|
|||||||
impl<A, B> Clone for AndThenServiceFactory<A, B>
|
impl<A, B> Clone for AndThenServiceFactory<A, B>
|
||||||
where
|
where
|
||||||
A: ServiceFactory + Clone,
|
A: ServiceFactory + Clone,
|
||||||
B: ServiceFactory + Clone,
|
A::Config: Clone,
|
||||||
|
B: ServiceFactory<
|
||||||
|
Config = A::Config,
|
||||||
|
Request = A::Response,
|
||||||
|
Error = A::Error,
|
||||||
|
InitError = A::InitError,
|
||||||
|
> + Clone,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@ -80,6 +80,8 @@ where
|
|||||||
pub struct ApplyServiceFactory<T, F, R, In, Out, Err>
|
pub struct ApplyServiceFactory<T, F, R, In, Out, Err>
|
||||||
where
|
where
|
||||||
T: ServiceFactory<Error = Err>,
|
T: ServiceFactory<Error = Err>,
|
||||||
|
F: FnMut(In, &mut T::Service) -> R + Clone,
|
||||||
|
R: Future<Output = Result<Out, Err>>,
|
||||||
{
|
{
|
||||||
service: T,
|
service: T,
|
||||||
f: F,
|
f: F,
|
||||||
|
@ -23,6 +23,7 @@ mod map_init_err;
|
|||||||
mod pipeline;
|
mod pipeline;
|
||||||
mod then;
|
mod then;
|
||||||
mod transform;
|
mod transform;
|
||||||
|
mod transform_err;
|
||||||
|
|
||||||
pub use self::apply::{apply_fn, apply_fn_factory};
|
pub use self::apply::{apply_fn, apply_fn_factory};
|
||||||
pub use self::apply_cfg::{apply_cfg, apply_cfg_factory};
|
pub use self::apply_cfg::{apply_cfg, apply_cfg_factory};
|
||||||
@ -323,4 +324,5 @@ pub mod dev {
|
|||||||
pub use crate::map_init_err::MapInitErr;
|
pub use crate::map_init_err::MapInitErr;
|
||||||
pub use crate::then::{ThenService, ThenServiceFactory};
|
pub use crate::then::{ThenService, ThenServiceFactory};
|
||||||
pub use crate::transform::ApplyTransform;
|
pub use crate::transform::ApplyTransform;
|
||||||
|
pub use crate::transform_err::TransformMapInitErr;
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ use std::rc::Rc;
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
|
use crate::transform_err::TransformMapInitErr;
|
||||||
use crate::{IntoServiceFactory, Service, ServiceFactory};
|
use crate::{IntoServiceFactory, Service, ServiceFactory};
|
||||||
|
|
||||||
/// Apply transform to a service. Function returns
|
/// Apply transform to a service. Function returns
|
||||||
@ -48,6 +49,16 @@ pub trait Transform<S> {
|
|||||||
|
|
||||||
/// Creates and returns a new Service component, asynchronously
|
/// Creates and returns a new Service component, asynchronously
|
||||||
fn new_transform(&self, service: S) -> Self::Future;
|
fn new_transform(&self, service: S) -> Self::Future;
|
||||||
|
|
||||||
|
/// Map this transforms's factory error to a different error,
|
||||||
|
/// returning a new transform service factory.
|
||||||
|
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, F, E>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: Fn(Self::InitError) -> E + Clone,
|
||||||
|
{
|
||||||
|
TransformMapInitErr::new(self, f)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, S> Transform<S> for Rc<T>
|
impl<T, S> Transform<S> for Rc<T>
|
||||||
|
94
actix-service/src/transform_err.rs
Normal file
94
actix-service/src/transform_err.rs
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
use std::future::Future;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
|
use super::Transform;
|
||||||
|
|
||||||
|
/// Transform for the `map_init_err` combinator, changing the type of a new
|
||||||
|
/// transform's init error.
|
||||||
|
///
|
||||||
|
/// This is created by the `Transform::map_init_err` method.
|
||||||
|
pub struct TransformMapInitErr<T, S, F, E> {
|
||||||
|
t: T,
|
||||||
|
f: F,
|
||||||
|
e: PhantomData<(S, E)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, F, E> TransformMapInitErr<T, S, F, E> {
|
||||||
|
pub(crate) fn new(t: T, f: F) -> Self
|
||||||
|
where
|
||||||
|
T: Transform<S>,
|
||||||
|
F: Fn(T::InitError) -> E,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
t,
|
||||||
|
f,
|
||||||
|
e: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, F, E> Clone for TransformMapInitErr<T, S, F, E>
|
||||||
|
where
|
||||||
|
T: Clone,
|
||||||
|
F: Clone,
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
t: self.t.clone(),
|
||||||
|
f: self.f.clone(),
|
||||||
|
e: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, F, E> Transform<S> for TransformMapInitErr<T, S, F, E>
|
||||||
|
where
|
||||||
|
T: Transform<S>,
|
||||||
|
F: Fn(T::InitError) -> E + Clone,
|
||||||
|
{
|
||||||
|
type Request = T::Request;
|
||||||
|
type Response = T::Response;
|
||||||
|
type Error = T::Error;
|
||||||
|
type Transform = T::Transform;
|
||||||
|
|
||||||
|
type InitError = E;
|
||||||
|
type Future = TransformMapInitErrFuture<T, S, F, E>;
|
||||||
|
|
||||||
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
|
TransformMapInitErrFuture {
|
||||||
|
fut: self.t.new_transform(service),
|
||||||
|
f: self.f.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pin_project! {
|
||||||
|
pub struct TransformMapInitErrFuture<T, S, F, E>
|
||||||
|
where
|
||||||
|
T: Transform<S>,
|
||||||
|
F: Fn(T::InitError) -> E,
|
||||||
|
{
|
||||||
|
#[pin]
|
||||||
|
fut: T::Future,
|
||||||
|
f: F,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, F, E> Future for TransformMapInitErrFuture<T, S, F, E>
|
||||||
|
where
|
||||||
|
T: Transform<S>,
|
||||||
|
F: Fn(T::InitError) -> E + Clone,
|
||||||
|
{
|
||||||
|
type Output = Result<T::Transform, E>;
|
||||||
|
|
||||||
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
let this = self.project();
|
||||||
|
if let Poll::Ready(res) = this.fut.poll(cx) {
|
||||||
|
Poll::Ready(res.map_err(this.f))
|
||||||
|
} else {
|
||||||
|
Poll::Pending
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user