Debian Codegraph @

tests/value_delegate_derive.rs

raw symbols SCIP
1 use glib::{Value, ValueDelegate, prelude::*, value::FromValue};
2
3 #[test]
4 fn into_value() {
5 fn test_func(_: impl Into<Value>) {}
6
7 #[derive(ValueDelegate)]
8 pub struct Test(i32);
9
10 #[derive(ValueDelegate)]
11 #[value_delegate(nullable)]
12 pub struct TestNullable(String);
13
14 #[derive(ValueDelegate)]
15 #[value_delegate(from = i64)]
16 pub struct TestManualFrom(i32);
17
18 impl From<i64> for TestManualFrom {
19 fn from(v: i64) -> Self {
20 Self(v as i32)
21 }
22 }
23 impl<'a> From<&'a TestManualFrom> for i64 {
24 fn from(v: &'a TestManualFrom) -> Self {
25 v.0 as i64
26 }
27 }
28 impl From<TestManualFrom> for i64 {
29 fn from(v: TestManualFrom) -> Self {
30 v.0 as i64
31 }
32 }
33
34 test_func(Test(123));
35 test_func(Test(123));
36
37 test_func(TestManualFrom(123));
38 test_func(TestManualFrom(123));
39
40 test_func(TestNullable("foo".to_string()));
41 test_func(TestNullable("foo".to_string()));
42 test_func(Some(&TestNullable("foo".to_string())));
43 test_func(Some(TestNullable("foo".to_string())));
44 test_func(Some(TestNullable("foo".to_string())));
45
46 assert_eq!(glib::Value::from(Test(123)).get::<Test>().unwrap().0, 123);
47 assert_eq!(glib::Value::from(123).get::<Test>().unwrap().0, 123);
48 assert_eq!(glib::Value::from(Test(123)).get::<i32>().unwrap(), 123);
49
50 assert_eq!(
51 glib::Value::from(TestManualFrom(123))
52 .get::<TestManualFrom>()
53 .unwrap()
54 .0,
55 123
56 );
57 assert_eq!(
58 glib::Value::from(123_i64)
59 .get::<TestManualFrom>()
60 .unwrap()
61 .0,
62 123
63 );
64 assert_eq!(
65 glib::Value::from(TestManualFrom(123)).get::<i64>().unwrap(),
66 123
67 );
68
69 // From TestNullable
70 assert_eq!(
71 glib::Value::from(TestNullable("foo".to_string()))
72 .get::<Option<TestNullable>>()
73 .unwrap()
74 .unwrap()
75 .0,
76 "foo"
77 );
78 assert_eq!(
79 glib::Value::from("foo")
80 .get::<Option<TestNullable>>()
81 .unwrap()
82 .unwrap()
83 .0,
84 "foo"
85 );
86 assert_eq!(
87 glib::Value::from(TestNullable("foo".to_string()))
88 .get::<Option<String>>()
89 .unwrap()
90 .unwrap(),
91 "foo"
92 );
93 // From Option<TestNullable> Some
94 assert_eq!(
95 glib::Value::from(Some(TestNullable("foo".to_string())))
96 .get::<Option<TestNullable>>()
97 .unwrap()
98 .unwrap()
99 .0,
100 "foo"
101 );
102 assert_eq!(
103 glib::Value::from(Some("foo"))
104 .get::<Option<TestNullable>>()
105 .unwrap()
106 .unwrap()
107 .0,
108 "foo"
109 );
110 assert_eq!(
111 glib::Value::from(Some(TestNullable("foo".to_string())))
112 .get::<Option<String>>()
113 .unwrap()
114 .unwrap(),
115 "foo"
116 );
117 // From Option<TestNullable> None
118 assert!(
119 glib::Value::from(None::<TestNullable>)
120 .get::<Option<TestNullable>>()
121 .unwrap()
122 .is_none()
123 );
124 assert!(
125 glib::Value::from(None::<String>)
126 .get::<Option<TestNullable>>()
127 .unwrap()
128 .is_none()
129 );
130 assert!(
131 glib::Value::from(None::<TestNullable>)
132 .get::<Option<String>>()
133 .unwrap()
134 .is_none()
135 );
136 }
137
138 #[allow(clippy::unnecessary_literal_unwrap)]
139 #[test]
140 fn higher_level_types() {
141 #[derive(Debug, ValueDelegate)]
142 pub struct MyVec(Vec<String>);
143
144 #[derive(Debug, ValueDelegate)]
145 #[value_delegate(nullable)]
146 pub struct MyString(Box<str>);
147
148 #[derive(Debug, ValueDelegate)]
149 #[value_delegate(from = Option<String>)]
150 struct MyVecManualFrom(Vec<String>);
151
152 impl From<Option<String>> for MyVecManualFrom {
153 fn from(v: Option<String>) -> Self {
154 Self(v.into_iter().collect::<Vec<_>>())
155 }
156 }
157 impl<'a> From<&'a MyVecManualFrom> for Option<String> {
158 fn from(v: &'a MyVecManualFrom) -> Self {
159 v.0.first().cloned()
160 }
161 }
162 impl From<MyVecManualFrom> for Option<String> {
163 fn from(v: MyVecManualFrom) -> Self {
164 v.0.into_iter().next()
165 }
166 }
167
168 let vec = vec!["foo".to_string(), "bar".to_string()];
169 let vec_value = vec.to_value();
170 let my_vec_value = MyVec(vec).to_value();
171 assert_eq!(MyVec::static_type(), Vec::<String>::static_type());
172 assert_eq!(
173 vec_value.get::<Vec<String>>().unwrap(),
174 my_vec_value.get::<Vec<String>>().unwrap(),
175 );
176 assert_eq!(vec_value.value_type(), my_vec_value.value_type());
177 assert_eq!(unsafe { Vec::<String>::from_value(&vec_value) }, unsafe {
178 MyVec::from_value(&vec_value).0
179 });
180 assert_eq!(
181 unsafe { Vec::<String>::from_value(&my_vec_value) },
182 unsafe { MyVec::from_value(&my_vec_value).0 }
183 );
184
185 let string = "foo".to_string();
186 let string_value = string.to_value();
187 let my_string_value = MyString(string.into()).to_value();
188 assert_eq!(MyString::static_type(), Box::<str>::static_type());
189 assert_eq!(
190 string_value.get::<Box<str>>().unwrap(),
191 my_string_value.get::<Box<str>>().unwrap(),
192 );
193 assert_eq!(string_value.value_type(), my_string_value.value_type());
194 assert_eq!(unsafe { Box::<str>::from_value(&string_value) }, unsafe {
195 MyString::from_value(&string_value).0
196 });
197 assert_eq!(
198 unsafe { Box::<str>::from_value(&my_string_value) },
199 unsafe { MyString::from_value(&my_string_value).0 }
200 );
201
202 let string_some = Some("foo".to_string());
203 let string_some_value = string_some.to_value();
204 let string_none_value = None::<String>.to_value();
205 let my_string_some_value = MyString(string_some.unwrap().into()).to_value();
206 let my_string_none_value = None::<MyString>.to_value();
207 assert_eq!(
208 Option::<MyString>::static_type(),
209 Option::<Box<str>>::static_type()
210 );
211 assert_eq!(
212 string_some_value
213 .get::<Option<Box<str>>>()
214 .unwrap()
215 .unwrap(),
216 my_string_some_value
217 .get::<Option<Box<str>>>()
218 .unwrap()
219 .unwrap(),
220 );
221 assert_eq!(
222 string_none_value
223 .get::<Option<Box<str>>>()
224 .unwrap()
225 .is_none(),
226 my_string_none_value
227 .get::<Option<Box<str>>>()
228 .unwrap()
229 .is_none(),
230 );
231 assert_eq!(
232 string_some_value.value_type(),
233 my_string_some_value.value_type()
234 );
235 assert_eq!(
236 string_none_value.value_type(),
237 my_string_none_value.value_type()
238 );
239 assert_eq!(
240 unsafe { Option::<Box<str>>::from_value(&string_some_value).unwrap() },
241 unsafe {
242 Option::<MyString>::from_value(&string_some_value)
243 .unwrap()
244 .0
245 }
246 );
247 assert_eq!(
248 unsafe { Option::<Box<str>>::from_value(&string_none_value).is_none() },
249 unsafe { Option::<MyString>::from_value(&string_none_value).is_none() }
250 );
251 assert_eq!(
252 unsafe { Option::<Box<str>>::from_value(&my_string_some_value).unwrap() },
253 unsafe {
254 Option::<MyString>::from_value(&my_string_some_value)
255 .unwrap()
256 .0
257 }
258 );
259 assert_eq!(
260 unsafe { Option::<Box<str>>::from_value(&my_string_none_value).is_none() },
261 unsafe { Option::<MyString>::from_value(&my_string_none_value).is_none() }
262 );
263
264 let opt = Some("foo".to_string());
265 let opt_value = opt.to_value();
266 let my_vec_manual_from_value = MyVecManualFrom::from(opt).to_value();
267 assert_eq!(
268 MyVecManualFrom::static_type(),
269 Option::<String>::static_type()
270 );
271 assert_eq!(
272 opt_value.get::<Option<String>>().unwrap(),
273 my_vec_manual_from_value.get::<Option<String>>().unwrap(),
274 );
275 assert_eq!(
276 opt_value.value_type(),
277 my_vec_manual_from_value.value_type()
278 );
279 assert_eq!(
280 unsafe {
281 Option::<String>::from_value(&opt_value)
282 .into_iter()
283 .collect::<Vec<_>>()
284 },
285 unsafe { MyVecManualFrom::from_value(&opt_value).0 }
286 );
287 assert_eq!(
288 unsafe {
289 Option::<String>::from_value(&my_vec_manual_from_value)
290 .into_iter()
291 .collect::<Vec<_>>()
292 },
293 unsafe { MyVecManualFrom::from_value(&my_vec_manual_from_value).0 }
294 );
295 }