Export AddExtension from middleware (#811)

* Export `AddExtension` from `middleware`

* Move `AddExtension` into `extension` module
This commit is contained in:
David Pedersen 2022-03-01 14:59:33 +01:00 committed by GitHub
parent bad3abb960
commit 2428d99081
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 45 deletions

View file

@ -58,6 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **breaking:** `Redirect::found` has been removed ([#800]) - **breaking:** `Redirect::found` has been removed ([#800])
- **breaking:** `AddExtensionLayer` has been removed. Use `Extension` instead. It now implements - **breaking:** `AddExtensionLayer` has been removed. Use `Extension` instead. It now implements
`tower::Layer` ([#807]) `tower::Layer` ([#807])
- **breaking:** `AddExtension` has been moved from the root module to `middleware`
- **fixed:** Set `Allow` header when responding with `405 Method Not Allowed` ([#733]) - **fixed:** Set `Allow` header when responding with `405 Method Not Allowed` ([#733])
- **fixed:** Correctly set the `Content-Length` header for response to `HEAD` - **fixed:** Correctly set the `Content-Length` header for response to `HEAD`
requests ([#734]) requests ([#734])

View file

@ -1,37 +0,0 @@
// this is vendored from tower-http to reduce public dependencies
use http::Request;
use std::task::{Context, Poll};
use tower_service::Service;
/// Middleware for adding some shareable value to [request extensions].
///
/// See [Sharing state with handlers](index.html#sharing-state-with-handlers)
/// for more details.
///
/// [request extensions]: https://docs.rs/http/latest/http/struct.Extensions.html
#[derive(Clone, Copy, Debug)]
pub struct AddExtension<S, T> {
pub(crate) inner: S,
pub(crate) value: T,
}
impl<ResBody, S, T> Service<Request<ResBody>> for AddExtension<S, T>
where
S: Service<Request<ResBody>>,
T: Clone + Send + Sync + 'static,
{
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
#[inline]
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, mut req: Request<ResBody>) -> Self::Future {
req.extensions_mut().insert(self.value.clone());
self.inner.call(req)
}
}

View file

@ -1,8 +1,15 @@
use crate::extract::{rejection::*, FromRequest, RequestParts}; use crate::{
use crate::response::IntoResponseParts; extract::{rejection::*, FromRequest, RequestParts},
response::IntoResponseParts,
};
use async_trait::async_trait; use async_trait::async_trait;
use axum_core::response::{IntoResponse, Response, ResponseParts}; use axum_core::response::{IntoResponse, Response, ResponseParts};
use std::ops::Deref; use http::Request;
use std::{
ops::Deref,
task::{Context, Poll},
};
use tower_service::Service;
/// Extractor and response for extensions. /// Extractor and response for extensions.
/// ///
@ -120,12 +127,44 @@ impl<S, T> tower_layer::Layer<S> for Extension<T>
where where
T: Clone + Send + Sync + 'static, T: Clone + Send + Sync + 'static,
{ {
type Service = crate::AddExtension<S, T>; type Service = AddExtension<S, T>;
fn layer(&self, inner: S) -> Self::Service { fn layer(&self, inner: S) -> Self::Service {
crate::AddExtension { AddExtension {
inner, inner,
value: self.0.clone(), value: self.0.clone(),
} }
} }
} }
/// Middleware for adding some shareable value to [request extensions].
///
/// See [Sharing state with handlers](index.html#sharing-state-with-handlers)
/// for more details.
///
/// [request extensions]: https://docs.rs/http/latest/http/struct.Extensions.html
#[derive(Clone, Copy, Debug)]
pub struct AddExtension<S, T> {
pub(crate) inner: S,
pub(crate) value: T,
}
impl<ResBody, S, T> Service<Request<ResBody>> for AddExtension<S, T>
where
S: Service<Request<ResBody>>,
T: Clone + Send + Sync + 'static,
{
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
#[inline]
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, mut req: Request<ResBody>) -> Self::Future {
req.extensions_mut().insert(self.value.clone());
self.inner.call(req)
}
}

View file

@ -5,7 +5,7 @@
//! [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info //! [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info
use super::{Extension, FromRequest, RequestParts}; use super::{Extension, FromRequest, RequestParts};
use crate::AddExtension; use crate::middleware::AddExtension;
use async_trait::async_trait; use async_trait::async_trait;
use hyper::server::conn::AddrStream; use hyper::server::conn::AddrStream;
use std::{ use std::{

View file

@ -392,7 +392,6 @@
#[macro_use] #[macro_use]
pub(crate) mod macros; pub(crate) mod macros;
mod add_extension;
mod extension; mod extension;
#[cfg(feature = "json")] #[cfg(feature = "json")]
mod json; mod json;
@ -409,7 +408,6 @@ pub mod routing;
#[cfg(test)] #[cfg(test)]
mod test_helpers; mod test_helpers;
pub use add_extension::AddExtension;
#[doc(no_inline)] #[doc(no_inline)]
pub use async_trait::async_trait; pub use async_trait::async_trait;
#[cfg(feature = "headers")] #[cfg(feature = "headers")]

View file

@ -5,6 +5,7 @@
mod from_fn; mod from_fn;
pub use self::from_fn::{from_fn, FromFn, FromFnLayer, Next}; pub use self::from_fn::{from_fn, FromFn, FromFnLayer, Next};
pub use crate::extension::AddExtension;
pub mod future { pub mod future {
//! Future types. //! Future types.