-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkeyed.rs
156 lines (132 loc) · 3.46 KB
/
keyed.rs
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
// this file tests code generation for #[injectable] using
// keyed dependencies. if the project compiles, then code
// generation completed successfully
#![allow(dead_code)]
use di::{injectable, lazy::Lazy, KeyedRef, KeyedRefMut};
use cfg_if::cfg_if;
cfg_if! {
if #[cfg(feature = "async")] {
use std::sync::RwLock;
} else {
use std::cell::RefCell;
}
}
pub mod key {
pub struct Key1;
pub struct Key2;
pub struct Key3;
}
#[injectable]
pub struct KeyedDep(usize);
#[injectable]
pub struct KeyedStruct {
pub dep: KeyedRef<key::Key1, KeyedDep>,
}
#[injectable]
pub struct KeyedTupleStruct(pub KeyedRef<key::Key1, KeyedDep>);
#[injectable]
pub struct KeyedTupleGeneric<T: 'static>(pub KeyedRef<key::Key1, T>);
cfg_if! {
if #[cfg(feature = "async")] {
#[injectable]
pub struct KeyedStructRef {
pub dep: KeyedRef<key::Key1, RwLock<KeyedDep>>,
}
} else {
#[injectable]
pub struct KeyedStructRef {
pub dep: KeyedRef<key::Key1, RefCell<KeyedDep>>,
}
}
}
pub struct KeyedStructImpl {
dep: KeyedRef<key::Key1, KeyedDep>,
}
#[injectable]
impl KeyedStructImpl {
fn new(dep: KeyedRef<key::Key1, KeyedDep>) -> Self {
Self { dep }
}
}
cfg_if! {
if #[cfg(feature = "async")] {
pub struct KeyedStructImplRef {
dep: KeyedRef<key::Key1, RwLock<KeyedDep>>,
}
#[injectable]
impl KeyedStructImplRef {
fn new(dep: KeyedRef<key::Key1, RwLock<KeyedDep>>) -> Self {
Self { dep }
}
}
} else {
pub struct KeyedStructImplRef {
dep: KeyedRef<key::Key1, RefCell<KeyedDep>>,
}
#[injectable]
impl KeyedStructImplRef {
fn new(dep: KeyedRef<key::Key1, RefCell<KeyedDep>>) -> Self {
Self { dep }
}
}
}
}
#[injectable]
pub struct KeyedStructVec {
pub vec: Vec<KeyedRef<key::Key1, KeyedDep>>,
}
pub struct KeyedStructIter {
pub vec: Vec<KeyedRef<key::Key1, KeyedDep>>,
}
#[injectable]
impl KeyedStructIter {
pub fn new(deps: impl Iterator<Item = KeyedRef<key::Key1, KeyedDep>>) -> Self {
Self {
vec: deps.collect(),
}
}
}
cfg_if! {
if #[cfg(feature = "async")] {
pub struct KeyedStructIterRef {
pub vec: Vec<KeyedRef<key::Key1, RwLock<KeyedDep>>>,
}
#[injectable]
impl KeyedStructIterRef {
pub fn new(deps: impl Iterator<Item = KeyedRef<key::Key1, RwLock<KeyedDep>>>) -> Self {
Self {
vec: deps.collect(),
}
}
}
} else {
pub struct KeyedStructIterRef {
pub vec: Vec<KeyedRef<key::Key1, RefCell<KeyedDep>>>,
}
#[injectable]
impl KeyedStructIterRef {
pub fn new(deps: impl Iterator<Item = KeyedRef<key::Key1, RefCell<KeyedDep>>>) -> Self {
Self {
vec: deps.collect(),
}
}
}
}
}
#[injectable]
pub struct KeyedStructLazy {
pub dep: Lazy<KeyedRef<key::Key2, KeyedDep>>,
}
pub struct KeyedStructLazyImpl {
dep: Lazy<KeyedRef<key::Key2, KeyedDep>>,
}
#[injectable]
impl KeyedStructLazyImpl {
fn new(dep: Lazy<KeyedRef<key::Key2, KeyedDep>>) -> Self {
Self { dep }
}
}
#[injectable]
pub struct KeyedStructMut {
pub dep: KeyedRefMut<key::Key3, KeyedDep>
}