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
use Arc;
use crateInjectionError;
/// Represents a value whose construction is delayed upon request rather than
/// resolved from the catalog immediately. This is often useful in cases when
/// some expensive type is used rarely, thus it's beneficial to only construct
/// it on-demand.
///
/// When instance is requested, this type will first attempt to resolve it using
/// [`crate::Catalog::current`] and will fall back to the catalog instance used
/// to create the `Lazy<T>`. This means that `Lazy<T>` can be used to access
/// values that are dynamically added into a chain of catalogs and may not be
/// present when the `Lazy<T>` itself is injected.
///
/// Note that this style of injection still respects the component's control
/// over its own lifetime via [`crate::Scope`]. So It's recommended to use
/// [`Self::get`] liberally and release the instances ASAP without attempting
/// to cache them.
///
/// See also:
/// - [`crate::Catalog::builder_chained`]
/// - [`crate::Catalog::scope`]
/// - [`crate::Catalog::current`]
///
/// ### Examples
///
/// ```
/// #[dill::component]
/// struct A;
///
/// impl A {
/// fn test(&self) -> String {
/// "A".into()
/// }
/// }
///
/// #[dill::component]
/// struct B {
/// lazy_a: dill::Lazy<std::sync::Arc<A>>,
/// }
/// impl B {
/// fn test(&self) -> String {
/// // A is created on-demand during this call
/// let a = self.lazy_a.get().unwrap();
/// a.test()
/// }
/// }
///
/// let cat = dill::Catalog::builder()
/// .add::<A>()
/// .add::<B>()
/// .build();
/// ```