Debian Codegraph @

debian/missing-sources/derive_more/tests/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::{
9 borrow::Cow,
10 borrow::ToOwned,
11 boxed::Box,
12 string::{String, ToString},
13 };
14 use core::mem;
15 #[cfg(feature = "std")]
16 use std::borrow::Cow;
17
18 use derive_more::Into;
19 use static_assertions::assert_not_impl_any;
20
21 /// Nasty [`mem::transmute()`] that works in generic contexts
22 /// by [`mem::forget`]ing stuff.
23 ///
24 /// It's OK for tests!
25 unsafe fn transmute<From, To>(from: From) -> To {
26 let to = unsafe { mem::transmute_copy(&from) };
27 mem::forget(from);
28 to
29 }
30
31 #[derive(Clone, Copy, Debug, PartialEq)]
32 #[repr(transparent)]
33 struct Wrapped<T>(T);
34
35 #[derive(Clone, Copy, Debug, PartialEq)]
36 #[repr(transparent)]
37 struct Transmuted<T>(T);
38
39 impl<T> From<Wrapped<T>> for Transmuted<T> {
40 fn from(from: Wrapped<T>) -> Self {
41 // SAFETY: repr(transparent)
42 unsafe { transmute(from) }
43 }
44 }
45
46 impl<T> From<&Wrapped<T>> for &Transmuted<T> {
47 fn from(from: &Wrapped<T>) -> Self {
48 // SAFETY: repr(transparent)
49 unsafe { transmute(from) }
50 }
51 }
52
53 impl<T> From<&mut Wrapped<T>> for &mut Transmuted<T> {
54 fn from(from: &mut Wrapped<T>) -> Self {
55 // SAFETY: repr(transparent)
56 unsafe { transmute(from) }
57 }
58 }
59
60 mod unit {
61 #![allow(clippy::unit_cmp)] // because of type inference in assertions
62
63 use super::*;
64
65 #[derive(Debug, Into, PartialEq)]
66 struct Unit;
67
68 #[derive(Debug, Into, PartialEq)]
69 struct Tuple();
70
71 #[derive(Debug, Into, PartialEq)]
72 struct Struct {}
73
74 #[test]
75 fn assert() {
76 assert_eq!((), Unit.into());
77 assert_eq!((), Tuple().into());
78 assert_eq!((), Struct {}.into());
79 }
80
81 mod generic {
82 use super::*;
83
84 #[derive(Debug, Into, PartialEq)]
85 struct Unit<const N: usize>;
86
87 #[derive(Debug, Into, PartialEq)]
88 struct Tuple<const N: usize>();
89
90 #[derive(Debug, Into, PartialEq)]
91 struct Struct<const N: usize> {}
92
93 #[test]
94 fn assert() {
95 assert_eq!((), Unit::<1>.into());
96 assert_eq!((), Tuple::<1>().into());
97 assert_eq!((), Struct::<1> {}.into());
98 }
99 }
100 }
101
102 mod single_field {
103 use super::*;
104
105 #[derive(Debug, Into, PartialEq)]
106 struct Tuple(i32);
107
108 #[derive(Debug, Into, PartialEq)]
109 struct Struct {
110 field: i32,
111 }
112
113 #[derive(Debug, Into, PartialEq)]
114 pub struct A([u8; Self::SIZE]);
115
116 impl A {
117 const SIZE: usize = 32;
118 }
119
120 #[test]
121 fn assert() {
122 assert_eq!(42, Tuple(42).into());
123 assert_eq!(42, Struct { field: 42 }.into());
124
125 let arr: [u8; A::SIZE] = A([42u8; A::SIZE]).into();
126 assert_eq!([42u8; A::SIZE], arr);
127 }
128
129 mod skip {
130 use super::*;
131
132 #[derive(Debug, Into, PartialEq)]
133 struct Tuple(#[into(skip)] i32);
134
135 #[derive(Debug, Into, PartialEq)]
136 struct Struct {
137 #[into(skip)]
138 field: i32,
139 }
140
141 #[test]
142 fn assert() {
143 #![allow(clippy::unit_cmp)] // because of type inference in assertions
144
145 assert_eq!((), Tuple(42).into());
146 assert_eq!((), Struct { field: 42 }.into());
147 }
148 }
149
150 mod types {
151 use super::*;
152
153 #[derive(Debug, Into, PartialEq)]
154 #[into(i64)]
155 #[into(i128)]
156 struct Tuple(i32);
157
158 #[derive(Debug, Into, PartialEq)]
159 #[into(Box<str>, Cow<'_, str>)]
160 struct Struct {
161 field: String,
162 }
163
164 #[test]
165 fn assert() {
166 assert_not_impl_any!(Tuple: Into<i32>);
167 assert_not_impl_any!(Struct: Into<String>);
168
169 assert_eq!(42_i64, Tuple(42).into());
170 assert_eq!(42_i128, Tuple(42).into());
171 assert_eq!(
172 Box::<str>::from("42".to_owned()),
173 Struct {
174 field: "42".to_string(),
175 }
176 .into(),
177 );
178 assert_eq!(
179 Cow::Borrowed("42"),
180 Cow::<str>::from(Struct {
181 field: "42".to_string(),
182 }),
183 );
184 }
185
186 mod ref_ {
187 use super::*;
188
189 #[derive(Debug, Into, PartialEq)]
190 #[into(ref)]
191 struct Unnamed(i32);
192
193 #[derive(Debug, Into, PartialEq)]
194 #[into(ref)]
195 struct Named {
196 field: i32,
197 }
198
199 #[test]
200 fn assert() {
201 assert_eq!(&42, <&i32>::from(&Unnamed(42)));
202 assert_eq!(&42, <&i32>::from(&Named { field: 42 }));
203 }
204
205 mod types {
206 use super::*;
207
208 #[derive(Debug, Into, PartialEq)]
209 #[into(ref(i32, Unnamed))]
210 struct Tuple(Unnamed);
211
212 #[derive(Debug, Into, PartialEq)]
213 #[into(ref(i32, Named))]
214 struct Struct {
215 field: Named,
216 }
217
218 #[test]
219 fn assert() {
220 assert_eq!(&42, <&i32>::from(&Tuple(Unnamed(42))));
221 assert_eq!(&Unnamed(42), <&Unnamed>::from(&Tuple(Unnamed(42))));
222 assert_eq!(
223 &42,
224 <&i32>::from(&Struct {
225 field: Named { field: 42 },
226 }),
227 );
228 assert_eq!(
229 &Named { field: 42 },
230 <&Named>::from(&Struct {
231 field: Named { field: 42 },
232 }),
233 );
234 }
235 }
236 }
237
238 mod ref_mut {
239 use super::*;
240
241 #[derive(Debug, Into, PartialEq)]
242 #[into(ref_mut)]
243 struct Unnamed(i32);
244
245 #[derive(Debug, Into, PartialEq)]
246 #[into(ref_mut)]
247 struct Named {
248 field: i32,
249 }
250
251 #[test]
252 fn assert() {
253 assert_eq!(&mut 42, <&mut i32>::from(&mut Unnamed(42)));
254 assert_eq!(&mut 42, <&mut i32>::from(&mut Named { field: 42 }));
255 }
256
257 mod types {
258 use super::*;
259
260 #[derive(Debug, Into, PartialEq)]
261 #[into(ref_mut(i32, Unnamed))]
262 struct Tuple(Unnamed);
263
264 #[derive(Debug, Into, PartialEq)]
265 #[into(ref_mut(i32, Named))]
266 struct Struct {
267 field: Named,
268 }
269
270 #[test]
271 fn assert() {
272 assert_eq!(&mut 42, <&mut i32>::from(&mut Tuple(Unnamed(42))));
273 assert_eq!(
274 &mut Unnamed(42),
275 <&mut Unnamed>::from(&mut Tuple(Unnamed(42))),
276 );
277 assert_eq!(
278 &mut 42,
279 <&mut i32>::from(&mut Struct {
280 field: Named { field: 42 },
281 }),
282 );
283 assert_eq!(
284 &mut Named { field: 42 },
285 <&mut Named>::from(&mut Struct {
286 field: Named { field: 42 },
287 }),
288 );
289 }
290 }
291 }
292 }
293
294 mod generic {
295 use super::*;
296
297 #[derive(Debug, Into, PartialEq)]
298 struct Tuple<T>(Wrapped<T>);
299
300 #[derive(Debug, Into, PartialEq)]
301 struct Struct<T> {
302 field: Wrapped<T>,
303 }
304
305 #[test]
306 fn assert() {
307 assert_eq!(Wrapped(42), Tuple(Wrapped(42)).into());
308 assert_eq!(Wrapped(42), Struct { field: Wrapped(42) }.into());
309 }
310
311 mod skip {
312 use super::*;
313
314 #[derive(Debug, Into, PartialEq)]
315 struct Tuple<T>(#[into(skip)] Wrapped<T>);
316
317 #[derive(Debug, Into, PartialEq)]
318 struct Struct<T> {
319 #[into(skip)]
320 field: Wrapped<T>,
321 }
322
323 #[test]
324 fn assert() {
325 #![allow(clippy::unit_cmp)] // because of type inference in assertions
326
327 assert_eq!((), Tuple(Wrapped(42)).into());
328 assert_eq!((), Struct { field: Wrapped(42) }.into());
329 }
330 }
331
332 mod types {
333 use super::*;
334
335 #[derive(Debug, Into, PartialEq)]
336 #[into(Transmuted<T>)]
337 struct Tuple<T>(Wrapped<T>);
338
339 #[derive(Debug, Into, PartialEq)]
340 #[into(Transmuted<T>)]
341 struct Struct<T> {
342 field: Wrapped<T>,
343 }
344
345 #[test]
346 fn assert() {
347 assert_eq!(Transmuted(42), Tuple(Wrapped(42)).into());
348 assert_eq!(Transmuted(42), Struct { field: Wrapped(42) }.into());
349 }
350 }
351
352 mod ref_ {
353 use super::*;
354
355 #[derive(Debug, Into, PartialEq)]
356 #[into(ref)]
357 struct Tuple<T>(Wrapped<T>);
358
359 #[derive(Debug, Into, PartialEq)]
360 #[into(ref)]
361 struct Struct<T> {
362 field: Wrapped<T>,
363 }
364
365 #[test]
366 fn assert() {
367 assert_eq!(&Wrapped(42), <&Wrapped<_>>::from(&Tuple(Wrapped(42))));
368 assert_eq!(
369 &Wrapped(42),
370 <&Wrapped<_>>::from(&Struct { field: Wrapped(42) })
371 );
372 }
373
374 mod types {
375 use super::*;
376
377 #[derive(Debug, Into, PartialEq)]
378 #[into(ref(Transmuted<T>))]
379 struct Tuple<T>(Wrapped<T>);
380
381 #[derive(Debug, Into, PartialEq)]
382 #[into(ref(Transmuted<T>))]
383 struct Struct<T> {
384 field: Wrapped<T>,
385 }
386
387 #[test]
388 fn assert() {
389 assert_eq!(
390 &Transmuted(42),
391 <&Transmuted<_>>::from(&Tuple(Wrapped(42))),
392 );
393 assert_eq!(
394 &Transmuted(42),
395 <&Transmuted<_>>::from(&Struct { field: Wrapped(42) }),
396 );
397 }
398 }
399 }
400
401 mod ref_mut {
402 use super::*;
403
404 #[derive(Debug, Into, PartialEq)]
405 #[into(ref_mut)]
406 struct Tuple<T>(Wrapped<T>);
407
408 #[derive(Debug, Into, PartialEq)]
409 #[into(ref_mut)]
410 struct Struct<T> {
411 field: Wrapped<T>,
412 }
413
414 #[test]
415 fn assert() {
416 assert_eq!(
417 &mut Wrapped(42),
418 <&mut Wrapped<_>>::from(&mut Tuple(Wrapped(42)))
419 );
420 assert_eq!(
421 &mut Wrapped(42),
422 <&mut Wrapped<_>>::from(&mut Struct { field: Wrapped(42) }),
423 );
424 }
425
426 mod types {
427 use super::*;
428
429 #[derive(Debug, Into, PartialEq)]
430 #[into(ref_mut(Transmuted<T>))]
431 struct Tuple<T>(Wrapped<T>);
432
433 #[derive(Debug, Into, PartialEq)]
434 #[into(ref_mut(Transmuted<T>))]
435 struct Struct<T> {
436 field: Wrapped<T>,
437 }
438
439 #[test]
440 fn assert() {
441 assert_eq!(
442 &mut Transmuted(42),
443 <&mut Transmuted<_>>::from(&mut Tuple(Wrapped(42))),
444 );
445 assert_eq!(
446 &mut Transmuted(42),
447 <&mut Transmuted<_>>::from(&mut Struct { field: Wrapped(42) }),
448 );
449 }
450 }
451 }
452
453 mod indirect {
454 use super::*;
455
456 #[derive(Debug, Into, PartialEq)]
457 struct Tuple<T: 'static>(&'static Wrapped<T>);
458
459 #[derive(Debug, Into, PartialEq)]
460 struct Struct<T: 'static> {
461 field: &'static Wrapped<T>,
462 }
463
464 #[test]
465 fn assert() {
466 assert_eq!(&Wrapped(42), <&Wrapped<_>>::from(Tuple(&Wrapped(42))));
467 assert_eq!(
468 &Wrapped(42),
469 <&Wrapped<_>>::from(Struct {
470 field: &Wrapped(42),
471 }),
472 );
473 }
474 }
475
476 mod bounded {
477 use super::*;
478
479 #[derive(Debug, Into, PartialEq)]
480 struct Tuple<T: Clone>(Wrapped<T>);
481
482 #[derive(Debug, Into, PartialEq)]
483 struct Struct<T: Clone> {
484 field: Wrapped<T>,
485 }
486
487 #[test]
488 fn assert() {
489 assert_eq!(Wrapped(42), Tuple(Wrapped(42)).into());
490 assert_eq!(Wrapped(42), Struct { field: Wrapped(42) }.into());
491 }
492 }
493
494 mod r#const {
495 use super::*;
496
497 #[derive(Debug, Into, PartialEq)]
498 struct Tuple<const N: usize, T>(Wrapped<T>);
499
500 #[derive(Debug, Into, PartialEq)]
501 struct Struct<T, const N: usize> {
502 field: Wrapped<T>,
503 }
504
505 #[test]
506 fn assert() {
507 assert_eq!(Wrapped(1), Tuple::<1, _>(Wrapped(1)).into());
508 assert_eq!(Wrapped(1), Struct::<_, 1> { field: Wrapped(1) }.into());
509 }
510 }
511 }
512 }
513
514 mod multi_field {
515 use super::*;
516
517 #[derive(Debug, Into, PartialEq)]
518 struct Tuple(i32, i64);
519
520 #[derive(Debug, Into, PartialEq)]
521 struct Struct {
522 field1: i32,
523 field2: i64,
524 }
525
526 #[test]
527 fn assert() {
528 assert_eq!((1, 2_i64), Tuple(1, 2_i64).into());
529 assert_eq!(
530 (1, 2_i64),
531 Struct {
532 field1: 1,
533 field2: 2_i64,
534 }
535 .into(),
536 );
537 }
538
539 mod skip {
540 use super::*;
541
542 #[derive(Debug, Into, PartialEq)]
543 struct Tuple(i32, #[into(skip)] i64);
544
545 #[derive(Debug, Into, PartialEq)]
546 struct Struct {
547 #[into(skip)]
548 field1: i32,
549 field2: i64,
550 }
551
552 #[test]
553 fn assert() {
554 assert_eq!(1, Tuple(1, 2_i64).into());
555 assert_eq!(
556 2_i64,
557 Struct {
558 field1: 1,
559 field2: 2_i64,
560 }
561 .into(),
562 );
563 }
564 }
565
566 mod types {
567 use super::*;
568
569 #[derive(Debug, Into, PartialEq)]
570 #[into((i32, i64))]
571 #[into((i64, i128))]
572 struct Tuple(i16, i32);
573
574 #[derive(Debug, Into, PartialEq)]
575 #[into((Box<str>, i32), (Cow<'_, str>, i64))]
576 struct Struct {
577 field1: String,
578 field2: i32,
579 }
580
581 #[test]
582 fn assert() {
583 assert_not_impl_any!(Tuple: Into<(i16, i32)>);
584 assert_not_impl_any!(Struct: Into<(String, i32)>);
585
586 assert_eq!((1, 2_i64), Tuple(1_i16, 2).into());
587 assert_eq!((1_i64, 2_i128), Tuple(1_i16, 2).into());
588 assert_eq!(
589 (Box::<str>::from("42".to_owned()), 1),
590 Struct {
591 field1: "42".to_string(),
592 field2: 1,
593 }
594 .into(),
595 );
596 assert_eq!(
597 (Cow::Borrowed("42"), 1_i64),
598 Struct {
599 field1: "42".to_string(),
600 field2: 1,
601 }
602 .into(),
603 );
604 }
605
606 mod ref_ {
607 use super::*;
608
609 #[derive(Debug, Into, PartialEq)]
610 #[into(ref)]
611 struct Unnamed(i32, i64);
612
613 #[derive(Debug, Into, PartialEq)]
614 #[into(ref)]
615 struct Named {
616 field1: i32,
617 field2: i64,
618 }
619
620 #[test]
621 fn assert() {
622 assert_eq!((&1, &2_i64), (&Unnamed(1, 2_i64)).into());
623 assert_eq!(
624 (&1, &2_i64),
625 (&Named {
626 field1: 1,
627 field2: 2_i64,
628 })
629 .into(),
630 );
631 }
632
633 mod types {
634 use super::*;
635
636 #[derive(Debug, Into, PartialEq)]
637 #[into(ref(
638 (Transmuted<i32>, Transmuted<i64>),
639 (Transmuted<i32>, Wrapped<i64>)),
640 )]
641 struct Tuple(Wrapped<i32>, Wrapped<i64>);
642
643 #[derive(Debug, Into, PartialEq)]
644 #[into(ref(
645 (Transmuted<i32>, Transmuted<i64>),
646 (Transmuted<i32>, Wrapped<i64>)),
647 )]
648 struct Struct {
649 field1: Wrapped<i32>,
650 field2: Wrapped<i64>,
651 }
652
653 #[test]
654 fn assert() {
655 assert_eq!(
656 (&Transmuted(1), &Transmuted(2_i64)),
657 (&Tuple(Wrapped(1), Wrapped(2_i64))).into(),
658 );
659 assert_eq!(
660 (&Transmuted(1), &Wrapped(2_i64)),
661 (&Tuple(Wrapped(1), Wrapped(2_i64))).into(),
662 );
663 assert_eq!(
664 (&Transmuted(1), &Transmuted(2_i64)),
665 (&Struct {
666 field1: Wrapped(1),
667 field2: Wrapped(2_i64),
668 })
669 .into(),
670 );
671 assert_eq!(
672 (&Transmuted(1), &Wrapped(2_i64)),
673 (&Struct {
674 field1: Wrapped(1),
675 field2: Wrapped(2_i64),
676 })
677 .into(),
678 );
679 }
680 }
681 }
682
683 mod ref_mut {
684 use super::*;
685
686 #[derive(Debug, Into, PartialEq)]
687 #[into(ref_mut)]
688 struct Unnamed(i32, i64);
689
690 #[derive(Debug, Into, PartialEq)]
691 #[into(ref_mut)]
692 struct Named {
693 field1: i32,
694 field2: i64,
695 }
696
697 #[test]
698 fn assert() {
699 assert_eq!((&mut 1, &mut 2_i64), (&mut Unnamed(1, 2_i64)).into());
700 assert_eq!(
701 (&mut 1, &mut 2_i64),
702 (&mut Named {
703 field1: 1,
704 field2: 2_i64,
705 })
706 .into(),
707 );
708 }
709
710 mod types {
711 use super::*;
712
713 #[derive(Debug, Into, PartialEq)]
714 #[into(ref_mut(
715 (Transmuted<i32>, Transmuted<i64>),
716 (Transmuted<i32>, Wrapped<i64>)),
717 )]
718 struct Tuple(Wrapped<i32>, Wrapped<i64>);
719
720 #[derive(Debug, Into, PartialEq)]
721 #[into(ref_mut(
722 (Transmuted<i32>, Transmuted<i64>),
723 (Transmuted<i32>, Wrapped<i64>)),
724 )]
725 struct Struct {
726 field1: Wrapped<i32>,
727 field2: Wrapped<i64>,
728 }
729
730 #[test]
731 fn assert() {
732 assert_eq!(
733 (&mut Transmuted(1), &mut Transmuted(2_i64)),
734 (&mut Tuple(Wrapped(1), Wrapped(2_i64))).into(),
735 );
736 assert_eq!(
737 (&mut Transmuted(1), &mut Wrapped(2_i64)),
738 (&mut Tuple(Wrapped(1), Wrapped(2_i64))).into(),
739 );
740 assert_eq!(
741 (&mut Transmuted(1), &mut Transmuted(2_i64)),
742 (&mut Struct {
743 field1: Wrapped(1),
744 field2: Wrapped(2_i64),
745 })
746 .into(),
747 );
748 assert_eq!(
749 (&mut Transmuted(1), &mut Wrapped(2_i64)),
750 (&mut Struct {
751 field1: Wrapped(1),
752 field2: Wrapped(2_i64),
753 })
754 .into(),
755 );
756 }
757 }
758 }
759 }
760
761 mod generic {
762 use super::*;
763
764 #[derive(Debug, Into, PartialEq)]
765 struct Tuple<A, B>(Wrapped<A>, Wrapped<B>);
766
767 #[derive(Debug, Into, PartialEq)]
768 struct Struct<A, B> {
769 field1: Wrapped<A>,
770 field2: Wrapped<B>,
771 }
772
773 #[test]
774 fn assert() {
775 assert_eq!(
776 (Wrapped(1), Wrapped(2)),
777 Tuple(Wrapped(1), Wrapped(2)).into(),
778 );
779 assert_eq!(
780 (Wrapped(1), Wrapped(2)),
781 Struct {
782 field1: Wrapped(1),
783 field2: Wrapped(2),
784 }
785 .into(),
786 );
787 }
788
789 mod skip {
790 use super::*;
791
792 #[derive(Debug, Into, PartialEq)]
793 struct Tuple<A, B>(Wrapped<A>, #[into(skip)] Wrapped<B>);
794
795 #[derive(Debug, Into, PartialEq)]
796 struct Struct<A, B> {
797 #[into(skip)]
798 field1: Wrapped<A>,
799 field2: Wrapped<B>,
800 }
801
802 #[test]
803 fn assert() {
804 assert_eq!(Wrapped(1), Tuple(Wrapped(1), Wrapped(2)).into());
805 assert_eq!(
806 Wrapped(2),
807 Struct {
808 field1: Wrapped(1),
809 field2: Wrapped(2),
810 }
811 .into(),
812 );
813 }
814 }
815
816 mod types {
817 use super::*;
818
819 #[derive(Debug, Into, PartialEq)]
820 #[into((Transmuted<A>, Transmuted<B>))]
821 struct Tuple<A, B>(Wrapped<A>, Wrapped<B>);
822
823 #[derive(Debug, Into, PartialEq)]
824 #[into((Transmuted<A>, Transmuted<B>))]
825 struct Struct<A, B> {
826 field1: Wrapped<A>,
827 field2: Wrapped<B>,
828 }
829
830 #[test]
831 fn assert() {
832 assert_eq!(
833 (Transmuted(1), Transmuted(2)),
834 Tuple(Wrapped(1), Wrapped(2)).into(),
835 );
836 assert_eq!(
837 (Transmuted(1), Transmuted(2)),
838 Struct {
839 field1: Wrapped(1),
840 field2: Wrapped(2),
841 }
842 .into(),
843 );
844 }
845 }
846
847 mod ref_ {
848 use super::*;
849
850 #[derive(Debug, Into, PartialEq)]
851 #[into(ref)]
852 struct Tuple<A, B>(Wrapped<A>, Wrapped<B>);
853
854 #[derive(Debug, Into, PartialEq)]
855 #[into(ref)]
856 struct Struct<A, B> {
857 field1: Wrapped<A>,
858 field2: Wrapped<B>,
859 }
860
861 #[test]
862 fn assert() {
863 assert_eq!(
864 (&Wrapped(1), &Wrapped(2)),
865 (&Tuple(Wrapped(1), Wrapped(2))).into(),
866 );
867 assert_eq!(
868 (&Wrapped(1), &Wrapped(2)),
869 (&Struct {
870 field1: Wrapped(1),
871 field2: Wrapped(2),
872 })
873 .into(),
874 );
875 }
876
877 mod types {
878 use super::*;
879
880 #[derive(Debug, Into, PartialEq)]
881 #[into(ref((Transmuted<A>, Transmuted<B>)))]
882 struct Tuple<A, B>(Wrapped<A>, Wrapped<B>);
883
884 #[derive(Debug, Into, PartialEq)]
885 #[into(ref((Transmuted<A>, Transmuted<B>)))]
886 struct Struct<A, B> {
887 field1: Wrapped<A>,
888 field2: Wrapped<B>,
889 }
890
891 #[test]
892 fn assert() {
893 assert_eq!(
894 (&Transmuted(1), &Transmuted(2)),
895 (&Tuple(Wrapped(1), Wrapped(2))).into(),
896 );
897 assert_eq!(
898 (&Transmuted(1), &Transmuted(2)),
899 (&Struct {
900 field1: Wrapped(1),
901 field2: Wrapped(2),
902 })
903 .into(),
904 );
905 }
906 }
907 }
908
909 mod ref_mut {
910 use super::*;
911
912 #[derive(Debug, Into, PartialEq)]
913 #[into(ref_mut)]
914 struct Tuple<A, B>(Wrapped<A>, Wrapped<B>);
915
916 #[derive(Debug, Into, PartialEq)]
917 #[into(ref_mut)]
918 struct Struct<A, B> {
919 field1: Wrapped<A>,
920 field2: Wrapped<B>,
921 }
922
923 #[test]
924 fn assert() {
925 assert_eq!(
926 (&mut Wrapped(1), &mut Wrapped(2)),
927 (&mut Tuple(Wrapped(1), Wrapped(2))).into(),
928 );
929 assert_eq!(
930 (&mut Wrapped(1), &mut Wrapped(2)),
931 (&mut Struct {
932 field1: Wrapped(1),
933 field2: Wrapped(2),
934 })
935 .into(),
936 );
937 }
938
939 mod types {
940 use super::*;
941
942 #[derive(Debug, Into, PartialEq)]
943 #[into(ref_mut((Transmuted<A>, Transmuted<B>)))]
944 struct Tuple<A, B>(Wrapped<A>, Wrapped<B>);
945
946 #[derive(Debug, Into, PartialEq)]
947 #[into(ref_mut((Transmuted<A>, Transmuted<B>)))]
948 struct Struct<A, B> {
949 field1: Wrapped<A>,
950 field2: Wrapped<B>,
951 }
952
953 #[test]
954 fn assert() {
955 assert_eq!(
956 (&mut Transmuted(1), &mut Transmuted(2)),
957 (&mut Tuple(Wrapped(1), Wrapped(2))).into(),
958 );
959 assert_eq!(
960 (&mut Transmuted(1), &mut Transmuted(2)),
961 (&mut Struct {
962 field1: Wrapped(1),
963 field2: Wrapped(2),
964 })
965 .into(),
966 );
967 }
968 }
969 }
970
971 mod indirect {
972 use super::*;
973
974 #[derive(Debug, Into, PartialEq)]
975 struct Tuple<A: 'static, B: 'static>(
976 &'static Wrapped<A>,
977 &'static Wrapped<B>,
978 );
979
980 #[derive(Debug, Into, PartialEq)]
981 struct Struct<A: 'static, B: 'static> {
982 field1: &'static Wrapped<A>,
983 field2: &'static Wrapped<B>,
984 }
985
986 #[test]
987 fn assert() {
988 assert_eq!(
989 (&Wrapped(1), &Wrapped(2)),
990 Tuple(&Wrapped(1), &Wrapped(2)).into(),
991 );
992 assert_eq!(
993 (&Wrapped(1), &Wrapped(2)),
994 (Struct {
995 field1: &Wrapped(1),
996 field2: &Wrapped(2),
997 })
998 .into(),
999 );
1000 }
1001 }
1002
1003 mod bounded {
1004 use super::*;
1005
1006 #[derive(Debug, Into, PartialEq)]
1007 struct Tuple<A: Clone, B: Clone>(Wrapped<A>, Wrapped<B>);
1008
1009 #[derive(Debug, Into, PartialEq)]
1010 struct Struct<A: Clone, B: Clone> {
1011 field1: Wrapped<A>,
1012 field2: Wrapped<B>,
1013 }
1014
1015 #[test]
1016 fn assert() {
1017 assert_eq!(
1018 (Wrapped(1), Wrapped(2)),
1019 Tuple(Wrapped(1), Wrapped(2)).into(),
1020 );
1021 assert_eq!(
1022 (Wrapped(1), Wrapped(2)),
1023 Struct {
1024 field1: Wrapped(1),
1025 field2: Wrapped(2),
1026 }
1027 .into(),
1028 );
1029 }
1030 }
1031
1032 mod r#const {
1033 use super::*;
1034
1035 #[derive(Debug, Into, PartialEq)]
1036 struct Tuple<const N: usize, A, B>(Wrapped<A>, Wrapped<B>);
1037
1038 #[derive(Debug, Into, PartialEq)]
1039 struct Struct<A, const N: usize, B> {
1040 field1: Wrapped<A>,
1041 field2: Wrapped<B>,
1042 }
1043
1044 #[test]
1045 fn assert() {
1046 assert_eq!(
1047 (Wrapped(1), Wrapped(2)),
1048 Tuple::<1, _, _>(Wrapped(1), Wrapped(2)).into(),
1049 );
1050 assert_eq!(
1051 (Wrapped(1), Wrapped(2)),
1052 Struct::<_, 1, _> {
1053 field1: Wrapped(1),
1054 field2: Wrapped(2),
1055 }
1056 .into(),
1057 );
1058 }
1059 }
1060 }
1061 }
1062
1063 mod with_fields {
1064 use super::*;
1065
1066 mod only {
1067 use super::*;
1068
1069 #[derive(Clone, Copy, Debug, Into)]
1070 struct Tuple(#[into] i32, f64, #[into] f64);
1071
1072 // Asserts that macro expansion doesn't generate this impl, by producing a trait
1073 // implementations conflict error during compilation, if it does.
1074 impl From<Tuple> for (i32, f64, f64) {
1075 fn from(value: Tuple) -> Self {
1076 (value.0, value.1, value.2)
1077 }
1078 }
1079
1080 // Asserts that macro expansion doesn't generate this impl, by producing a trait
1081 // implementations conflict error during compilation, if it does.
1082 impl From<Tuple> for (i32, f64) {
1083 fn from(value: Tuple) -> Self {
1084 (value.0, value.2)
1085 }
1086 }
1087
1088 #[test]
1089 fn tuple() {
1090 let t = Tuple(1, 2.0, 3.0);
1091
1092 assert_eq!(1, t.into());
1093 assert_eq!(3.0, t.into());
1094 }
1095
1096 #[derive(Clone, Copy, Debug, Into)]
1097 struct Struct {
1098 #[into]
1099 a: i32,
1100 b: f64,
1101 #[into]
1102 c: f64,
1103 }
1104
1105 // Asserts that macro expansion doesn't generate this impl, by producing a trait
1106 // implementations conflict error during compilation, if it does.
1107 impl From<Struct> for (i32, f64, f64) {
1108 fn from(value: Struct) -> Self {
1109 (value.a, value.b, value.c)
1110 }
1111 }
1112
1113 // Asserts that macro expansion doesn't generate this impl, by producing a trait
1114 // implementations conflict error during compilation, if it does.
1115 impl From<Struct> for (i32, f64) {
1116 fn from(value: Struct) -> Self {
1117 (value.a, value.c)
1118 }
1119 }
1120
1121 #[test]
1122 fn named() {
1123 let s = Struct {
1124 a: 1,
1125 b: 2.0,
1126 c: 3.0,
1127 };
1128
1129 assert_eq!(1, s.into());
1130 assert_eq!(3.0, s.into());
1131 }
1132
1133 mod types {
1134 use super::*;
1135
1136 #[derive(Clone, Debug, Into)]
1137 struct Tuple(
1138 #[into(Box<str>, Cow<'_, str>)] String,
1139 f64,
1140 #[into(f32, f64)] f32,
1141 );
1142
1143 // Asserts that macro expansion doesn't generate this impl, by producing a trait
1144 // implementations conflict error during compilation, if it does.
1145 impl From<Tuple> for String {
1146 fn from(value: Tuple) -> Self {
1147 value.0
1148 }
1149 }
1150
1151 // Asserts that macro expansion doesn't generate this impl, by producing a trait
1152 // implementations conflict error during compilation, if it does.
1153 impl From<Tuple> for (String, f64, f32) {
1154 fn from(value: Tuple) -> Self {
1155 (value.0, value.1, value.2)
1156 }
1157 }
1158
1159 #[test]
1160 fn tuple() {
1161 let f = Tuple("1".to_owned(), 2.0, 3.0);
1162
1163 assert_eq!(Box::<str>::from("1".to_owned()), f.clone().into());
1164 assert_eq!(Cow::Borrowed("1"), Cow::<str>::from(f.clone()));
1165 assert_eq!(3.0f32, f.clone().into());
1166 assert_eq!(3.0f64, f.into());
1167 }
1168
1169 #[derive(Clone, Debug, Into)]
1170 struct Struct {
1171 #[into(Box<str>, Cow<'_, str>)]
1172 a: String,
1173 b: f64,
1174 #[into(f32, f64)]
1175 c: f32,
1176 }
1177
1178 // Asserts that macro expansion doesn't generate this impl, by producing a trait
1179 // implementations conflict error during compilation, if it does.
1180 impl From<Struct> for String {
1181 fn from(value: Struct) -> Self {
1182 value.a
1183 }
1184 }
1185
1186 // Asserts that macro expansion doesn't generate this impl, by producing a trait
1187 // implementations conflict error during compilation, if it does.
1188 impl From<Struct> for (String, f64, f32) {
1189 fn from(value: Struct) -> Self {
1190 (value.a, value.b, value.c)
1191 }
1192 }
1193
1194 // Asserts that macro expansion doesn't generate this impl, by producing a trait
1195 // implementations conflict error during compilation, if it does.
1196 impl From<Struct> for (Box<str>, f32) {
1197 fn from(value: Struct) -> Self {
1198 (value.a.into(), value.c)
1199 }
1200 }
1201
1202 #[test]
1203 fn named() {
1204 let s = Struct {
1205 a: "1".to_owned(),
1206 b: 2.0,
1207 c: 3.0,
1208 };
1209
1210 assert_eq!(Box::<str>::from("1".to_owned()), s.clone().into());
1211 assert_eq!(Cow::Borrowed("1"), Cow::<str>::from(s.clone()));
1212 assert_eq!(3.0f32, s.clone().into());
1213 assert_eq!(3.0f64, s.into());
1214 }
1215
1216 mod r#ref {
1217 use super::*;
1218
1219 #[derive(Debug, Into)]
1220 struct Tuple(#[into(ref)] String, f64, #[into(ref)] f64);
1221
1222 // Asserts that macro expansion doesn't generate this impl, by producing a trait
1223 // implementations conflict error during compilation, if it does.
1224 impl<'a> From<&'a Tuple> for (&'a String, &'a f64, &'a f64) {
1225 fn from(value: &'a Tuple) -> Self {
1226 (&value.0, &value.1, &value.2)
1227 }
1228 }
1229
1230 #[test]
1231 fn tuple() {
1232 let t = Tuple("1".to_owned(), 2.0, 3.0);
1233
1234 assert_eq!(&"1".to_owned(), <&String>::from(&t));
1235 assert_eq!(&3.0, <&f64>::from(&t));
1236 }
1237
1238 #[derive(Debug, Into)]
1239 struct Struct {
1240 #[into(ref)]
1241 a: String,
1242 b: f64,
1243 #[into(ref)]
1244 c: f64,
1245 }
1246
1247 // Asserts that macro expansion doesn't generate this impl, by producing a trait
1248 // implementations conflict error during compilation, if it does.
1249 impl<'a> From<&'a Struct> for (&'a String, &'a f64, &'a f64) {
1250 fn from(value: &'a Struct) -> Self {
1251 (&value.a, &value.b, &value.c)
1252 }
1253 }
1254
1255 // Asserts that macro expansion doesn't generate this impl, by producing a trait
1256 // implementations conflict error during compilation, if it does.
1257 impl<'a> From<&'a Struct> for (&'a String, &'a f64) {
1258 fn from(value: &'a Struct) -> Self {
1259 (&value.a, &value.c)
1260 }
1261 }
1262
1263 #[test]
1264 fn named() {
1265 let s = Struct {
1266 a: "1".to_owned(),
1267 b: 2.0,
1268 c: 3.0,
1269 };
1270
1271 assert_eq!(&"1".to_owned(), <&String>::from(&s));
1272 assert_eq!(&3.0, <&f64>::from(&s));
1273 }
1274
1275 mod types {
1276 use super::*;
1277
1278 #[derive(Debug, Into)]
1279 struct Tuple(
1280 #[into(ref(Transmuted<i32>))] Wrapped<i32>,
1281 #[into(ref(Wrapped<i64>))] Wrapped<i64>,
1282 );
1283
1284 #[test]
1285 fn tuple() {
1286 let t = Tuple(Wrapped(1), Wrapped(2));
1287
1288 assert_eq!(&Transmuted(1), <&Transmuted<i32>>::from(&t));
1289 assert_eq!(&Wrapped(2), <&Wrapped<i64>>::from(&t));
1290 }
1291
1292 #[derive(Debug, Into)]
1293 struct Struct {
1294 #[into(ref(Transmuted<i32>))]
1295 a: Wrapped<i32>,
1296 #[into(ref(Wrapped<i64>))]
1297 b: Wrapped<i64>,
1298 }
1299
1300 #[test]
1301 fn named() {
1302 let s = Struct {
1303 a: Wrapped(1),
1304 b: Wrapped(2),
1305 };
1306
1307 assert_eq!(&Transmuted(1), <&Transmuted<i32>>::from(&s));
1308 assert_eq!(&Wrapped(2), <&Wrapped<i64>>::from(&s));
1309 }
1310 }
1311
1312 mod ref_mut {
1313 use super::*;
1314
1315 #[derive(Debug, Into)]
1316 struct Tuple(#[into(ref_mut)] i32, f64, #[into(ref_mut)] f64);
1317
1318 #[test]
1319 fn tuple() {
1320 let mut t = Tuple(1, 2.0, 3.0);
1321
1322 assert_eq!(&mut 1, <&mut i32>::from(&mut t));
1323 assert_eq!(&mut 3.0, <&mut f64>::from(&mut t));
1324 }
1325
1326 #[derive(Debug, Into)]
1327 struct Struct {
1328 #[into(ref_mut)]
1329 a: i32,
1330 b: f64,
1331 #[into(ref_mut)]
1332 c: f64,
1333 }
1334
1335 #[test]
1336 fn named() {
1337 let mut s = Struct {
1338 a: 1,
1339 b: 2.0,
1340 c: 3.0,
1341 };
1342
1343 assert_eq!(&mut 1, <&mut i32>::from(&mut s));
1344 assert_eq!(&mut 3.0, <&mut f64>::from(&mut s));
1345 }
1346
1347 mod types {
1348 use super::*;
1349
1350 #[derive(Debug, Into)]
1351 struct Tuple(
1352 #[into(ref_mut(Transmuted<i32>))] Wrapped<i32>,
1353 #[into(ref_mut(Wrapped<i64>))] Wrapped<i64>,
1354 );
1355
1356 #[test]
1357 fn tuple() {
1358 let mut t = Tuple(Wrapped(1), Wrapped(2));
1359
1360 assert_eq!(
1361 &mut Transmuted(1),
1362 <&mut Transmuted<i32>>::from(&mut t),
1363 );
1364 assert_eq!(
1365 &mut Wrapped(2),
1366 <&mut Wrapped<i64>>::from(&mut t),
1367 );
1368 }
1369
1370 #[derive(Debug, Into)]
1371 struct Struct {
1372 #[into(ref_mut(Transmuted<i32>))]
1373 a: Wrapped<i32>,
1374 #[into(ref_mut(Wrapped<i64>))]
1375 b: Wrapped<i64>,
1376 }
1377
1378 #[test]
1379 fn named() {
1380 let mut s = Struct {
1381 a: Wrapped(1),
1382 b: Wrapped(2),
1383 };
1384
1385 assert_eq!(
1386 &mut Transmuted(1),
1387 <&mut Transmuted<i32>>::from(&mut s),
1388 );
1389 assert_eq!(
1390 &mut Wrapped(2),
1391 <&mut Wrapped<i64>>::from(&mut s),
1392 );
1393 }
1394 }
1395 }
1396 }
1397 }
1398 }
1399
1400 mod mixed {
1401 use super::*;
1402
1403 #[derive(Debug, Into)]
1404 #[into(ref((Wrapped<i32>, Transmuted<f32>)))]
1405 struct Tuple(
1406 #[into(owned, ref(Transmuted<i32>))] Wrapped<i32>,
1407 #[into(skip)]
1408 #[into(ref)]
1409 Wrapped<f32>,
1410 #[into(ref_mut(Wrapped<f32>, Transmuted<f32>))] Wrapped<f32>,
1411 );
1412
1413 #[test]
1414 fn tuple() {
1415 let mut t = Tuple(Wrapped(1), Wrapped(2.0), Wrapped(3.0));
1416
1417 assert_eq!(&Transmuted(1), <&Transmuted<i32>>::from(&t));
1418 assert_eq!(&mut Transmuted(3.0), <&mut Transmuted<f32>>::from(&mut t));
1419 assert_eq!(&mut Wrapped(3.0), <&mut Wrapped<f32>>::from(&mut t));
1420 assert_eq!((&Wrapped(1), &Transmuted(3.0)), (&t).into());
1421 assert_eq!(&Wrapped(2.0), <&Wrapped<f32>>::from(&t));
1422 assert_eq!(Wrapped(1), t.into());
1423 }
1424
1425 #[derive(Debug, Into)]
1426 #[into(ref((Wrapped<i32>, Transmuted<f32>)))]
1427 struct Struct {
1428 #[into(owned, ref(Transmuted<i32>))]
1429 a: Wrapped<i32>,
1430 #[into(skip)]
1431 #[into(ref)]
1432 b: Wrapped<f32>,
1433 #[into(ref_mut(Wrapped<f32>, Transmuted<f32>))]
1434 c: Wrapped<f32>,
1435 }
1436
1437 #[test]
1438 fn named() {
1439 let mut s = Struct {
1440 a: Wrapped(1),
1441 b: Wrapped(2.0),
1442 c: Wrapped(3.0),
1443 };
1444
1445 assert_eq!(&Transmuted(1), <&Transmuted<i32>>::from(&s));
1446 assert_eq!(&mut Transmuted(3.0), <&mut Transmuted<f32>>::from(&mut s));
1447 assert_eq!(&mut Wrapped(3.0), <&mut Wrapped<f32>>::from(&mut s));
1448 assert_eq!((&Wrapped(1), &Transmuted(3.0)), (&s).into());
1449 assert_eq!(&Wrapped(2.0), <&Wrapped<f32>>::from(&s));
1450 assert_eq!(Wrapped(1), s.into());
1451 }
1452
1453 mod separate {
1454 use super::*;
1455
1456 #[derive(Clone, Copy, Debug, Into)]
1457 #[into(ref)]
1458 #[into(owned)]
1459 #[into((Wrapped<i32>, Transmuted<f32>))]
1460 #[into(ref_mut((Wrapped<i32>, Transmuted<f32>)))]
1461 struct Tuple(
1462 #[into(ref)]
1463 #[into(ref(Transmuted<i32>))]
1464 #[into]
1465 Wrapped<i32>,
1466 #[into(ref_mut)]
1467 #[into(ref_mut(Transmuted<f32>))]
1468 #[into(owned)]
1469 Wrapped<f32>,
1470 );
1471
1472 #[test]
1473 fn tuple() {
1474 let mut t = Tuple(Wrapped(1), Wrapped(2.0));
1475
1476 assert_eq!((&Wrapped(1), &Wrapped(2.0)), (&t).into());
1477 assert_eq!((Wrapped(1), Wrapped(2.0)), t.into());
1478 assert_eq!((Wrapped(1), Transmuted(2.0)), t.into());
1479 assert_eq!((&mut Wrapped(1), &mut Transmuted(2.0)), (&mut t).into());
1480 assert_eq!(&Wrapped(1), <&Wrapped<i32>>::from(&t));
1481 assert_eq!(&Transmuted(1), <&Transmuted<i32>>::from(&t));
1482 assert_eq!(Wrapped(1), <Wrapped<i32>>::from(t));
1483 assert_eq!(&mut Wrapped(2.0), <&mut Wrapped<f32>>::from(&mut t));
1484 assert_eq!(&mut Transmuted(2.0), <&mut Transmuted<f32>>::from(&mut t));
1485 assert_eq!(Wrapped(2.0), <Wrapped<f32>>::from(t));
1486 }
1487
1488 #[derive(Clone, Copy, Debug, Into)]
1489 #[into(ref)]
1490 #[into(owned)]
1491 #[into((Wrapped<i32>, Transmuted<f32>))]
1492 #[into(ref_mut((Wrapped<i32>, Transmuted<f32>)))]
1493 struct Struct {
1494 #[into(ref)]
1495 #[into(ref (Transmuted < i32 >))]
1496 #[into]
1497 a: Wrapped<i32>,
1498 #[into(ref_mut)]
1499 #[into(ref_mut(Transmuted < f32 >))]
1500 #[into(owned)]
1501 b: Wrapped<f32>,
1502 }
1503
1504 #[test]
1505 fn named() {
1506 let mut s = Struct {
1507 a: Wrapped(1),
1508 b: Wrapped(2.0),
1509 };
1510
1511 assert_eq!((&Wrapped(1), &Wrapped(2.0)), (&s).into());
1512 assert_eq!((Wrapped(1), Wrapped(2.0)), s.into());
1513 assert_eq!((Wrapped(1), Transmuted(2.0)), s.into());
1514 assert_eq!((&mut Wrapped(1), &mut Transmuted(2.0)), (&mut s).into());
1515 assert_eq!(&Wrapped(1), <&Wrapped<i32>>::from(&s));
1516 assert_eq!(&Transmuted(1), <&Transmuted<i32>>::from(&s));
1517 assert_eq!(Wrapped(1), <Wrapped<i32>>::from(s));
1518 assert_eq!(&mut Wrapped(2.0), <&mut Wrapped<f32>>::from(&mut s));
1519 assert_eq!(&mut Transmuted(2.0), <&mut Transmuted<f32>>::from(&mut s),);
1520 assert_eq!(Wrapped(2.0), <Wrapped<f32>>::from(s));
1521 }
1522 }
1523 }
1524 }