pavex

Attribute Macro request_scoped

#[request_scoped]
Expand description

Mark a function (or method) as a request-scoped constructor.

Request-scoped constructors are invoked once per request to create a new instance of their output type. The created instance is cached for the duration of the request processing lifecycle.

§Imports

The annotated function must be imported via Blueprint::import, otherwise it won’t be considered by Pavex.

§Guide

Check out the “Dependency injection” section of Pavex’s guide for a thorough introduction to dependency injection in Pavex applications.

§Example

use pavex::request_scoped;

pub struct MyType {
    // [...]
}

impl MyType {
    #[request_scoped]
    pub fn new() -> Self {
        Self {
            // [...]
        }
    }
}

MyType::new will be called once per request whenever a new instance of MyType is needed.