Remove AddExtensionLayer (#807)

* Remove `AddExtensionLayer`

Its deprecated on 0.4.x so we can remove it from `main`.

* changelog
This commit is contained in:
David Pedersen 2022-03-01 13:59:43 +01:00 committed by GitHub
parent 768d8a8218
commit 2de202da40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 51 deletions

View file

@ -56,6 +56,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `MatchedPathRejection`
- `WebSocketUpgradeRejection`
- **breaking:** `Redirect::found` has been removed ([#800])
- **breaking:** `AddExtensionLayer` has been removed. Use `Extension` instead. It now implements
`tower::Layer` ([#807])
- **fixed:** Set `Allow` header when responding with `405 Method Not Allowed` ([#733])
- **fixed:** Correctly set the `Content-Length` header for response to `HEAD`
requests ([#734])
@ -76,6 +78,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#797]: https://github.com/tokio-rs/axum/pull/797
[#800]: https://github.com/tokio-rs/axum/pull/800
[#801]: https://github.com/tokio-rs/axum/pull/801
[#807]: https://github.com/tokio-rs/axum/pull/807
# 0.4.4 (13. January, 2022)

View file

@ -2,41 +2,8 @@
use http::Request;
use std::task::{Context, Poll};
use tower_layer::Layer;
use tower_service::Service;
/// [`Layer`] 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 AddExtensionLayer<T> {
value: T,
}
impl<T> AddExtensionLayer<T> {
/// Create a new [`AddExtensionLayer`].
pub fn new(value: T) -> Self {
Self { value }
}
}
impl<S, T> Layer<S> for AddExtensionLayer<T>
where
T: Clone,
{
type Service = AddExtension<S, T>;
fn layer(&self, inner: S) -> Self::Service {
AddExtension {
inner,
value: self.value.clone(),
}
}
}
/// Middleware for adding some shareable value to [request extensions].
///
/// See [Sharing state with handlers](index.html#sharing-state-with-handlers)
@ -49,13 +16,6 @@ pub struct AddExtension<S, T> {
pub(crate) value: T,
}
impl<S, T> AddExtension<S, T> {
/// Create a new [`AddExtensionLayer`].
pub fn layer(value: T) -> AddExtensionLayer<T> {
AddExtensionLayer::new(value)
}
}
impl<ResBody, S, T> Service<Request<ResBody>> for AddExtension<S, T>
where
S: Service<Request<ResBody>>,

View file

@ -376,7 +376,7 @@ use axum::{
http::StatusCode,
response::{IntoResponse, Response},
routing::get,
AddExtensionLayer, Router,
Router,
};
#[derive(Clone)]
@ -416,7 +416,7 @@ async fn handler(user: AuthenticatedUser) {
let state = State { /* ... */ };
let app = Router::new().route("/", get(handler)).layer(AddExtensionLayer::new(state));
let app = Router::new().route("/", get(handler)).layer(Extension(state));
# async {
# axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
# };

View file

@ -23,7 +23,7 @@ once, instead of calling `layer` (or `route_layer`) repeatedly:
```rust
use axum::{
routing::get,
AddExtensionLayer,
Extension,
Router,
};
use tower_http::{trace::TraceLayer};
@ -39,7 +39,7 @@ let app = Router::new()
.layer(
ServiceBuilder::new()
.layer(TraceLayer::new_for_http())
.layer(AddExtensionLayer::new(State {}))
.layer(Extension(State {}))
);
# async {
# axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
@ -74,9 +74,9 @@ use axum::{routing::get, Router};
async fn handler() {}
# let layer_one = axum::AddExtensionLayer::new(());
# let layer_two = axum::AddExtensionLayer::new(());
# let layer_three = axum::AddExtensionLayer::new(());
# let layer_one = axum::Extension(());
# let layer_two = axum::Extension(());
# let layer_three = axum::Extension(());
#
let app = Router::new()
.route("/", get(handler))
@ -130,9 +130,9 @@ use axum::{routing::get, Router};
async fn handler() {}
# let layer_one = axum::AddExtensionLayer::new(());
# let layer_two = axum::AddExtensionLayer::new(());
# let layer_three = axum::AddExtensionLayer::new(());
# let layer_one = axum::Extension(());
# let layer_two = axum::Extension(());
# let layer_three = axum::Extension(());
#
let app = Router::new()
.route("/", get(handler))

View file

@ -409,7 +409,7 @@ pub mod routing;
#[cfg(test)]
mod test_helpers;
pub use add_extension::{AddExtension, AddExtensionLayer};
pub use add_extension::AddExtension;
#[doc(no_inline)]
pub use async_trait::async_trait;
#[cfg(feature = "headers")]