Skip to content

Commit 76268ae

Browse files
authored
Document type parameter T on Handler (#3435)
Signed-off-by: Olivier 'reivilibre <[email protected]>
1 parent dfc15bc commit 76268ae

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

axum/src/handler/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,23 @@ pub use self::service::HandlerService;
125125
/// )));
126126
/// # let _: Router = app;
127127
/// ```
128+
///
129+
/// # About type parameter `T`
130+
///
131+
/// **Generally you shouldn't need to worry about `T`**; when calling methods such as
132+
/// [`post`](crate::routing::method_routing::post) it will be automatically inferred and this is
133+
/// the intended way for this parameter to be provided in application code.
134+
///
135+
/// If you are implementing your own methods that accept implementations of `Handler` as
136+
/// arguments, then the following may be useful:
137+
///
138+
/// The type parameter `T` is a workaround for trait coherence rules, allowing us to
139+
/// write blanket implementations of `Handler` over many types of handler functions
140+
/// with different numbers of arguments, without the compiler forbidding us from doing
141+
/// so because one type `F` can in theory implement both `Fn(A) -> X` and `Fn(A, B) -> Y`.
142+
/// `T` is a placeholder taking on a representation of the parameters of the handler function,
143+
/// as well as other similar 'coherence rule workaround' discriminators,
144+
/// allowing us to select one function signature to use as a `Handler`.
128145
#[diagnostic::on_unimplemented(
129146
note = "Consider using `#[axum::debug_handler]` to improve the error message"
130147
)]

0 commit comments

Comments
 (0)