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> Freeze for ContextBuilder<CF>where
CF: Freeze,
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, 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.