Skip to main content

RequestScoped

Struct RequestScoped 

Source
pub struct RequestScoped<T: Send + Sync + 'static>(/* private fields */);
Expand description

Axum extractor for a provider resolved from the active Nidus request scope.

Attach crate::middleware::request_scope_layer with the application nidus_core::Container before using this extractor. The requested type must be registered in the container, commonly with Container::register_request or Container::register_request_scoped.

Missing middleware rejects with 500 Internal Server Error and request_scope_unavailable. A provider resolution failure also returns 500, with request_scope_resolution_failed.

RequestScoped<T> dereferences to T for handler reads. Use Self::into_inner when you need the shared Arc<T>, or Self::into_inject when passing the value to APIs that expect Nidus’ Inject<T> wrapper.

use std::sync::Arc;
use axum::{Router, routing::get};
use nidus_core::Container;
use nidus_http::{RequestScoped, middleware::request_scope_layer};

struct CurrentTenant(String);

async fn handler(tenant: RequestScoped<CurrentTenant>) -> String {
    tenant.0.clone()
}

let mut container = Container::new();
container.register_request::<CurrentTenant, _>(|_container| {
    Ok(CurrentTenant("demo".to_owned()))
})?;

let app = Router::new()
    .route("/tenant", get(handler))
    .layer(request_scope_layer(Arc::new(container)));

Implementations§

Source§

impl<T> RequestScoped<T>
where T: Send + Sync + 'static,

Source

pub fn new(value: Inject<T>) -> Self

Creates a request-scoped extractor value from an injected dependency.

Most application code receives this from Axum extraction rather than constructing it manually.

Source

pub fn into_inject(self) -> Inject<T>

Returns the underlying injected dependency wrapper.

Use this when downstream Nidus APIs need the injection wrapper rather than a borrowed T or shared Arc<T>.

Source

pub fn into_inner(self) -> Arc<T>

Returns a cloned shared pointer to the resolved dependency.

This is useful when spawning work that must own the provider beyond the handler’s borrow.

Trait Implementations§

Source§

impl<T: Clone + Send + Sync + 'static> Clone for RequestScoped<T>

Source§

fn clone(&self) -> RequestScoped<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Send + Sync + 'static> Debug for RequestScoped<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for RequestScoped<T>
where T: Send + Sync + 'static,

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<S, T> FromRequestParts<S> for RequestScoped<T>
where S: Send + Sync, T: Send + Sync + 'static,

Source§

type Rejection = RequestScopeRejection

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

fn from_request_parts( parts: &mut Parts, _state: &S, ) -> impl Future<Output = Result<Self, Self::Rejection>> + Send

Perform the extraction.

Auto Trait Implementations§

§

impl<T> Freeze for RequestScoped<T>

§

impl<T> RefUnwindSafe for RequestScoped<T>
where T: RefUnwindSafe,

§

impl<T> Send for RequestScoped<T>

§

impl<T> Sync for RequestScoped<T>

§

impl<T> Unpin for RequestScoped<T>

§

impl<T> UnsafeUnpin for RequestScoped<T>

§

impl<T> UnwindSafe for RequestScoped<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Formattable for T
where T: Deref, <T as Deref>::Target: Formattable,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<S, T> FromRequest<S, ViaParts> for T
where S: Send + Sync, T: FromRequestParts<S>,

Source§

type Rejection = <T as FromRequestParts<S>>::Rejection

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>

Perform the extraction.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Provider for T
where T: Send + Sync + 'static,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more