pavex/blueprint/lifecycle.rs
1use std::fmt::{Display, Formatter};
2
3#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
4/// How many times should a constructor be invoked?
5pub enum Lifecycle {
6 /// The constructor for a `Singleton` type is invoked at most once.
7 ///
8 /// As a consequence, there is at most one instance of `Singleton` types,
9 /// stored inside the server's global state.
10 Singleton,
11 /// The constructor for a `RequestScoped` type is invoked at most once for every incoming request.
12 ///
13 /// As a consequence, there is at most one instance of `RequestScoped` types for every incoming
14 /// request.
15 RequestScoped,
16 /// The constructor for a `Transient` type is invoked every single time an instance of the type
17 /// is required.
18 ///
19 /// As a consequence, there can be **multiple** instances of `Transient` types for every
20 /// incoming request.
21 Transient,
22}
23
24impl Display for Lifecycle {
25 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
26 let s = match self {
27 Lifecycle::Singleton => "singleton",
28 Lifecycle::RequestScoped => "request-scoped",
29 Lifecycle::Transient => "transient",
30 };
31 write!(f, "{s}")
32 }
33}