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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
pub use submit;
use crate::;
collect!;
/// Returns an iterator over all auto-registered providers.
///
/// [`AutoRegisterModule`] uses this function to collect all auto-registered [`DynProvider`]s.
/// If you don't want to use `AutoRegisterModule`, you can use this function to customize your own module.
///
/// # Example
///
/// ```rust
/// use rudi::{auto_registered_providers, modules, Context, DynProvider, Module, SingleOwner};
///
/// struct MyAutoRegisterModule;
///
/// impl Module for MyAutoRegisterModule {
/// fn eager_create() -> bool {
/// true
/// }
///
/// fn providers() -> Vec<DynProvider> {
/// auto_registered_providers().collect()
/// }
/// }
///
/// #[SingleOwner]
/// struct A;
///
/// # fn main() {
/// let cx = Context::create(modules![MyAutoRegisterModule]);
/// assert!(cx.get_single_option::<A>().is_some());
/// # }
/// ```
/// A module that auto-registers all providers.
///
/// This module is enabled by the `auto-register` feature.
/// Because auto-registration relies on [`inventory`] crate, auto-registration
/// is not available on platforms where `inventory` is not supported.
///
/// # Example
///
/// ```rust
/// use rudi::{modules, AutoRegisterModule, Context, Singleton, Transient};
///
/// #[Singleton]
/// #[derive(Clone)]
/// struct A;
///
/// #[Transient]
/// struct B(A);
///
/// # fn main() {
/// let mut cx = Context::create(modules![AutoRegisterModule]);
/// assert!(cx.resolve_option::<B>().is_some());
/// # }
/// ```
;
/// Register a `Provider` that will be collected by [`auto_registered_providers`].
///
/// If you have:
/// - Enabled the `auto-register` feature (which is enabled by default).
/// - Define [`Provider`](crate::Provider) using the [`#[Singleton]`](crate::Singleton), [`#[Transient]`](crate::Transient) or [`#[SingleOwner]`](crate::SingleOwner) macro.
/// - [`#[Singleton]`](crate::Singleton), [`#[Transient]`](crate::Transient) or [`#[SingleOwner]`](crate::SingleOwner) does not use the `auto_register = false` attribute.
///
/// Then you don't need to use this macro to register `Provider`.
///
/// But if you use function define a [`Provider`](crate::Provider) and you want to use auto-registration,
/// then you need to use this macro.
///
/// # Example
///
/// ```rust
/// use rudi::{register_provider, singleton, Context, Provider};
///
/// fn foo() -> Provider<&'static str> {
/// singleton(|_| "Hello").into()
/// }
///
/// register_provider!(foo());
///
/// fn main() {
/// let mut cx = Context::auto_register();
/// assert!(cx.resolve_option::<&'static str>().is_some());
/// }
/// ```
/// Generate a function to enable auto-registration.
///
/// In Rust, it is possible to use [`inventory`] to accomplish something like
/// auto-registration, but there is still a problem, and it exists in Rudi as well.
///
/// Suppose you have two crates, one called `crate_1` and one called `crate_2`,
/// and you define some auto-registration types in `crate_2`.
///
/// If it is just a dependency on `crate_2` in `crate_1`'s `Cargo.toml`, then using
/// [`auto_registered_providers`] in `crate_1` will not collect the types defined in `crate_2`,
/// you have to use a function (or type, or constant) in `crate_1` that is defined in `crate_2`
/// in order to enable auto-registration.
///
/// So, there is this macro, which generates a function called `enable`, with no parameters
/// and no return, just to be called by other crates to enable auto-registration.
///
/// At the same time, you can also call the enable functions of other crates that the current
/// crate depends on in this macro, so that when the enable function of the current crate is
/// called, the enable functions of other crates will be called together.
///
/// # Example
///
/// ```rust ignore
/// // lib1/src/lib.rs
/// use rudi::{enable, Transient};
///
/// enable! {}
///
/// #[Transient(name = "lib1")]
/// fn Lib1() -> i32 {
/// 5
/// }
///
/// // lib2/src/lib.rs
/// use rudi::{enable, Transient};
///
/// enable! {
/// lib1::enable();
/// }
///
/// #[Transient(name = "lib2")]
/// fn Lib2() -> i32 {
/// 5
/// }
///
/// // bin/src/main.rs
/// use rudi::Context;
///
/// fn main() {
/// lib2::enable();
///
/// let mut cx = Context::auto_register();
/// assert_eq!(cx.resolve_by_type::<i32>().into_iter().sum::<i32>(), 10);
/// }
/// ```