1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//! [`ServiceProvider`] — the unit of framework wiring.
//!
//! Every Rainier component ships a provider rather than a `setup()` function,
//! because registration and booting have to happen in
//! two separate passes across *all* providers, or a provider could resolve a
//! service that a later provider has not bound yet.
//!
//! - [`register`](ServiceProvider::register) binds factories into the
//! container. It must **not** resolve anything, because the providers after
//! it have not registered yet.
//! - [`boot`](ServiceProvider::boot) runs once every provider has registered,
//! so by then it may resolve freely. It is `async`, which is what lets a
//! provider open a database pool or warm a cache during boot.
use type_name;
use ;
use crateApplication;
/// A bundle of related bindings and their boot-time wiring.
///
/// ```
/// # use rainier_container::{Application, Container, ServiceProvider};
/// # use rainier_support::Result;
/// struct Clock;
/// struct ClockProvider;
///
/// impl ServiceProvider for ClockProvider {
/// fn register(&self, app: &Application) -> Result<()> {
/// app.singleton(|_: &Container| Ok(Clock));
/// Ok(())
/// }
/// }
/// ```
/// Implements [`ServiceProvider::boot`] from an `async fn`, so a provider can
/// be written with ordinary async syntax instead of hand-boxing the future.
///
/// ```
/// # use rainier_container::{boot_provider, Application, Container, ServiceProvider};
/// # use rainier_support::Result;
/// struct Pool;
/// struct DatabaseProvider;
///
/// impl ServiceProvider for DatabaseProvider {
/// fn register(&self, app: &Application) -> Result<()> {
/// app.singleton(|_: &Container| Ok(Pool));
/// Ok(())
/// }
///
/// boot_provider!(async |self, app| {
/// let _pool = app.resolve::<Pool>()?;
/// Ok(())
/// });
/// }
/// ```