pavex

Attribute Macro transient

#[transient]
Expand description

Mark a function (or method) as a transient constructor.

Transient constructors are invoked each time a new instance of their output type is needed, even within the same request. The created instances are not cached.

§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::transient;

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

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

MyType::new will be called each time a new instance of MyType is needed, even within the same request.