Debian Codegraph @

third_party/rust/derive_more/tests/lib.rs

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