1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//! Handler trait and type erasure.
//!
//! # How async handlers are stored
//!
//! The router holds handlers of *different* concrete types in one
//! `HashMap<Method, Tree>`. Rust collections hold one type. So we use
//! **trait objects** (`dyn ErasedHandler`) to erase the concrete type and
//! store everything uniformly. This is standard Rust — not magic.
//!
//! The chain from user code to vtable call is:
//!
//! ```text
//! async fn hello(req: Request) -> Response { … } ← user writes this
//! ↓ router.get("/", hello)
//! hello.into_boxed_handler() ← Handler blanket impl
//! ↓
//! Arc::new(FnHandler(hello)) ← heap-allocated wrapper
//! ↓ stored as BoxedHandler = Arc<dyn ErasedHandler>
//! handler.call(req) at request time ← one vtable dispatch
//! ↓
//! Box::pin(async { hello(req).await.into_response() }) ← BoxFuture
//! ```
//!
//! The only runtime cost per request is **one Arc clone** (atomic inc) +
//! **one virtual call** — negligible compared to network I/O.
use Future;
use Pin;
use Arc;
use crateRequest;
use crate;
// ── Internal types ────────────────────────────────────────────────────────────
/// A heap-allocated, type-erased future that resolves to a [`Response`].
///
/// `Pin<Box<…>>` is required because the async runtime must be able to poll
/// the future in-place — it cannot move it in memory after the first poll.
/// `Send + 'static` let tokio move the future across threads safely.
pub type BoxFuture = ;
/// Internal dispatch interface.
///
/// `#[doc(hidden)] pub` rather than `pub(crate)` because it appears in the
/// return type of the public `Handler` trait's `into_boxed_handler` method.
/// External crates cannot usefully interact with this trait.
/// A heap-allocated, type-erased handler shared across concurrent requests.
///
/// `#[doc(hidden)] pub` for the same reason as `ErasedHandler`.
/// `Arc` gives us cheap, thread-safe shared ownership (one atomic reference
/// count increment per request) without copying the handler.
pub type BoxedHandler = ;
// ── Public Handler trait ──────────────────────────────────────────────────────
/// Implemented for every valid route handler.
///
/// You never implement this yourself. It is automatically satisfied for any
/// `async fn` with the signature:
///
/// ```text
/// async fn name(req: Request) -> impl IntoResponse
/// ```
///
/// The trait is **sealed** (via the private `Sealed` supertrait): only the
/// blanket impl below can satisfy it. This prevents accidental misuse and
/// keeps the API surface stable across versions.
/// The sealing module. Because `Sealed` is private, external crates cannot
/// name it and therefore cannot implement `Handler` on their own types.
// ── Blanket implementations ───────────────────────────────────────────────────
/// Implement the sealing trait for any function with the right signature.
///
/// `Fn(Request) -> Fut` covers:
/// - named `async fn` items
/// - `async` closures (when they stabilise)
/// - any struct that implements `Fn`
/// Implement `Handler` for any function with the right signature.
// ── Concrete wrapper ──────────────────────────────────────────────────────────
/// Newtype wrapper that holds a concrete handler `F` and implements
/// [`ErasedHandler`], bridging the typed world to the trait-object world.
;