Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RequestParts::extract #897

Merged
merged 1 commit into from Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion axum-core/CHANGELOG.md
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- None.
- **added:** Add `RequestParts::extract` which allows applying an extractor as a method call

# 0.2.0 (31. March, 2022)

Expand Down
32 changes: 32 additions & 0 deletions axum-core/src/extract/mod.rs
Expand Up @@ -113,6 +113,38 @@ impl<B> RequestParts<B> {
}
}

/// Apply an extractor to this `RequestParts`.
///
/// `req.extract::<Extractor>()` is equivalent to `Extractor::from_request(req)`.
/// This function simply exists as a convenience.
///
/// # Example
///
/// ```
/// # struct MyExtractor {}
///
/// use std::convert::Infallible;
///
/// use async_trait::async_trait;
/// use axum::extract::{FromRequest, RequestParts};
/// use http::{Method, Uri};
///
/// #[async_trait]
/// impl<B: Send> FromRequest<B> for MyExtractor {
/// type Rejection = Infallible;
///
/// async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Infallible> {
/// let method = req.extract::<Method>().await?;
/// let path = req.extract::<Uri>().await?.path().to_owned();
///
/// todo!()
/// }
/// }
/// ```
pub async fn extract<E: FromRequest<B>>(&mut self) -> Result<E, E::Rejection> {
E::from_request(self).await
}

/// Convert this `RequestParts` back into a [`Request`].
///
/// Fails if The request body has been extracted, that is [`take_body`] has
Expand Down
2 changes: 1 addition & 1 deletion axum/CHANGELOG.md
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- None.
- **added:** Add `RequestParts::extract` which allows applying an extractor as a method call

# 0.5.0 (31. March, 2022)

Expand Down