mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-21 22:56:46 +01:00
Make FromRequest
default to use axum::body::Body
(#146)
Most users will implement `FromRequest<axum::body::Body>` so making that the default makes things a bit easier to use.
This commit is contained in:
parent
5922c4fa50
commit
3d45a97db9
3 changed files with 43 additions and 2 deletions
|
@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
# Unreleased
|
||||
|
||||
None.
|
||||
- Make `FromRequest` default to being generic over `axum::body::Body` ([#146](https://github.com/tokio-rs/axum/pull/146))
|
||||
|
||||
## Breaking changes
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ use http_body::Body as _;
|
|||
use std::{error::Error as StdError, fmt};
|
||||
use tower::BoxError;
|
||||
|
||||
#[doc(no_inline)]
|
||||
pub use hyper::body::Body;
|
||||
|
||||
/// A boxed [`Body`] trait object.
|
||||
|
|
|
@ -304,8 +304,48 @@ pub use self::typed_header::TypedHeader;
|
|||
/// Types that can be created from requests.
|
||||
///
|
||||
/// See the [module docs](crate::extract) for more details.
|
||||
///
|
||||
/// # What is the `B` type parameter?
|
||||
///
|
||||
/// `FromRequest` is generic over the request body (the `B` in
|
||||
/// [`http::Request<B>`]). This is to allow `FromRequest` to be usable will any
|
||||
/// type of request body. This is necessary because some middleware change the
|
||||
/// request body, for example to add timeouts.
|
||||
///
|
||||
/// If you're writing your own `FromRequest` that wont be used outside your
|
||||
/// application, and not using any middleware that changes the request body, you
|
||||
/// can most likely use `axum::body::Body`. Note this is also the default.
|
||||
///
|
||||
/// If you're writing a library, thats intended for others to use, its recommended
|
||||
/// to keep the generic type parameter:
|
||||
///
|
||||
/// ```rust
|
||||
/// use axum::{
|
||||
/// async_trait,
|
||||
/// extract::{FromRequest, RequestParts},
|
||||
/// };
|
||||
///
|
||||
/// struct MyExtractor;
|
||||
///
|
||||
/// #[async_trait]
|
||||
/// impl<B> FromRequest<B> for MyExtractor
|
||||
/// where
|
||||
/// B: Send, // required by `async_trait`
|
||||
/// {
|
||||
/// type Rejection = http::StatusCode;
|
||||
///
|
||||
/// async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
|
||||
/// // ...
|
||||
/// # unimplemented!()
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// This ensures your extractor is as flexible as possible.
|
||||
///
|
||||
/// [`http::Request<B>`]: http::Request
|
||||
#[async_trait]
|
||||
pub trait FromRequest<B>: Sized {
|
||||
pub trait FromRequest<B = crate::body::Body>: Sized {
|
||||
/// If the extractor fails it'll use this "rejection" type. A rejection is
|
||||
/// a kind of error that can be converted into a response.
|
||||
type Rejection: IntoResponse;
|
||||
|
|
Loading…
Reference in a new issue