Skip to main content

FromRef

Derive Macro FromRef 

Source
#[derive(FromRef)]
{
    // Attributes available to this derive:
    #[from_ref]
}
Expand description

Derives FromRef for each field of an application-state struct, so #[subscriber] handlers can inject any field with State<FieldType> without a hand-written impl.

Each field gets a FromRef impl that clones it out of the state. Because the generated impl carries no generic parameter, it is legal even for fields whose type comes from another crate (a broker publisher, a client pool). A field that another field’s type already claims, or that should not be injectable, opts out with #[from_ref(skip)]; two fields may not share a type (injection by type would be ambiguous).

#[derive(FromRef)]
struct AppState {
    orders: OrderService, // handlers can now take `State<OrderService>`
    #[from_ref(skip)]
    config: Config,
}