pub struct TypeEraser<M>(/* private fields */);
Expand description
Type eraser middleware is for erasing “unwanted” complex types produced by service tree
and expose well known concrete types xitca-web
can handle.
§Example
// a handler function expect xitca_web::body::RequestBody as body type.
async fn handler(_: &WebContext<'_>) -> &'static str {
"hello,world!"
}
// a limit middleware that limit request body to max size of 1MB.
// this middleware would produce a new type of request body that
// handler function don't know of.
let limit = Limit::new().set_request_body_max_size(1024 * 1024);
// an eraser middleware that transform any request body to xitca_web::body::RequestBody.
let eraser = TypeEraser::request_body();
App::new()
.at("/", handler_service(handler))
// introduce eraser middleware between handler and limit middleware
// to resolve the type difference between them.
// without it this piece of code would simply refuse to compile.
.enclosed(eraser.clone())
.enclosed(limit.clone());
// group middleware is suggested way of handling of use case like this.
let group = Group::new()
.enclosed(eraser)
.enclosed(limit);
// it's also suggested to group multiple type mutation middlewares together and apply
// eraser on them once if possible. the reason being TypeErase has a cost and by
// grouping you only pay the cost once.
App::new()
.at("/", handler_service(handler))
.enclosed(group);
Implementations§
Source§impl TypeEraser<EraseReqBody>
impl TypeEraser<EraseReqBody>
Sourcepub const fn request_body() -> Self
pub const fn request_body() -> Self
Erase generic request body type. making downstream middlewares observe RequestBody
.
Source§impl TypeEraser<EraseResBody>
impl TypeEraser<EraseResBody>
Sourcepub const fn response_body() -> Self
pub const fn response_body() -> Self
Erase generic response body type. making downstream middlewares observe ResponseBody
.
Trait Implementations§
Source§impl<M> Clone for TypeEraser<M>
impl<M> Clone for TypeEraser<M>
Auto Trait Implementations§
impl<M> Freeze for TypeEraser<M>
impl<M> RefUnwindSafe for TypeEraser<M>where
M: RefUnwindSafe,
impl<M> Send for TypeEraser<M>where
M: Send,
impl<M> Sync for TypeEraser<M>where
M: Sync,
impl<M> Unpin for TypeEraser<M>where
M: Unpin,
impl<M> UnwindSafe for TypeEraser<M>where
M: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<S, Arg> ServiceExt<Arg> for Swhere
S: Service<Arg>,
impl<S, Arg> ServiceExt<Arg> for Swhere
S: Service<Arg>,
Source§fn enclosed<T>(self, build: T) -> Pipeline<Self, T, BuildEnclosed>
fn enclosed<T>(self, build: T) -> Pipeline<Self, T, BuildEnclosed>
Enclose Self with given
T as Service<<Self as Service<_>>::Response>>
. In other word T
would take Self’s Service::Response
type as it’s generic argument of Service<_>
impl.Source§fn enclosed_fn<T, Req, O>(
self,
func: T,
) -> Pipeline<Self, AsyncFn<T>, BuildEnclosed>
fn enclosed_fn<T, Req, O>( self, func: T, ) -> Pipeline<Self, AsyncFn<T>, BuildEnclosed>
Function version of Self::enclosed method.
Source§fn map<F, Res, ResMap>(self, mapper: F) -> Pipeline<Self, Map<F>, BuildEnclosed>
fn map<F, Res, ResMap>(self, mapper: F) -> Pipeline<Self, Map<F>, BuildEnclosed>
Mutate
<<Self::Response as Service<Req>>::Future as Future>::Output
type with given
closure.