pub struct AsyncAppWithState<S> { /* private fields */ }http2 only.Expand description
An Application whose route handlers are async functions.
State is stored as Arc<S> and passed by value (cheap clone) to each
handler invocation. Handlers receive owned Request, PathParams, and
ConnectionInfo values so the returned future is 'static.
Implementations§
Source§impl<S: Send + Sync + 'static> AsyncAppWithState<S>
impl<S: Send + Sync + 'static> AsyncAppWithState<S>
Sourcepub fn with_config(self, config: ServerConfig) -> Self
pub fn with_config(self, config: ServerConfig) -> Self
Pin the fallback App (used for any request this app’s own routes
don’t match) to an explicit crate::server_config::ServerConfig,
instead of reading RWS_CONFIG_* environment variables per request.
Mirrors App::with_config / crate::state::AppWithState::with_config.
Sourcepub fn route_entries(&self) -> Vec<RouteInfo>
pub fn route_entries(&self) -> Vec<RouteInfo>
Return a snapshot of all registered routes as (method, pattern) pairs.
Sourcepub fn route_conflicts(&self) -> Vec<RouteConflict>
pub fn route_conflicts(&self) -> Vec<RouteConflict>
Routes that never run because an earlier one with the same method
matches every path they do (18.28.0). See
Router::route_conflicts;
each is also printed once to stderr when it’s registered.
Sourcepub fn route<H: AsyncHandler<S>>(self, handler: H) -> Self
pub fn route<H: AsyncHandler<S>>(self, handler: H) -> Self
Register a typed handler (18.28.0): an async fn (or a sync
function) with extractor arguments annotated #[get("/path")] &
co.; the method and pattern come from the attribute.
#[get("/posts/{id}")]
async fn show_post(Path(id): Path<i64>, State(pool): State<DbPool>) -> Result<Json<Post>, AppError> {
// … .await …
}
let app = App::with_async_state(pool).route(show_post);Sourcepub fn get<F, Fut>(self, pattern: &str, handler: F) -> Selfwhere
F: Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Response> + Send + 'static,
pub fn get<F, Fut>(self, pattern: &str, handler: F) -> Selfwhere
F: Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Response> + Send + 'static,
Register an async GET handler for pattern.
Sourcepub fn post<F, Fut>(self, pattern: &str, handler: F) -> Selfwhere
F: Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Response> + Send + 'static,
pub fn post<F, Fut>(self, pattern: &str, handler: F) -> Selfwhere
F: Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Response> + Send + 'static,
Register an async POST handler for pattern.
Sourcepub fn put<F, Fut>(self, pattern: &str, handler: F) -> Selfwhere
F: Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Response> + Send + 'static,
pub fn put<F, Fut>(self, pattern: &str, handler: F) -> Selfwhere
F: Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Response> + Send + 'static,
Register an async PUT handler for pattern.
Sourcepub fn patch<F, Fut>(self, pattern: &str, handler: F) -> Selfwhere
F: Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Response> + Send + 'static,
pub fn patch<F, Fut>(self, pattern: &str, handler: F) -> Selfwhere
F: Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Response> + Send + 'static,
Register an async PATCH handler for pattern.
Sourcepub fn delete<F, Fut>(self, pattern: &str, handler: F) -> Selfwhere
F: Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Response> + Send + 'static,
pub fn delete<F, Fut>(self, pattern: &str, handler: F) -> Selfwhere
F: Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Response> + Send + 'static,
Register an async DELETE handler for pattern.
Sourcepub fn openapi(self, config: OpenApiConfig) -> Self
Available on crate feature openapi only.
pub fn openapi(self, config: OpenApiConfig) -> Self
openapi only.Add GET /openapi.json (a generated OpenAPI 3.1 document covering
every route registered so far) and GET /docs (Swagger UI, loaded
from a CDN, pointed at /openapi.json).
Call this after registering your routes — routes added afterward still work but won’t appear in the generated spec, since it’s built once at this call rather than read dynamically per request.
Requires the openapi feature (and http2, since AsyncAppWithState
already requires it).
Trait Implementations§
Source§impl<S: Send + Sync + 'static> Application for AsyncAppWithState<S>
impl<S: Send + Sync + 'static> Application for AsyncAppWithState<S>
Source§fn execute(
&self,
request: &Request,
connection: &ConnectionInfo,
) -> Result<Response, String>
fn execute( &self, request: &Request, connection: &ConnectionInfo, ) -> Result<Response, String>
is_matching / process and return the first match.Source§fn registered_routes(&self) -> Vec<RouteInfo>
fn registered_routes(&self) -> Vec<RouteInfo>
(method, pattern) —
what AppWithState::route_entries returns; wrappers
(WithMiddleware, McpServer) report the routes of the
application inside. For tools that describe an app (the dev MCP
server’s routes tool), not for dispatch. Default: none. Since
18.28.0.Source§fn runs_inline(&self, _request: &Request, _connection: &ConnectionInfo) -> bool
fn runs_inline(&self, _request: &Request, _connection: &ConnectionInfo) -> bool
true when executing request is quick and never waits on anything
but the local filesystem — health checks, metrics, small static files.
The async servers (HTTP/1.1, HTTP/2, HTTP/3) then call execute
directly on the event loop, as nginx does, instead of handing the
request to tokio’s blocking pool: that handoff (a shared lock and two
cross-thread wake-ups) costs more than serving a small file. Read moreSource§fn execute_async<'a>(
&'a self,
_request: &'a Request,
_connection: &'a ConnectionInfo,
) -> Option<AsyncResponse<'a>>
fn execute_async<'a>( &'a self, _request: &'a Request, _connection: &'a ConnectionInfo, ) -> Option<AsyncResponse<'a>>
execute for this request, when the
application has one: the async servers await it on the connection’s
task instead of tying up a blocking-pool thread while it waits (the
config-driven proxy uses this to wait for a backend without holding a
thread). None (the default) means “use execute”. Wrappers return
Some only when they add no synchronous work of their own.Source§fn streams_body(&self, _request: &Request, _connection: &ConnectionInfo) -> bool
fn streams_body(&self, _request: &Request, _connection: &ConnectionInfo) -> bool
true to receive this request’s body as a stream (Body::Stream)
instead of read into memory first: execute then runs while the
body is still arriving, on a blocking-pool thread, and reads it with
std::io::Read — a multi-gigabyte upload in a few chunks of memory.
Asked after the head is read, so request.body is empty here;
decide from the method, path and headers. Read moreSource§fn middleware_names(&self) -> Vec<&'static str>
fn middleware_names(&self) -> Vec<&'static str>
Middleware::layer_name).
Default: none; WithMiddleware lists its layers, then the inner
application’s. Since 18.28.0.Auto Trait Implementations§
impl<S> !RefUnwindSafe for AsyncAppWithState<S>
impl<S> !UnwindSafe for AsyncAppWithState<S>
impl<S> Freeze for AsyncAppWithState<S>
impl<S> Send for AsyncAppWithState<S>
impl<S> Sync for AsyncAppWithState<S>
impl<S> Unpin for AsyncAppWithState<S>
impl<S> UnsafeUnpin for AsyncAppWithState<S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more