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
use std::{
borrow::Cow,
cmp::Ordering,
hash::{Hash, Hasher},
};
use crate::{Color, Scope, Type};
/// Represents a unique key for a provider.
#[derive(Clone, Debug)]
pub struct Key {
/// The name of the provider.
pub name: Cow<'static, str>,
/// The type of the provider generic.
pub ty: Type,
}
impl Key {
pub(crate) fn new<T: 'static>(name: Cow<'static, str>) -> Self {
Self {
name,
ty: Type::new::<T>(),
}
}
}
impl PartialEq for Key {
fn eq(&self, other: &Self) -> bool {
self.ty == other.ty && self.name == other.name
}
}
impl Eq for Key {}
impl PartialOrd for Key {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Key {
fn cmp(&self, other: &Self) -> Ordering {
match self.ty.cmp(&other.ty) {
Ordering::Equal => {}
ord => return ord,
}
self.name.cmp(&other.name)
}
}
impl Hash for Key {
fn hash<H: Hasher>(&self, state: &mut H) {
self.ty.hash(state);
self.name.hash(state);
}
}
/// Represents a definition of a provider.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Definition {
/// The unique key of the provider.
pub key: Key,
/// The origin type of the provider.
///
/// When the following methods are called, current definition represents the
/// return type of the method, and this field represents the parameter type of the method:
/// - [`SingletonProvider::bind`](crate::SingletonProvider::bind)
/// - [`TransientProvider::bind`](crate::TransientProvider::bind)
/// - [`SingleOwnerProvider::bind`](crate::SingleOwnerProvider::bind)
/// - [`SingletonAsyncProvider::bind`](crate::SingletonAsyncProvider::bind)
/// - [`TransientAsyncProvider::bind`](crate::TransientAsyncProvider::bind)
/// - [`SingleOwnerAsyncProvider::bind`](crate::SingleOwnerAsyncProvider::bind)
pub origin: Option<Type>,
/// The scope of the provider.
pub scope: Scope,
/// The color of the constructor.
pub color: Option<Color>,
/// Whether the provider is conditional.
pub conditional: bool,
}
impl Definition {
pub(crate) fn new<T: 'static>(
name: Cow<'static, str>,
scope: Scope,
color: Option<Color>,
conditional: bool,
) -> Self {
Self {
key: Key::new::<T>(name),
origin: None,
scope,
color,
conditional,
}
}
pub(crate) fn bind<T: 'static>(self) -> Definition {
let Definition {
key: Key { name, ty },
scope,
color,
conditional,
origin: _origin,
} = self;
Self {
key: Key::new::<T>(name),
origin: Some(ty),
scope,
color,
conditional,
}
}
}