-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex_generics.rs
More file actions
194 lines (170 loc) · 5.97 KB
/
complex_generics.rs
File metadata and controls
194 lines (170 loc) · 5.97 KB
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
189
190
191
192
193
194
use std::hash::Hash;
use std::marker::PhantomData;
use enum_dispatch::enum_dispatch;
/// In this module, the attribute with the argument appears over the enum.
mod attribute_argument_on_enum {
use super::*;
#[enum_dispatch]
trait StoresKV<K: Hash, V> {
fn store(&mut self, key: K, value: V);
fn get(&self, key: &K) -> Option<&V>;
fn update_value<F: Fn(V) -> V>(&mut self, key: &K, update_by: F);
}
#[enum_dispatch(StoresKV<K, V>)]
enum KVStore<K: Hash, V, C: Credentials> {
FileStorage(FileStorage<K, V>),
RemoteStorage(RemoteStorage<K, V, C>),
DevNull,
}
impl<K: Hash, V> StoresKV<K, V> for FileStorage<K, V> {
#[allow(unused_variables)]
fn store(&mut self, key: K, value: V) {
unimplemented!();
}
#[allow(unused_variables)]
fn get(&self, key: &K) -> Option<&V> {
unimplemented!();
}
#[allow(unused_variables)]
fn update_value<F: Fn(V) -> V>(&mut self, key: &K, update_by: F) {
unimplemented!();
}
}
impl<K: Hash, V, C: Credentials> StoresKV<K, V> for RemoteStorage<K, V, C> {
#[allow(unused_variables)]
fn store(&mut self, key: K, value: V) {
let identity = self.credentials.identity();
let api_key = self.credentials.api_key();
unimplemented!();
}
#[allow(unused_variables)]
fn get(&self, key: &K) -> Option<&V> {
let identity = self.credentials.identity();
let api_key = self.credentials.api_key();
unimplemented!();
}
#[allow(unused_variables)]
fn update_value<F: Fn(V) -> V>(&mut self, key: &K, update_by: F) {
let identity = self.credentials.identity();
let api_key = self.credentials.api_key();
unimplemented!();
}
}
impl<K: Hash, V> StoresKV<K, V> for DevNull {
fn store(&mut self, _key: K, _value: V) {}
fn get(&self, _key: &K) -> Option<&V> {
None
}
fn update_value<F: Fn(V) -> V>(&mut self, _key: &K, _update_by: F) {}
}
#[test]
fn attribute_argument_on_enum() {
let mut storages: Vec<KVStore<String, String, SimpleCreds>> = vec![
DevNull.into(),
FileStorage::default().into(),
RemoteStorage::new(SimpleCreds).into(),
];
storages[0].store(String::from("key1"), String::from("value"));
assert_eq!(storages[0].get(&String::from("key2")), None);
storages[0].update_value(&String::from("key1"), |value| format!("{} + 1", value));
}
}
/// Same as above, but the attributes are reversed such that the one with an argument is on the
/// trait rather than the enum.
mod attribute_argument_on_trait {
use super::*;
#[enum_dispatch(KVStoreReversed<K, V, _C>)]
trait StoresKVReversed<K: Hash, V> {
fn store(&mut self, key: K, value: V);
fn get(&self, key: &K) -> Option<&V>;
fn update_value<F: Fn(V) -> V>(&mut self, key: &K, update_by: F);
}
#[enum_dispatch]
enum KVStoreReversed<K: Hash, V, C: Credentials> {
FileStorage(FileStorage<K, V>),
RemoteStorage(RemoteStorage<K, V, C>),
DevNull,
}
impl<K: Hash, V> StoresKVReversed<K, V> for FileStorage<K, V> {
#[allow(unused_variables)]
fn store(&mut self, key: K, value: V) {
unimplemented!();
}
#[allow(unused_variables)]
fn get(&self, key: &K) -> Option<&V> {
unimplemented!();
}
#[allow(unused_variables)]
fn update_value<F: Fn(V) -> V>(&mut self, key: &K, update_by: F) {
unimplemented!();
}
}
impl<K: Hash, V, C: Credentials> StoresKVReversed<K, V> for RemoteStorage<K, V, C> {
#[allow(unused_variables)]
fn store(&mut self, key: K, value: V) {
let identity = self.credentials.identity();
let api_key = self.credentials.api_key();
unimplemented!();
}
#[allow(unused_variables)]
fn get(&self, key: &K) -> Option<&V> {
let identity = self.credentials.identity();
let api_key = self.credentials.api_key();
unimplemented!();
}
#[allow(unused_variables)]
fn update_value<F: Fn(V) -> V>(&mut self, key: &K, update_by: F) {
let identity = self.credentials.identity();
let api_key = self.credentials.api_key();
unimplemented!();
}
}
impl<K: Hash, V> StoresKVReversed<K, V> for DevNull {
fn store(&mut self, _key: K, _value: V) {}
fn get(&self, _key: &K) -> Option<&V> {
None
}
fn update_value<F: Fn(V) -> V>(&mut self, _key: &K, _update_by: F) {}
}
#[test]
fn attribute_argument_on_enum() {
let mut storages: Vec<KVStoreReversed<String, String, SimpleCreds>> = vec![
DevNull.into(),
FileStorage::default().into(),
RemoteStorage::new(SimpleCreds).into(),
];
storages[0].store(String::from("key1"), String::from("value"));
assert_eq!(storages[0].get(&String::from("key2")), None);
storages[0].update_value(&String::from("key1"), |value| format!("{} + 1", value));
}
}
#[derive(Default)]
struct FileStorage<K: Hash, V> {
key_value_type: PhantomData<(K, V)>,
}
trait Credentials {
fn identity(&self) -> String;
fn api_key(&self) -> String;
}
struct RemoteStorage<K: Hash, V, C: Credentials> {
key_value_type: PhantomData<(K, V)>,
credentials: C,
}
impl<K: Hash, V, C: Credentials> RemoteStorage<K, V, C> {
fn new(credentials: C) -> Self {
Self {
key_value_type: Default::default(),
credentials,
}
}
}
struct DevNull;
struct SimpleCreds;
impl Credentials for SimpleCreds {
fn identity(&self) -> String {
String::from("root")
}
fn api_key(&self) -> String {
String::from("D0nTb3f0ol3D-Thi51Sn0tAR34al4P1kEY")
}
}