pub struct State<S>(_);
Expand description
Access an application state
S
- application state type
Example
#[macro_use] extern crate serde_derive;
use actix_web::{http, App, Path, State};
/// Application state
struct MyApp {
msg: &'static str,
}
#[derive(Deserialize)]
struct Info {
username: String,
}
/// extract path info using serde
fn index(state: State<MyApp>, path: Path<Info>) -> String {
format!("{} {}!", state.msg, path.username)
}
fn main() {
let app = App::with_state(MyApp { msg: "Welcome" }).resource(
"/{username}/index.html", // <- define path parameters
|r| r.method(http::Method::GET).with(index),
); // <- use `with` extractor
}
Trait Implementations
sourceimpl<S> FromRequest<S> for State<S>
impl<S> FromRequest<S> for State<S>
sourcefn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result
fn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result
Convert request to a Self
sourcefn extract(req: &HttpRequest<S>) -> Self::Result
fn extract(req: &HttpRequest<S>) -> Self::Result
Convert request to a Self Read more
Auto Trait Implementations
impl<S> !RefUnwindSafe for State<S>
impl<S> !Send for State<S>
impl<S> !Sync for State<S>
impl<S> Unpin for State<S>
impl<S> !UnwindSafe for State<S>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more