Struct rocket::State [] [src]

pub struct State<'r, T: Send + Sync + 'static>(_);

Request guard to retrieve managed state.

This type can be used as a request guard to retrieve the state Rocket is managing for some type T. This allows for the sharing of state across any number of handlers. A value for the given type must previously have been registered to be managed by Rocket via the manage method. The type being managed must be thread safe and sendable across thread boundaries. In other words, it must implement Send + Sync + 'static.

Example

Imagine you have some configuration struct of the type MyConfig that you'd like to initialize at start-up and later access it in several handlers. The following example does just this:

use rocket::State;

// In a real application, this would likely be more complex.
struct MyConfig(String);

#[get("/")]
fn index(state: State<MyConfig>) -> String {
    format!("The config value is: {}", state.0)
}

#[get("/raw")]
fn raw_config_value<'r>(state: State<'r, MyConfig>) -> &'r str {
    // use `inner()` to get a lifetime longer than `deref` gives us
    state.inner().0.as_str()
}

fn main() {
    let config = MyConfig("user input".to_string());
    rocket::ignite()
        .mount("/", routes![index, raw_config_value])
        .manage(config)
        .launch()
}

Methods

impl<'r, T: Send + Sync + 'static> State<'r, T>
[src]

[src]

Retrieve a borrow to the underyling value.

Using this method is typically unnecessary as State implements Deref with a Target of T. This means Rocket will automatically coerce a State<T> to an &T when the types call for it.

Trait Implementations

impl<'r, T: Debug + Send + Sync + 'static> Debug for State<'r, T>
[src]

[src]

Formats the value using the given formatter.

impl<'r, T: PartialEq + Send + Sync + 'static> PartialEq for State<'r, T>
[src]

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

[src]

This method tests for !=.

impl<'r, T: Eq + Send + Sync + 'static> Eq for State<'r, T>
[src]

impl<'a, 'r, T: Send + Sync + 'static> FromRequest<'a, 'r> for State<'r, T>
[src]

The associated error to be returned if derivation fails.

[src]

Derives an instance of Self from the incoming request metadata. Read more

impl<'r, T: Send + Sync + 'static> Deref for State<'r, T>
[src]

The resulting type after dereferencing.

[src]

Dereferences the value.