Make the B type parameter in Handler default to axum::body::Body (#527)

* Swap type params in `Handler`

* Default `B` type param in `Handler`
This commit is contained in:
David Pedersen
2021-11-16 23:00:39 +01:00
committed by GitHub
parent 1dc49ae039
commit 939995e80e
4 changed files with 29 additions and 25 deletions
+4
View File
@@ -20,9 +20,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`routing::{get, get_service, ..., MethodRouter}`. `routing::{get, get_service, ..., MethodRouter}`.
- **breaking:** `HandleErrorExt` has been removed in favor of - **breaking:** `HandleErrorExt` has been removed in favor of
`MethodRouter::handle_error`. `MethodRouter::handle_error`.
- **breaking:** The `Handler<B, T>` trait is now defined as `Handler<T, B =
Body>`. That is the type parameters have been swapped and `B` defaults to
`axum::body::Body` ([#527])
- Update WebSockets to use tokio-tungstenite 0.16 ([#525]) - Update WebSockets to use tokio-tungstenite 0.16 ([#525])
[#525]: https://github.com/tokio-rs/axum/pull/525 [#525]: https://github.com/tokio-rs/axum/pull/525
[#527]: https://github.com/tokio-rs/axum/pull/527
# 0.3.3 (13. November, 2021) # 0.3.3 (13. November, 2021)
+7 -7
View File
@@ -12,9 +12,9 @@ use tower_service::Service;
/// An adapter that makes a [`Handler`] into a [`Service`]. /// An adapter that makes a [`Handler`] into a [`Service`].
/// ///
/// Created with [`Handler::into_service`]. /// Created with [`Handler::into_service`].
pub struct IntoService<H, B, T> { pub struct IntoService<H, T, B> {
handler: H, handler: H,
_marker: PhantomData<fn() -> (B, T)>, _marker: PhantomData<fn() -> (T, B)>,
} }
#[test] #[test]
@@ -24,7 +24,7 @@ fn traits() {
assert_sync::<IntoService<(), NotSendSync, NotSendSync>>(); assert_sync::<IntoService<(), NotSendSync, NotSendSync>>();
} }
impl<H, B, T> IntoService<H, B, T> { impl<H, T, B> IntoService<H, T, B> {
pub(super) fn new(handler: H) -> Self { pub(super) fn new(handler: H) -> Self {
Self { Self {
handler, handler,
@@ -33,7 +33,7 @@ impl<H, B, T> IntoService<H, B, T> {
} }
} }
impl<H, B, T> fmt::Debug for IntoService<H, B, T> { impl<H, T, B> fmt::Debug for IntoService<H, T, B> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("IntoService") f.debug_tuple("IntoService")
.field(&format_args!("...")) .field(&format_args!("..."))
@@ -41,7 +41,7 @@ impl<H, B, T> fmt::Debug for IntoService<H, B, T> {
} }
} }
impl<H, B, T> Clone for IntoService<H, B, T> impl<H, T, B> Clone for IntoService<H, T, B>
where where
H: Clone, H: Clone,
{ {
@@ -53,9 +53,9 @@ where
} }
} }
impl<H, T, B> Service<Request<B>> for IntoService<H, B, T> impl<H, T, B> Service<Request<B>> for IntoService<H, T, B>
where where
H: Handler<B, T> + Clone + Send + 'static, H: Handler<T, B> + Clone + Send + 'static,
B: Send + 'static, B: Send + 'static,
{ {
type Response = Response<BoxBody>; type Response = Response<BoxBody>;
+10 -10
View File
@@ -60,7 +60,7 @@
//! | //! |
//! ::: axum/src/handler/mod.rs:116:8 //! ::: axum/src/handler/mod.rs:116:8
//! | //! |
//! 116 | H: Handler<B, T>, //! 116 | H: Handler<T, B>,
//! | ------------- required by this bound in `axum::routing::get` //! | ------------- required by this bound in `axum::routing::get`
//! ``` //! ```
//! //!
@@ -71,7 +71,7 @@
//! [axum-debug]: https://docs.rs/axum-debug //! [axum-debug]: https://docs.rs/axum-debug
use crate::{ use crate::{
body::{box_body, BoxBody}, body::{box_body, Body, BoxBody},
extract::{ extract::{
connect_info::{Connected, IntoMakeServiceWithConnectInfo}, connect_info::{Connected, IntoMakeServiceWithConnectInfo},
FromRequest, RequestParts, FromRequest, RequestParts,
@@ -108,7 +108,7 @@ pub(crate) mod sealed {
/// ///
/// See the [module docs](crate::handler) for more details. /// See the [module docs](crate::handler) for more details.
#[async_trait] #[async_trait]
pub trait Handler<B, T>: Clone + Send + Sized + 'static { pub trait Handler<T, B = Body>: Clone + Send + Sized + 'static {
// This seals the trait. We cannot use the regular "sealed super trait" // This seals the trait. We cannot use the regular "sealed super trait"
// approach due to coherence. // approach due to coherence.
#[doc(hidden)] #[doc(hidden)]
@@ -155,7 +155,7 @@ pub trait Handler<B, T>: Clone + Send + Sized + 'static {
/// ``` /// ```
fn layer<L>(self, layer: L) -> Layered<L::Service, T> fn layer<L>(self, layer: L) -> Layered<L::Service, T>
where where
L: Layer<IntoService<Self, B, T>>, L: Layer<IntoService<Self, T, B>>,
{ {
Layered::new(layer.layer(self.into_service())) Layered::new(layer.layer(self.into_service()))
} }
@@ -192,7 +192,7 @@ pub trait Handler<B, T>: Clone + Send + Sized + 'static {
/// ``` /// ```
/// ///
/// [`Router::fallback`]: crate::routing::Router::fallback /// [`Router::fallback`]: crate::routing::Router::fallback
fn into_service(self) -> IntoService<Self, B, T> { fn into_service(self) -> IntoService<Self, T, B> {
IntoService::new(self) IntoService::new(self)
} }
@@ -219,7 +219,7 @@ pub trait Handler<B, T>: Clone + Send + Sized + 'static {
/// ``` /// ```
/// ///
/// [`MakeService`]: tower::make::MakeService /// [`MakeService`]: tower::make::MakeService
fn into_make_service(self) -> IntoMakeService<IntoService<Self, B, T>> { fn into_make_service(self) -> IntoMakeService<IntoService<Self, T, B>> {
IntoMakeService::new(self.into_service()) IntoMakeService::new(self.into_service())
} }
@@ -253,7 +253,7 @@ pub trait Handler<B, T>: Clone + Send + Sized + 'static {
/// [`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
fn into_make_service_with_connect_info<C, Target>( fn into_make_service_with_connect_info<C, Target>(
self, self,
) -> IntoMakeServiceWithConnectInfo<IntoService<Self, B, T>, C> ) -> IntoMakeServiceWithConnectInfo<IntoService<Self, T, B>, C>
where where
C: Connected<Target>, C: Connected<Target>,
{ {
@@ -262,7 +262,7 @@ pub trait Handler<B, T>: Clone + Send + Sized + 'static {
} }
#[async_trait] #[async_trait]
impl<F, Fut, Res, B> Handler<B, ()> for F impl<F, Fut, Res, B> Handler<(), B> for F
where where
F: FnOnce() -> Fut + Clone + Send + 'static, F: FnOnce() -> Fut + Clone + Send + 'static,
Fut: Future<Output = Res> + Send, Fut: Future<Output = Res> + Send,
@@ -280,7 +280,7 @@ macro_rules! impl_handler {
( $($ty:ident),* $(,)? ) => { ( $($ty:ident),* $(,)? ) => {
#[async_trait] #[async_trait]
#[allow(non_snake_case)] #[allow(non_snake_case)]
impl<F, Fut, B, Res, $($ty,)*> Handler<B, ($($ty,)*)> for F impl<F, Fut, B, Res, $($ty,)*> Handler<($($ty,)*), B> for F
where where
F: FnOnce($($ty,)*) -> Fut + Clone + Send + 'static, F: FnOnce($($ty,)*) -> Fut + Clone + Send + 'static,
Fut: Future<Output = Res> + Send, Fut: Future<Output = Res> + Send,
@@ -352,7 +352,7 @@ where
} }
#[async_trait] #[async_trait]
impl<S, T, ReqBody, ResBody> Handler<ReqBody, T> for Layered<S, T> impl<S, T, ReqBody, ResBody> Handler<T, ReqBody> for Layered<S, T>
where where
S: Service<Request<ReqBody>, Response = Response<ResBody>> + Clone + Send + 'static, S: Service<Request<ReqBody>, Response = Response<ResBody>> + Clone + Send + 'static,
S::Error: IntoResponse, S::Error: IntoResponse,
+8 -8
View File
@@ -134,9 +134,9 @@ macro_rules! top_level_handler_fn {
$name:ident, $method:ident $name:ident, $method:ident
) => { ) => {
$(#[$m])+ $(#[$m])+
pub fn $name<H, B, T>(handler: H) -> MethodRouter<B, Infallible> pub fn $name<H, T, B>(handler: H) -> MethodRouter<B, Infallible>
where where
H: Handler<B, T>, H: Handler<T, B>,
B: Send + 'static, B: Send + 'static,
T: 'static, T: 'static,
{ {
@@ -271,7 +271,7 @@ macro_rules! chained_handler_fn {
$(#[$m])+ $(#[$m])+
pub fn $name<H, T>(self, handler: H) -> Self pub fn $name<H, T>(self, handler: H) -> Self
where where
H: Handler<B, T>, H: Handler<T, B>,
T: 'static, T: 'static,
{ {
self.on(MethodFilter::$method, handler) self.on(MethodFilter::$method, handler)
@@ -417,9 +417,9 @@ top_level_handler_fn!(trace, TRACE);
/// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); /// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
/// # }; /// # };
/// ``` /// ```
pub fn on<H, B, T>(filter: MethodFilter, handler: H) -> MethodRouter<B, Infallible> pub fn on<H, T, B>(filter: MethodFilter, handler: H) -> MethodRouter<B, Infallible>
where where
H: Handler<B, T>, H: Handler<T, B>,
B: Send + 'static, B: Send + 'static,
T: 'static, T: 'static,
{ {
@@ -463,9 +463,9 @@ where
/// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); /// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
/// # }; /// # };
/// ``` /// ```
pub fn any<H, B, T>(handler: H) -> MethodRouter<B, Infallible> pub fn any<H, T, B>(handler: H) -> MethodRouter<B, Infallible>
where where
H: Handler<B, T>, H: Handler<T, B>,
B: Send + 'static, B: Send + 'static,
T: 'static, T: 'static,
{ {
@@ -557,7 +557,7 @@ where
/// ``` /// ```
pub fn on<H, T>(self, filter: MethodFilter, handler: H) -> Self pub fn on<H, T>(self, filter: MethodFilter, handler: H) -> Self
where where
H: Handler<B, T>, H: Handler<T, B>,
T: 'static, T: 'static,
{ {
self.on_service_boxed_response_body(filter, handler.into_service()) self.on_service_boxed_response_body(filter, handler.into_service())