pub trait Handler<T, S>:
Clone
+ Send
+ Sync
+ 'static {
// Required method
fn call(self, req: Request<Body>, state: Arc<S>) -> BoxedFuture;
}Expand description
Trait for types that can handle an HTTP request.
This is blanket-implemented for both async fns (which are marked with AsyncHandler) and
synchronous fn/closures (which are marked with SyncHandler).
§Performance & Zero-Allocation Routing
- Arity-0 handlers (no extractors) take the fast path: synchronous ones return
ResponseFuture::Readydirectly with zero box allocations; async ones are eagerly polled once (the same future instance is kept and reused if it turns out to be pending, never re-invoked, so any side effects before the first.awaitstill run exactly once) and only fall back to a boxed future (ResponseFuture::Boxed) if they actually yield (i.e. genuinely await something). - Handlers with ≥1 extractor always go through
ResponseFuture::Boxed, sync or async. This is because the last extractor implementsFromRequest, which isasync(bodies may be streamed in rather than already buffered — seecrate::routing::extract::BodyStream), so it can’t be resolved before decidingReadyvs.Boxed.
§⚠️ Thread Starvation & Blocking Warning
Because Tachyon runs on a cooperative async thread pool (Tokio), blocking any worker thread
with long-running synchronous code (e.g. std::fs::read or blocking database calls) will stall the event loop.
- DO: Use sync handlers ONLY for instant CPU operations (e.g., formatting data, static templates, or simple state reads).
- DON’T: Do heavy or blocking I/O synchronously inside sync handlers. Instead, use async versions or offload
blocking calls to
tokio::task::spawn_blocking.
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".