Struct xitca_http::util::middleware::context::ContextBuilder
source · pub struct ContextBuilder<CF> { /* private fields */ }Expand description
ServiceFactory type for constructing compile time checked stateful service.
State is roughly doing the same thing as move || style closure capture. The difference comes
down to:
-
The captured state is constructed lazily when Service::call method is called.
-
State can be referenced in nested types and beyond closures.
§Example:
// function service.
async fn state_handler(req: Context<'_, String, String>) -> Result<String, Infallible> {
let (parent_req, state) = req.into_parts();
assert_eq!(state, "string_state");
Ok(String::from("string_response"))
}
// Construct Stateful service builder with closure.
let service = fn_service(state_handler)
// Stateful service builder would construct given service builder and pass (&State, Req) to it's
// Service::call method.
.enclosed(ContextBuilder::new(|| async { Ok::<_, Infallible>(String::from("string_state")) }))
.call(())
.await
.unwrap();
let req = String::default();
let res = service.call(req).await.unwrap();
assert_eq!(res, "string_response");
Implementations§
Trait Implementations§
source§impl<CF, Fut, C, CErr, S, E> Service<Result<S, E>> for ContextBuilder<CF>
impl<CF, Fut, C, CErr, S, E> Service<Result<S, E>> for ContextBuilder<CF>
Auto Trait Implementations§
impl<CF> RefUnwindSafe for ContextBuilder<CF>where
CF: RefUnwindSafe,
impl<CF> Send for ContextBuilder<CF>where
CF: Send,
impl<CF> Sync for ContextBuilder<CF>where
CF: Sync,
impl<CF> Unpin for ContextBuilder<CF>where
CF: Unpin,
impl<CF> UnwindSafe for ContextBuilder<CF>where
CF: 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> 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>(self, func: T) -> Pipeline<Self, T, BuildEnclosedFn>
fn enclosed_fn<T, Req>(self, func: T) -> Pipeline<Self, T, BuildEnclosedFn>
Function version of Self::enclosed method.
source§fn map<F, Res, ResMap>(self, mapper: F) -> Pipeline<Self, F, BuildMap>
fn map<F, Res, ResMap>(self, mapper: F) -> Pipeline<Self, F, BuildMap>
Mutate
<<Self::Response as Service<Req>>::Future as Future>::Output type with given
closure.