1 #![allow(dead_code)]
2 #[macro_use]
3 extern crate derive_more;
4
5 use std::borrow::Cow;
6
7 #[derive(From)]
8 struct EmptyTuple();
9
10 #[derive(From)]
11 struct EmptyStruct {}
12
13 #[derive(From)]
14 struct EmptyUnit;
15
16 #[derive(From)]
17 struct MyInt(i32);
18
19 #[derive(From)]
20 struct MyInts(i32, i32);
21
22 #[derive(From)]
23 struct Point1D {
24 x: i32,
25 }
26
27 #[derive(From)]
28 struct Point2D {
29 x: i32,
30 y: i32,
31 }
32
33 #[derive(From)]
34 enum MixedInts {
35 SmallInt(i32),
36 NamedBigInt {
37 int: i64,
38 },
39 TwoSmallInts(i32, i32),
40 NamedBigInts {
41 x: i64,
42 y: i64,
43 },
44 #[from(ignore)]
45 Unsigned(u32),
46 NamedUnsigned {
47 x: u32,
48 },
49 }
50
51 #[derive(PartialEq, Eq, Debug)]
52 #[derive(From)]
53 #[from(forward)]
54 struct MyIntForward(u64);
55
56 #[test]
forward_struct()57 fn forward_struct() {
58 assert_eq!(MyIntForward(42), 42u32.into());
59 assert_eq!(MyIntForward(42), 42u16.into());
60 assert_eq!(MyIntForward(42), 42u64.into());
61 }
62
63 #[derive(PartialEq, Eq, Debug)]
64 #[derive(From)]
65 enum MixedIntsForward {
66 #[from(forward)]
67 SmallInt(i32),
68 NamedBigInt {
69 int: i64,
70 },
71 }
72
73 #[test]
forward_enum()74 fn forward_enum() {
75 assert_eq!(MixedIntsForward::SmallInt(42), 42i32.into());
76 assert_eq!(MixedIntsForward::SmallInt(42), 42i16.into());
77 }
78
79 #[derive(From, PartialEq)]
80 enum AutoIgnore {
81 SmallInt(i32),
82 Uninteresting,
83 Uninteresting2,
84 }
85
86 #[test]
auto_ignore_variants()87 fn auto_ignore_variants() {
88 assert!(AutoIgnore::SmallInt(42) == 42i32.into());
89 }
90
91 #[derive(From, PartialEq)]
92 enum AutoIgnoreWithDefaultTrue {
93 #[from(ignore)]
94 SmallInt(i32),
95 Uninteresting,
96 Uninteresting2,
97 }
98
99 #[derive(From, PartialEq)]
100 enum AutoIgnoreWithForwardFields2 {
101 #[from(forward)]
102 SmallInt(i32),
103 SmallIntIgnore(i32),
104 }
105
106 #[test]
auto_ignore_with_forward_field2()107 fn auto_ignore_with_forward_field2() {
108 assert!(AutoIgnoreWithForwardFields2::SmallInt(42) == 42i32.into());
109 assert!(AutoIgnoreWithForwardFields2::SmallInt(42) == 42i16.into());
110 }
111
112 #[derive(Debug, Eq, PartialEq)]
113 #[derive(From)]
114 #[from(types(u8, u16, u32))]
115 struct MyIntExplicit(u64);
116
117 #[test]
explicit_types_struct()118 fn explicit_types_struct() {
119 assert_eq!(MyIntExplicit(42), 42u8.into());
120 assert_eq!(MyIntExplicit(42), 42u16.into());
121 assert_eq!(MyIntExplicit(42), 42u32.into());
122 assert_eq!(MyIntExplicit(42), 42u64.into());
123 }
124
125 #[derive(Debug, Eq, PartialEq)]
126 #[derive(From)]
127 #[from(types(i8, i16))]
128 struct MyIntsExplicit(i32, i32);
129
130 #[test]
explicit_types_struct_tupled()131 fn explicit_types_struct_tupled() {
132 assert_eq!(MyIntsExplicit(42, 42), (42i32, 42i32).into());
133 assert_eq!(MyIntsExplicit(42, 42), (42i8, 42i8).into());
134 assert_eq!(MyIntsExplicit(42, 42), (42i16, 42i16).into());
135 }
136
137 #[derive(Debug, Eq, PartialEq)]
138 #[derive(From)]
139 enum MixedIntsExplicit {
140 #[from(types(i8))]
141 SmallInt(i32),
142 #[from(types(i16, i64))]
143 AnotherInt(i128),
144 NamedBigInt {
145 int: i64,
146 },
147 }
148
149 #[test]
explicit_types_enum()150 fn explicit_types_enum() {
151 assert_eq!(MixedIntsExplicit::SmallInt(42), 42i32.into());
152 assert_eq!(MixedIntsExplicit::SmallInt(42), 42i8.into());
153
154 assert_eq!(MixedIntsExplicit::AnotherInt(42), 42i128.into());
155 assert_eq!(MixedIntsExplicit::AnotherInt(42), 42i64.into());
156 assert_eq!(MixedIntsExplicit::AnotherInt(42), 42i16.into());
157 }
158
159 #[derive(Debug, Eq, PartialEq)]
160 #[derive(From)]
161 #[from(types(i8, i16))]
162 struct Point2DExplicit {
163 x: i32,
164 y: i32,
165 }
166
167 #[test]
explicit_types_point_2d()168 fn explicit_types_point_2d() {
169 let expected = Point2DExplicit { x: 42, y: 42 };
170 assert_eq!(expected, (42i32, 42i32).into());
171 assert_eq!(expected, (42i8, 42i8).into());
172 assert_eq!(expected, (42i16, 42i16).into());
173 }
174
175 #[derive(Debug, Eq, PartialEq)]
176 #[derive(From)]
177 #[from(types("Cow<'_, str>", "&str"))]
178 struct Name(String);
179
180 #[test]
explicit_complex_types_name()181 fn explicit_complex_types_name() {
182 let name = "Eärendil";
183 let expected = Name(name.to_owned());
184 assert_eq!(expected, name.to_owned().into());
185 assert_eq!(expected, name.into());
186 assert_eq!(expected, Cow::Borrowed(name).into());
187 }
188