Skip to main content

FromRef

Trait FromRef 

Source
pub trait FromRef<S>: Sized {
    // Required method
    fn from_ref(state: &S) -> Self;
}
Expand description

Produces a value from a reference to the shared application state S.

It is the bridge State<T> uses to pull a sub-value out of the state: a handler taking State<T> resolves when T: FromRef<S>. Derive it on the state struct with FromRef to get an impl per field (each cloning that field), or implement it by hand to derive a value from several fields.

§Examples

use ruststream::prelude::*;

#[derive(Clone)]
struct Db;

struct AppState {
    db: Db,
}

// What `#[derive(FromRef)]` generates for each field.
impl FromRef<AppState> for Db {
    fn from_ref(state: &AppState) -> Db {
        state.db.clone()
    }
}

Required Methods§

Source

fn from_ref(state: &S) -> Self

Produces the value from a reference to the state.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§