From 939995e80edaf4e882a6e2decb07db5a7c0a2c06 Mon Sep 17 00:00:00 2001
From: David Pedersen <david.pdrsn@gmail.com>
Date: Tue, 16 Nov 2021 23:00:39 +0100
Subject: [PATCH] Make the `B` type parameter in `Handler` default to
 `axum::body::Body` (#527)

* Swap type params in `Handler`

* Default `B` type param in `Handler`
---
 axum/CHANGELOG.md                  |  4 ++++
 axum/src/handler/into_service.rs   | 14 +++++++-------
 axum/src/handler/mod.rs            | 20 ++++++++++----------
 axum/src/routing/method_routing.rs | 16 ++++++++--------
 4 files changed, 29 insertions(+), 25 deletions(-)

diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md
index be8f2af2..e865fa75 100644
--- a/axum/CHANGELOG.md
+++ b/axum/CHANGELOG.md
@@ -20,9 +20,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
   `routing::{get, get_service, ..., MethodRouter}`.
 - **breaking:** `HandleErrorExt` has been removed in favor of
   `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])
 
 [#525]: https://github.com/tokio-rs/axum/pull/525
+[#527]: https://github.com/tokio-rs/axum/pull/527
 
 # 0.3.3 (13. November, 2021)
 
diff --git a/axum/src/handler/into_service.rs b/axum/src/handler/into_service.rs
index 7b116bb6..613fd159 100644
--- a/axum/src/handler/into_service.rs
+++ b/axum/src/handler/into_service.rs
@@ -12,9 +12,9 @@ use tower_service::Service;
 /// An adapter that makes a [`Handler`] into a [`Service`].
 ///
 /// Created with [`Handler::into_service`].
-pub struct IntoService<H, B, T> {
+pub struct IntoService<H, T, B> {
     handler: H,
-    _marker: PhantomData<fn() -> (B, T)>,
+    _marker: PhantomData<fn() -> (T, B)>,
 }
 
 #[test]
@@ -24,7 +24,7 @@ fn traits() {
     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 {
         Self {
             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 {
         f.debug_tuple("IntoService")
             .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
     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
-    H: Handler<B, T> + Clone + Send + 'static,
+    H: Handler<T, B> + Clone + Send + 'static,
     B: Send + 'static,
 {
     type Response = Response<BoxBody>;
diff --git a/axum/src/handler/mod.rs b/axum/src/handler/mod.rs
index 68e80045..6c3603a5 100644
--- a/axum/src/handler/mod.rs
+++ b/axum/src/handler/mod.rs
@@ -60,7 +60,7 @@
 //!     |
 //!    ::: 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`
 //! ```
 //!
@@ -71,7 +71,7 @@
 //! [axum-debug]: https://docs.rs/axum-debug
 
 use crate::{
-    body::{box_body, BoxBody},
+    body::{box_body, Body, BoxBody},
     extract::{
         connect_info::{Connected, IntoMakeServiceWithConnectInfo},
         FromRequest, RequestParts,
@@ -108,7 +108,7 @@ pub(crate) mod sealed {
 ///
 /// See the [module docs](crate::handler) for more details.
 #[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"
     // approach due to coherence.
     #[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>
     where
-        L: Layer<IntoService<Self, B, T>>,
+        L: Layer<IntoService<Self, T, B>>,
     {
         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
-    fn into_service(self) -> IntoService<Self, B, T> {
+    fn into_service(self) -> IntoService<Self, T, B> {
         IntoService::new(self)
     }
 
@@ -219,7 +219,7 @@ pub trait Handler<B, T>: Clone + Send + Sized + 'static {
     /// ```
     ///
     /// [`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())
     }
 
@@ -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
     fn into_make_service_with_connect_info<C, Target>(
         self,
-    ) -> IntoMakeServiceWithConnectInfo<IntoService<Self, B, T>, C>
+    ) -> IntoMakeServiceWithConnectInfo<IntoService<Self, T, B>, C>
     where
         C: Connected<Target>,
     {
@@ -262,7 +262,7 @@ pub trait Handler<B, T>: Clone + Send + Sized + 'static {
 }
 
 #[async_trait]
-impl<F, Fut, Res, B> Handler<B, ()> for F
+impl<F, Fut, Res, B> Handler<(), B> for F
 where
     F: FnOnce() -> Fut + Clone + Send + 'static,
     Fut: Future<Output = Res> + Send,
@@ -280,7 +280,7 @@ macro_rules! impl_handler {
     ( $($ty:ident),* $(,)? ) => {
         #[async_trait]
         #[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
             F: FnOnce($($ty,)*) -> Fut + Clone + Send + 'static,
             Fut: Future<Output = Res> + Send,
@@ -352,7 +352,7 @@ where
 }
 
 #[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
     S: Service<Request<ReqBody>, Response = Response<ResBody>> + Clone + Send + 'static,
     S::Error: IntoResponse,
diff --git a/axum/src/routing/method_routing.rs b/axum/src/routing/method_routing.rs
index 271dfa45..1f76a799 100644
--- a/axum/src/routing/method_routing.rs
+++ b/axum/src/routing/method_routing.rs
@@ -134,9 +134,9 @@ macro_rules! top_level_handler_fn {
         $name:ident, $method:ident
     ) => {
         $(#[$m])+
-        pub fn $name<H, B, T>(handler: H) -> MethodRouter<B, Infallible>
+        pub fn $name<H, T, B>(handler: H) -> MethodRouter<B, Infallible>
         where
-            H: Handler<B, T>,
+            H: Handler<T, B>,
             B: Send + 'static,
             T: 'static,
         {
@@ -271,7 +271,7 @@ macro_rules! chained_handler_fn {
         $(#[$m])+
         pub fn $name<H, T>(self, handler: H) -> Self
         where
-            H: Handler<B, T>,
+            H: Handler<T, B>,
             T: 'static,
         {
             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();
 /// # };
 /// ```
-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
-    H: Handler<B, T>,
+    H: Handler<T, B>,
     B: Send + 'static,
     T: 'static,
 {
@@ -463,9 +463,9 @@ where
 /// # 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
-    H: Handler<B, T>,
+    H: Handler<T, B>,
     B: Send + 'static,
     T: 'static,
 {
@@ -557,7 +557,7 @@ where
     /// ```
     pub fn on<H, T>(self, filter: MethodFilter, handler: H) -> Self
     where
-        H: Handler<B, T>,
+        H: Handler<T, B>,
         T: 'static,
     {
         self.on_service_boxed_response_body(filter, handler.into_service())