Debian Codegraph @

debian/missing-sources/derive_more/tests/try_into.rs

raw
1 #![cfg_attr(not(feature = "std"), no_std)]
2 #![allow(dead_code)] // some code is tested for type checking only
3
4 #[cfg(not(feature = "std"))]
5 extern crate alloc;
6
7 #[cfg(not(feature = "std"))]
8 use alloc::string::{String, ToString as _};
9
10 use derive_more::TryInto;
11
12 // Ensure that the `TryInto` macro is hygienic and doesn't break when `Result`
13 // has been redefined.
14 type Result = ();
15
16 mod enums {
17 use super::*;
18
19 #[test]
20 fn complex_with_ignoring() {
21 #[derive(TryInto, Clone, Copy, Debug, PartialEq)]
22 #[try_into(owned, ref, ref_mut)]
23 enum MixedInts {
24 SmallInt(i32),
25 NamedBigInt {
26 int: i64,
27 },
28 UnsignedWithIgnoredField(#[try_into(ignore)] bool, i64),
29 NamedUnsignedWithIgnoredField {
30 #[try_into(ignore)]
31 useless: bool,
32 x: i64,
33 },
34 TwoSmallInts(i32, i32),
35 NamedBigInts {
36 x: i64,
37 y: i64,
38 },
39 Unsigned(u32),
40 NamedUnsigned {
41 x: u32,
42 },
43 Unit,
44 #[try_into(ignore)]
45 Unit2,
46 }
47
48 let mut i = MixedInts::SmallInt(42);
49 assert_eq!(TryInto::<i32>::try_into(i).unwrap(), 42);
50 assert_eq!(TryInto::<&i32>::try_into(&i).unwrap(), &42);
51 assert_eq!(TryInto::<&mut i32>::try_into(&mut i).unwrap(), &mut 42);
52 assert_eq!(
53 i64::try_from(i).unwrap_err().to_string(),
54 "Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnoredField can be \
55 converted to i64",
56 );
57 assert_eq!(
58 <(i32, i32)>::try_from(i).unwrap_err().to_string(),
59 "Only TwoSmallInts can be converted to (i32, i32)",
60 );
61 assert_eq!(
62 <(i64, i64)>::try_from(i).unwrap_err().to_string(),
63 "Only NamedBigInts can be converted to (i64, i64)",
64 );
65 assert_eq!(
66 u32::try_from(i).unwrap_err().to_string(),
67 "Only Unsigned, NamedUnsigned can be converted to u32",
68 );
69 assert_eq!(
70 <()>::try_from(i).unwrap_err().to_string(),
71 "Only Unit can be converted to ()",
72 );
73
74 let mut i = MixedInts::NamedBigInt { int: 42 };
75 assert_eq!(TryInto::<i64>::try_into(i).unwrap(), 42);
76 assert_eq!(TryInto::<&i64>::try_into(&i).unwrap(), &42);
77 assert_eq!(TryInto::<&mut i64>::try_into(&mut i).unwrap(), &mut 42);
78 assert_eq!(
79 i32::try_from(i).unwrap_err().to_string(),
80 "Only SmallInt can be converted to i32",
81 );
82 assert_eq!(
83 <(i32, i32)>::try_from(i).unwrap_err().to_string(),
84 "Only TwoSmallInts can be converted to (i32, i32)",
85 );
86 assert_eq!(
87 <(i64, i64)>::try_from(i).unwrap_err().to_string(),
88 "Only NamedBigInts can be converted to (i64, i64)",
89 );
90 assert_eq!(
91 u32::try_from(i).unwrap_err().to_string(),
92 "Only Unsigned, NamedUnsigned can be converted to u32",
93 );
94 assert_eq!(
95 <()>::try_from(i).unwrap_err().to_string(),
96 "Only Unit can be converted to ()",
97 );
98
99 let mut i = MixedInts::TwoSmallInts(42, 64);
100 assert_eq!(TryInto::<(i32, i32)>::try_into(i).unwrap(), (42, 64));
101 assert_eq!(TryInto::<(&i32, &i32)>::try_into(&i).unwrap(), (&42, &64));
102 assert_eq!(
103 TryInto::<(&mut i32, &mut i32)>::try_into(&mut i).unwrap(),
104 (&mut 42, &mut 64),
105 );
106 assert_eq!(
107 i32::try_from(i).unwrap_err().to_string(),
108 "Only SmallInt can be converted to i32",
109 );
110 assert_eq!(
111 i64::try_from(i).unwrap_err().to_string(),
112 "Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnoredField can be \
113 converted to i64",
114 );
115 assert_eq!(
116 <(i64, i64)>::try_from(i).unwrap_err().to_string(),
117 "Only NamedBigInts can be converted to (i64, i64)",
118 );
119 assert_eq!(
120 u32::try_from(i).unwrap_err().to_string(),
121 "Only Unsigned, NamedUnsigned can be converted to u32",
122 );
123 assert_eq!(
124 <()>::try_from(i).unwrap_err().to_string(),
125 "Only Unit can be converted to ()",
126 );
127
128 let mut i = MixedInts::NamedBigInts { x: 42, y: 64 };
129 assert_eq!(TryInto::<(i64, i64)>::try_into(i).unwrap(), (42, 64));
130 assert_eq!(TryInto::<(&i64, &i64)>::try_into(&i).unwrap(), (&42, &64));
131 assert_eq!(
132 TryInto::<(&mut i64, &mut i64)>::try_into(&mut i).unwrap(),
133 (&mut 42, &mut 64),
134 );
135 assert_eq!(
136 i32::try_from(i).unwrap_err().to_string(),
137 "Only SmallInt can be converted to i32",
138 );
139 assert_eq!(
140 i64::try_from(i).unwrap_err().to_string(),
141 "Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnoredField can be \
142 converted to i64",
143 );
144 assert_eq!(
145 <(i32, i32)>::try_from(i).unwrap_err().to_string(),
146 "Only TwoSmallInts can be converted to (i32, i32)",
147 );
148 assert_eq!(
149 u32::try_from(i).unwrap_err().to_string(),
150 "Only Unsigned, NamedUnsigned can be converted to u32",
151 );
152 assert_eq!(
153 <()>::try_from(i).unwrap_err().to_string(),
154 "Only Unit can be converted to ()",
155 );
156
157 let mut i = MixedInts::Unsigned(42);
158 assert_eq!(TryInto::<u32>::try_into(i).unwrap(), 42);
159 assert_eq!(TryInto::<&u32>::try_into(&i).unwrap(), &42);
160 assert_eq!(TryInto::<&mut u32>::try_into(&mut i).unwrap(), &mut 42);
161 assert_eq!(
162 i32::try_from(i).unwrap_err().to_string(),
163 "Only SmallInt can be converted to i32",
164 );
165 assert_eq!(
166 i64::try_from(i).unwrap_err().to_string(),
167 "Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnoredField can be \
168 converted to i64",
169 );
170 assert_eq!(
171 <(i32, i32)>::try_from(i).unwrap_err().to_string(),
172 "Only TwoSmallInts can be converted to (i32, i32)",
173 );
174 assert_eq!(
175 <(i64, i64)>::try_from(i).unwrap_err().to_string(),
176 "Only NamedBigInts can be converted to (i64, i64)",
177 );
178 assert_eq!(
179 <()>::try_from(i).unwrap_err().to_string(),
180 "Only Unit can be converted to ()",
181 );
182
183 let mut i = MixedInts::NamedUnsigned { x: 42 };
184 assert_eq!(TryInto::<u32>::try_into(i).unwrap(), 42);
185 assert_eq!(TryInto::<&u32>::try_into(&i).unwrap(), &42);
186 assert_eq!(TryInto::<&mut u32>::try_into(&mut i).unwrap(), &mut 42);
187 assert_eq!(
188 i32::try_from(i).unwrap_err().to_string(),
189 "Only SmallInt can be converted to i32",
190 );
191 assert_eq!(
192 i64::try_from(i).unwrap_err().to_string(),
193 "Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnoredField can be \
194 converted to i64",
195 );
196 assert_eq!(
197 i64::try_from(i).unwrap_err().to_string(),
198 "Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnoredField can be \
199 converted to i64",
200 );
201 assert_eq!(
202 <(i32, i32)>::try_from(i).unwrap_err().to_string(),
203 "Only TwoSmallInts can be converted to (i32, i32)",
204 );
205 assert_eq!(
206 <(i64, i64)>::try_from(i).unwrap_err().to_string(),
207 "Only NamedBigInts can be converted to (i64, i64)",
208 );
209 assert_eq!(
210 <()>::try_from(i).unwrap_err().to_string(),
211 "Only Unit can be converted to ()",
212 );
213
214 let mut i = MixedInts::Unit;
215 assert!(TryInto::<()>::try_into(i).is_ok());
216 assert!(TryInto::<()>::try_into(&i).is_ok());
217 assert!(TryInto::<()>::try_into(&mut i).is_ok());
218 assert_eq!(
219 i32::try_from(i).unwrap_err().to_string(),
220 "Only SmallInt can be converted to i32",
221 );
222 assert_eq!(
223 i64::try_from(i).unwrap_err().to_string(),
224 "Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnoredField can be \
225 converted to i64",
226 );
227 assert_eq!(
228 <(i32, i32)>::try_from(i).unwrap_err().to_string(),
229 "Only TwoSmallInts can be converted to (i32, i32)",
230 );
231 assert_eq!(
232 <(i64, i64)>::try_from(i).unwrap_err().to_string(),
233 "Only NamedBigInts can be converted to (i64, i64)",
234 );
235 assert_eq!(
236 u32::try_from(i).unwrap_err().to_string(),
237 "Only Unsigned, NamedUnsigned can be converted to u32",
238 );
239 }
240
241 #[test]
242 fn associated_type_name_hygiene() {
243 #[derive(Debug, PartialEq)]
244 enum EnumWithError {
245 Error,
246 }
247
248 // Making sure that `TryInto` does not trigger an ambiguous associated item error for
249 // `Error`.
250 #[derive(TryInto, Debug, PartialEq)]
251 enum EnumIntoEnumWithError {
252 Foo(EnumWithError),
253 }
254
255 assert_eq!(
256 TryInto::<EnumWithError>::try_into(EnumIntoEnumWithError::Foo(
257 EnumWithError::Error
258 ))
259 .unwrap(),
260 EnumWithError::Error,
261 );
262 }
263
264 #[test]
265 fn error_preserves_input() {
266 #[derive(TryInto, Clone, Copy, Debug, PartialEq)]
267 #[try_into(owned, ref, ref_mut)]
268 enum MixedInts {
269 SmallInt(i32),
270 NamedBigInt { int: i64 },
271 }
272
273 let mut i = MixedInts::SmallInt(42);
274 assert_eq!(TryInto::<i64>::try_into(i).unwrap_err().input, i);
275 assert_eq!(TryInto::<&i64>::try_into(&i).unwrap_err().input, &i);
276 assert_eq!(
277 TryInto::<&mut i64>::try_into(&mut i.clone())
278 .unwrap_err()
279 .input,
280 &mut i
281 );
282 }
283
284 mod generic {
285 use super::*;
286
287 #[test]
288 fn simple_with_bound() {
289 #[derive(Debug, PartialEq)]
290 pub struct BarWrapper<V>(V);
291
292 #[derive(TryInto, Debug, PartialEq)]
293 #[try_into(ref, ref_mut)]
294 pub enum Bar<V>
295 where
296 Self: 'static,
297 {
298 V(BarWrapper<V>),
299 }
300
301 assert_eq!(
302 TryInto::<BarWrapper<_>>::try_into(Bar::V(BarWrapper(1))).unwrap(),
303 BarWrapper(1),
304 );
305 assert_eq!(
306 TryInto::<&BarWrapper<_>>::try_into(&Bar::V(BarWrapper(1))).unwrap(),
307 &BarWrapper(1),
308 );
309 assert_eq!(
310 TryInto::<&mut BarWrapper<_>>::try_into(&mut Bar::V(BarWrapper(1)))
311 .unwrap(),
312 &mut BarWrapper(1),
313 );
314 }
315
316 #[test]
317 fn complex_with_bound() {
318 #[derive(Debug, PartialEq)]
319 struct Wrapper<'a, const Y: usize, U>(&'a [U; Y]);
320
321 #[derive(TryInto, Debug, PartialEq)]
322 #[try_into(ref, ref_mut)]
323 enum Foo<'lt: 'static, T: Clone, const X: usize> {
324 X(Wrapper<'lt, X, T>),
325 }
326
327 assert_eq!(
328 TryInto::<Wrapper<1, _>>::try_into(Foo::X(Wrapper(&[1]))).unwrap(),
329 Wrapper(&[1]),
330 );
331 assert_eq!(
332 TryInto::<&Wrapper<1, _>>::try_into(&Foo::X(Wrapper(&[1]))).unwrap(),
333 &Wrapper(&[1]),
334 );
335 assert_eq!(
336 TryInto::<&mut Wrapper<1, _>>::try_into(&mut Foo::X(Wrapper(&[1])))
337 .unwrap(),
338 &mut Wrapper(&[1]),
339 );
340 }
341 }
342
343 mod custom_error {
344 use core::fmt;
345
346 use super::*;
347
348 #[derive(Debug)]
349 struct CustomError(String);
350
351 impl<T> From<derive_more::TryIntoError<T>> for CustomError {
352 fn from(value: derive_more::TryIntoError<T>) -> Self {
353 Self(value.to_string())
354 }
355 }
356
357 impl fmt::Display for CustomError {
358 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
359 self.0.fmt(f)
360 }
361 }
362
363 fn custom_error_fn<T>() -> fn(derive_more::TryIntoError<T>) -> CustomError {
364 CustomError::from
365 }
366
367 #[test]
368 fn simple() {
369 #[derive(TryInto, Clone, Debug)]
370 #[try_into(owned, ref, ref_mut)]
371 #[try_into(error(CustomError))]
372 enum MixedData {
373 Int(u32),
374 String(String),
375 }
376
377 let i = MixedData::Int(42);
378 assert_eq!(
379 String::try_from(i).unwrap_err().to_string(),
380 "Only String can be converted to String",
381 );
382
383 let s = MixedData::String("foo".into());
384 assert_eq!(
385 u32::try_from(s).unwrap_err().to_string(),
386 "Only Int can be converted to u32",
387 );
388 }
389
390 #[test]
391 fn with_fn() {
392 #[derive(TryInto, Clone, Debug)]
393 #[try_into(owned, ref, ref_mut)]
394 #[try_into(error(CustomError, CustomError::from))]
395 enum MixedData {
396 Int(u32),
397 String(String),
398 }
399
400 let i = MixedData::Int(42);
401 assert_eq!(
402 String::try_from(i).unwrap_err().to_string(),
403 "Only String can be converted to String",
404 );
405
406 let s = MixedData::String("foo".into());
407 assert_eq!(
408 u32::try_from(s).unwrap_err().to_string(),
409 "Only Int can be converted to u32",
410 );
411 }
412
413 #[test]
414 fn with_closure() {
415 #[derive(TryInto, Clone, Debug)]
416 #[try_into(owned, ref, ref_mut)]
417 #[try_into(error(CustomError, |e| e.into()))]
418 enum MixedData {
419 Int(u32),
420 String(String),
421 }
422
423 let i = MixedData::Int(42);
424 assert_eq!(
425 String::try_from(i).unwrap_err().to_string(),
426 "Only String can be converted to String",
427 );
428
429 let s = MixedData::String("foo".into());
430 assert_eq!(
431 u32::try_from(s).unwrap_err().to_string(),
432 "Only Int can be converted to u32",
433 );
434 }
435
436 #[test]
437 fn with_fn_call() {
438 #[derive(TryInto, Clone, Debug)]
439 #[try_into(owned, ref, ref_mut)]
440 #[try_into(error(CustomError, custom_error_fn()))]
441 enum MixedData {
442 Int(u32),
443 String(String),
444 }
445
446 let i = MixedData::Int(42);
447 assert_eq!(
448 String::try_from(i).unwrap_err().to_string(),
449 "Only String can be converted to String",
450 );
451
452 let s = MixedData::String("foo".into());
453 assert_eq!(
454 u32::try_from(s).unwrap_err().to_string(),
455 "Only Int can be converted to u32",
456 );
457 }
458 }
459 }