pub trait HandlerCallWithExtractors<T, S>: Sized {
type Future: Future<Output = Response> + Send + 'static;
// Required method
fn call(
self,
extractors: T,
state: S,
) -> <Self as HandlerCallWithExtractors<T, S>>::Future;
// Provided methods
fn into_handler(self) -> IntoHandler<Self, T, S> { ... }
fn or<R, Rt>(self, rhs: R) -> Or<Self, R, T, Rt, S>
where R: HandlerCallWithExtractors<Rt, S> { ... }
}
Expand description
Trait for async functions that can be used to handle requests.
This trait is similar to Handler
but rather than taking the request it takes the extracted
inputs.
The drawbacks of this trait is that you cannot apply middleware to individual handlers like you
can with Handler::layer
.
Required Associated Types§
Required Methods§
Sourcefn call(
self,
extractors: T,
state: S,
) -> <Self as HandlerCallWithExtractors<T, S>>::Future
fn call( self, extractors: T, state: S, ) -> <Self as HandlerCallWithExtractors<T, S>>::Future
Call the handler with the extracted inputs.
Provided Methods§
Sourcefn into_handler(self) -> IntoHandler<Self, T, S>
fn into_handler(self) -> IntoHandler<Self, T, S>
Convert this HandlerCallWithExtractors
into Handler
.
Sourcefn or<R, Rt>(self, rhs: R) -> Or<Self, R, T, Rt, S>where
R: HandlerCallWithExtractors<Rt, S>,
fn or<R, Rt>(self, rhs: R) -> Or<Self, R, T, Rt, S>where
R: HandlerCallWithExtractors<Rt, S>,
Chain two handlers together, running the second one if the first one rejects.
Note that this only moves to the next handler if an extractor fails. The response from handlers are not considered.
§Example
use axum_extra::handler::HandlerCallWithExtractors;
use axum::{
Router,
async_trait,
routing::get,
extract::FromRequestParts,
};
// handlers for varying levels of access
async fn admin(admin: AdminPermissions) {
// request came from an admin
}
async fn user(user: User) {
// we have a `User`
}
async fn guest() {
// `AdminPermissions` and `User` failed, so we're just a guest
}
// extractors for checking permissions
struct AdminPermissions {}
#[async_trait]
impl<S> FromRequestParts<S> for AdminPermissions
where
S: Send + Sync,
{
// check for admin permissions...
}
struct User {}
#[async_trait]
impl<S> FromRequestParts<S> for User
where
S: Send + Sync,
{
// check for a logged in user...
}
let app = Router::new().route(
"/users/:id",
get(
// first try `admin`, if that rejects run `user`, finally falling back
// to `guest`
admin.or(user).or(guest)
)
);
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.