1 #[macro_use]
2 extern crate derive_more;
3
4 #[derive(From)]
5 #[derive(Into)]
6 #[derive(Constructor)]
7 #[derive(Eq, PartialEq, Debug, Clone)]
8 #[derive(Add)]
9 #[derive(Mul)]
10 #[derive(Neg)]
11 #[derive(AddAssign)]
12 #[derive(MulAssign)]
13 #[derive(FromStr)]
14 #[derive(Display)]
15 #[derive(Octal)]
16 #[derive(Binary)]
17 #[derive(Deref, DerefMut)]
18 #[into(owned, ref, ref_mut)]
19 struct MyInt(i32);
20
21 #[derive(Clone, Debug, Eq, PartialEq)]
22 #[derive(Add)]
23 #[derive(Sum)]
24 #[derive(Mul)]
25 #[derive(MulAssign)]
26 #[derive(Product)]
27 #[mul(forward)]
28 #[mul_assign(forward)]
29 struct MyInt2(i32);
30
31 #[derive(Eq, PartialEq, Debug)]
32 #[derive(Index, IndexMut)]
33 #[derive(Deref, DerefMut)]
34 #[derive(IntoIterator)]
35 #[deref(forward)]
36 #[deref_mut(forward)]
37 #[into_iterator(owned, ref, ref_mut)]
38 struct MyVec(Vec<i32>);
39
40 #[derive(Eq, PartialEq, Debug)]
41 #[derive(Deref, DerefMut)]
42 #[deref(forward)]
43 #[deref_mut(forward)]
44 struct MyBoxedInt(Box<i32>);
45
46 #[derive(Eq, PartialEq, Debug)]
47 #[derive(Not)]
48 #[derive(From)]
49 struct MyBool(bool);
50
51 #[derive(From)]
52 #[derive(Into)]
53 #[derive(Constructor)]
54 #[derive(Add)]
55 #[derive(Eq, PartialEq, Debug)]
56 #[derive(Mul)]
57 #[derive(AddAssign)]
58 struct MyUInt(u64, u64);
59
60 #[derive(From)]
61 #[derive(Into)]
62 #[derive(Constructor)]
63 #[derive(FromStr)]
64 #[derive(Eq, PartialEq, Debug)]
65 #[derive(Display)]
66 struct SimpleStruct {
67 int1: u64,
68 }
69
70 #[derive(From)]
71 #[derive(Constructor)]
72 #[derive(Add, Sub, Mul, Div, Rem, BitAnd, BitOr, BitXor, Shr, Shl)]
73 #[derive(Eq, PartialEq, Debug, Clone, Copy)]
74 #[derive(Into)]
75 #[derive(AddAssign)]
76 #[into(owned, ref, ref_mut)]
77 struct NormalStruct {
78 int1: u64,
79 int2: u64,
80 }
81
82 #[derive(From)]
83 #[derive(Eq, PartialEq, Debug)]
84 struct NestedInt(MyInt);
85
86 #[derive(Eq, PartialEq, Debug)]
87 #[derive(From)]
88 #[derive(Add, Sub)]
89 enum SimpleMyIntEnum {
90 Int(i32),
91 #[from(ignore)]
92 _UnsignedOne(u32),
93 _UnsignedTwo(u32),
94 }
95 #[derive(Eq, PartialEq, Debug)]
96 #[derive(From)]
97 #[derive(Neg)]
98 enum SimpleSignedIntEnum {
99 Int(i32),
100 Int2(i16),
101 }
102
103 #[derive(Eq, PartialEq, Debug)]
104 #[derive(From)]
105 #[derive(Add, Sub)]
106 #[derive(Neg)]
107 enum SimpleEnum {
108 Int(i32),
109 #[from(ignore)]
110 _Ints(i32, i32),
111 LabeledInts {
112 a: i32,
113 b: i32,
114 },
115 _SomeUnit,
116 }
117
118 #[derive(Eq, PartialEq, Debug)]
119 #[derive(From)]
120 #[derive(Add, Sub)]
121 enum MyIntEnum {
122 SmallInt(i32),
123 BigInt(i64),
124 TwoInts(i32, i32),
125 Point2D {
126 x: i64,
127 y: i64,
128 },
129 #[from(ignore)]
130 _UnsignedOne(u32),
131 _UnsignedTwo(u32),
132 #[from(ignore)]
133 _Uints1(u64, u64),
134 _Uints2 {
135 x: u64,
136 y: u64,
137 },
138 Nothing,
139 }
140
141 #[derive(Eq, PartialEq, Debug)]
142 #[derive(Add, Mul)]
143 struct DoubleUInt(u32, u32);
144
145 #[derive(Eq, PartialEq, Debug)]
146 #[derive(Add, Mul)]
147 struct DoubleUIntStruct {
148 x: u32,
149 y: u32,
150 }
151
152 #[derive(Eq, PartialEq, Debug)]
153 #[derive(From, Into, Constructor)]
154 struct Unit;
155
156 // Tests that we can forward to a path
157 // containing `$crate`
158 macro_rules! use_dollar_crate {
159 () => {
160 struct Foo;
161 #[derive(From)]
162 enum Bar {
163 First(#[from(forward)] $crate::Foo),
164 }
165 };
166 }
167
168 use_dollar_crate!();
169
170 #[test]
main()171 fn main() {
172 let mut myint: MyInt = 5.into();
173 let _: SimpleMyIntEnum = 5i32.into();
174 let _: MyIntEnum = 5i32.into();
175 let _: MyIntEnum = 6i64.into();
176 let _: MyIntEnum = (5i32, 8i32).into();
177 let _: MyIntEnum = (5i64, 8i64).into();
178 let _: MyIntEnum = ().into();
179
180 let int_ref: &i32 = (&myint).into();
181 assert_eq!(int_ref, &5);
182
183 let int_ref_mut: &mut i32 = (&mut myint).into();
184 assert_eq!(int_ref_mut, &mut 5);
185
186 let mut myint: MyInt = 5.into();
187 let _: Unit = ().into();
188 assert_eq!((), Unit.into());
189 assert_eq!(Unit, Unit::new());
190 assert_eq!(MyInt(5), 5.into());
191 assert_eq!(Ok(MyInt(5)), "5".parse());
192 assert_eq!(5, MyInt(5).into());
193 assert_eq!(MyInt(5), MyInt::new(5));
194 assert_eq!(-MyInt(5), (-5).into());
195 assert_eq!("30", format!("{}", MyInt(30)));
196 assert_eq!("36", format!("{:o}", MyInt(30)));
197 assert_eq!("100", format!("{:b}", MyInt(4)));
198 assert_eq!(!MyBool(true), false.into());
199 assert_eq!(MyIntEnum::SmallInt(5), 5.into());
200
201 assert_eq!(SimpleStruct { int1: 5 }, 5.into());
202 assert_eq!(5u64, SimpleStruct { int1: 5 }.into());
203 assert_eq!(Ok(SimpleStruct { int1: 5 }), "5".parse());
204 assert_eq!("5", format!("{}", SimpleStruct { int1: 5 }));
205 assert_eq!(NormalStruct { int1: 5, int2: 6 }, (5, 6).into());
206 assert_eq!(SimpleStruct { int1: 5 }, SimpleStruct::new(5));
207 assert_eq!(NormalStruct { int1: 5, int2: 6 }, NormalStruct::new(5, 6));
208 assert_eq!((5, 6), NormalStruct::new(5, 6).into());
209 let mut norm_struct = NormalStruct::new(5, 6);
210 let uints_ref: (&u64, &u64) = (&norm_struct).into();
211 assert_eq!((&5, &6), uints_ref);
212 let uints_ref_mut: (&mut u64, &mut u64) = (&mut norm_struct).into();
213 assert_eq!((&mut 5, &mut 6), uints_ref_mut);
214
215 assert_eq!(MyInt(4) + MyInt(1), 5.into());
216 myint += MyInt(3);
217 assert_eq!(myint, 8.into());
218 myint *= 5;
219 assert_eq!(myint, 40.into());
220 assert_eq!(MyInt(4) + MyInt(1), 5.into());
221 assert_eq!(MyUInt(4, 5) + MyUInt(1, 2), MyUInt(5, 7));
222 assert_eq!(MyUInt(4, 5), MyUInt::new(4, 5));
223 assert_eq!((4, 5), MyUInt(4, 5).into());
224 let mut s1 = NormalStruct { int1: 1, int2: 2 };
225 let s2 = NormalStruct { int1: 2, int2: 3 };
226 let s3 = NormalStruct { int1: 3, int2: 5 };
227 assert_eq!(s1 + s2, s3);
228 assert_eq!(s3 - s2, s1);
229 s1 += s2;
230 assert_eq!(s1, s3);
231
232 assert_eq!((SimpleMyIntEnum::Int(6) + 5.into()).unwrap(), 11.into());
233 assert_eq!((SimpleMyIntEnum::Int(6) - 5.into()).unwrap(), 1.into());
234 assert_eq!((SimpleMyIntEnum::Int(6) - 5.into()).unwrap(), 1.into());
235 assert_eq!(-SimpleSignedIntEnum::Int(6), (-6i32).into());
236 assert_eq!(
237 (SimpleEnum::LabeledInts { a: 6, b: 5 }
238 + SimpleEnum::LabeledInts { a: 1, b: 4 })
239 .unwrap(),
240 SimpleEnum::LabeledInts { a: 7, b: 9 }
241 );
242
243 let _ = (MyIntEnum::SmallInt(5) + 6.into()).unwrap();
244 assert_eq!((-SimpleEnum::Int(5)).unwrap(), (-5).into());
245
246 assert_eq!(MyInt(50), MyInt(5) * 10);
247 assert_eq!(DoubleUInt(5, 6) * 10, DoubleUInt(50, 60));
248 // assert_eq!(DoubleUIntStruct{x:5, y:6} * 10, DoubleUIntStruct{x:50, y:60});
249
250 let mut myint = MyInt(5);
251 assert_eq!(5, *myint);
252 *myint = 7;
253 assert_eq!(MyInt(7), myint);
254
255 let mut my_vec = MyVec(vec![5, 8]);
256 assert_eq!(5, my_vec[0]);
257 assert_eq!(8, my_vec[1]);
258 my_vec[0] = 20;
259 assert_eq!(20, my_vec[0]);
260 assert_eq!((&my_vec).into_iter().next(), Some(&20));
261 assert_eq!((&mut my_vec).into_iter().next(), Some(&mut 20));
262 assert_eq!(my_vec.into_iter().next(), Some(20));
263
264 let int_vec = vec![MyInt2(2), MyInt2(3)];
265 assert_eq!(MyInt2(5), int_vec.clone().into_iter().sum());
266 assert_eq!(MyInt2(6), int_vec.clone().into_iter().product());
267 let mut myint2 = MyInt2(8);
268 myint2 *= MyInt2(4);
269 assert_eq!(MyInt2(32), myint2);
270
271 let mut boxed = MyBoxedInt(Box::new(5));
272 assert_eq!(5, *boxed);
273 *boxed = 7;
274 assert_eq!(MyBoxedInt(Box::new(7)), boxed)
275 }
276