Struct actix_web::State

source ·
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

The resulting type after dereferencing.
Dereferences the value.
Configuration for conversion process
Future that resolves to a Self
Convert request to a Self
Convert request to a Self Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.