-
Notifications
You must be signed in to change notification settings - Fork 0
/
async_graphql_like.rs
185 lines (150 loc) · 5.13 KB
/
async_graphql_like.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
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
#[cfg(test)]
mod tests {
use std::collections::HashMap;
struct Request(HashMap<std::any::TypeId, Box<dyn std::any::Any + Sync + Send>>);
impl Request {
fn data(&mut self, data: impl std::any::Any + Sync + Send) {
self.0.insert(data.type_id(), Box::new(data));
}
}
struct Context<'a>(&'a Request);
impl<'a> Context<'a> {
fn data<D: std::any::Any + Send + Sync>(&self) -> Option<&'a D> {
let request = self.0;
request
.0
.get(&std::any::TypeId::of::<D>())
.and_then(|d| d.downcast_ref::<D>())
}
}
#[test]
fn test_basic_types() {
let mut request = Request(HashMap::new());
request.data(123_i32);
request.data("abc".to_string());
let context = Context(&request);
assert_eq!(context.data::<u8>(), None);
assert_eq!(context.data::<i32>(), Some(&123));
assert_eq!(context.data::<String>(), Some(&"abc".to_string()));
}
#[test]
fn test_struct() {
#[derive(Debug, Eq, PartialEq)]
struct MyStruct {
i: i32,
s: String,
}
let mut request = Request(HashMap::new());
request.data(MyStruct {
i: 123_i32,
s: "abc".to_string(),
});
let context = Context(&request);
assert_eq!(context.data::<u8>(), None);
assert_eq!(
context.data::<MyStruct>(),
Some(&MyStruct {
i: 123_i32,
s: "abc".to_string(),
})
);
}
#[test]
#[allow(unused_variables)]
fn test_trait() {
trait MyTrait {
fn f(&self) -> i32;
}
#[derive(Debug, Eq, PartialEq)]
struct MyStruct {
i: i32,
}
impl MyTrait for MyStruct {
fn f(&self) -> i32 {
self.i
}
}
let mut request = Request(HashMap::new());
request.data(MyStruct { i: 123_i32 });
let context = Context(&request);
// add `dyn` keyword before this trait: `dyn `
// context.data::<MyTrait>()
// the size for values of type `dyn tests::test_trait::MyTrait` cannot be known at compilation time
// the trait `std::marker::Sized` is not implemented for `dyn tests::test_trait::MyTrait`
// context.data::<dyn MyTrait>();
}
#[test]
fn test_boxed_trait() {
trait MyTrait {
fn f(&self) -> i32;
}
#[derive(Debug, Eq, PartialEq)]
struct MyStruct {
i: i32,
}
impl MyTrait for MyStruct {
fn f(&self) -> i32 {
self.i
}
}
let mut request = Request(HashMap::new());
request.data(Box::new(MyStruct { i: 123_i32 }));
let context = Context(&request);
// `dyn tests::test_trait2::MyTrait` cannot be sent between threads safely
// the trait `std::marker::Send` is not implemented for `dyn tests::test_trait2::MyTrait`
// required for `std::ptr::Unique<dyn tests::test_trait2::MyTrait>` to implement `std::marker::Send`
// context.data::<Box<dyn MyTrait>>();
// `dyn tests::test_trait2::MyTrait + std::marker::Send` cannot be shared between threads safely
// the trait `std::marker::Sync` is not implemented for `dyn tests::test_trait2::MyTrait + std::marker::Send`
// required for `std::ptr::Unique<dyn tests::test_trait2::MyTrait + std::marker::Send>` to implement `std::marker::Sync`
// context.data::<Box<dyn MyTrait + Send>>();
let data = context.data::<Box<dyn MyTrait + Send + Sync>>();
assert!(data.is_none()); // can't get
}
#[test]
fn test_newtyped_boxed_trait() {
trait MyTrait {
fn f(&self) -> i32;
}
struct MyStruct {
i: i32,
}
impl MyTrait for MyStruct {
fn f(&self) -> i32 {
self.i
}
}
struct MyData(Box<dyn MyTrait + Send + Sync>);
let mut request = Request(HashMap::new());
request.data(MyData(Box::new(MyStruct { i: 123_i32 })));
let context = Context(&request);
let data = context.data::<MyData>();
assert!(data.is_some());
assert_eq!(data.unwrap().0.f(), 123_i32);
}
#[test]
fn test_trait_with_associated_types() {
trait MyTrait {
type T;
fn f(&self) -> Self::T;
}
struct MyStruct {
i: i32,
}
impl MyTrait for MyStruct {
type T = i32;
fn f(&self) -> Self::T {
self.i
}
}
// specify the associated type: `MyTrait<I = Type>`
// struct MyData(Box<dyn MyTrait + Send + Sync>);
struct MyData(Box<dyn MyTrait<T = i32> + Send + Sync>);
let mut request = Request(HashMap::new());
request.data(MyData(Box::new(MyStruct { i: 123_i32 })));
let context = Context(&request);
let data = context.data::<MyData>();
assert!(data.is_some());
assert_eq!(data.unwrap().0.f(), 123_i32);
}
}