Implement `FromRequest` for `http::Extensions` (#169)

Not sure its very useful but odd to not provide this. All other request
parts have an extractor and we already have the rejection for it.
This commit is contained in:
David Pedersen 2021-08-08 20:01:06 +02:00 committed by GitHub
parent 6b218c7150
commit 8500ea256d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `RoutingDsl::or` for combining routes ([#108](https://github.com/tokio-rs/axum/pull/108))
- Add `handle_error` to `service::OnMethod` ([#160](https://github.com/tokio-rs/axum/pull/160))
- Add `NestedUri` for extracting request URI in nested services ([#161](https://github.com/tokio-rs/axum/pull/161))
- Implement `FromRequest` for `http::Extensions`
## Breaking changes

View File

@ -2,7 +2,7 @@ use super::{rejection::*, take_body, Extension, FromRequest, RequestParts};
use async_trait::async_trait;
use bytes::Bytes;
use futures_util::stream::Stream;
use http::{HeaderMap, Method, Request, Uri, Version};
use http::{Extensions, HeaderMap, Method, Request, Uri, Version};
use std::{
convert::Infallible,
pin::Pin,
@ -148,6 +148,18 @@ where
}
}
#[async_trait]
impl<B> FromRequest<B> for Extensions
where
B: Send,
{
type Rejection = ExtensionsAlreadyExtracted;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
req.take_extensions().ok_or(ExtensionsAlreadyExtracted)
}
}
/// Extractor that extracts the request body as a [`Stream`].
///
/// # Example