Debian Codegraph @

third_party/rust/derive_more/tests/try_into.rs

raw
1 #![cfg_attr(not(feature = "std"), no_std)]
2 #![allow(dead_code)]
3
4 #[cfg(not(feature = "std"))]
5 extern crate alloc;
6
7 #[cfg(not(feature = "std"))]
8 use alloc::string::ToString;
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 #[derive(TryInto, Clone, Copy, Debug, Eq, PartialEq)]
17 #[try_into(owned, ref, ref_mut)]
18 enum MixedInts {
19 SmallInt(i32),
20 NamedBigInt {
21 int: i64,
22 },
23 UnsignedWithIgnoredField(#[try_into(ignore)] bool, i64),
24 NamedUnsignedWithIgnoredField {
25 #[try_into(ignore)]
26 useless: bool,
27 x: i64,
28 },
29 TwoSmallInts(i32, i32),
30 NamedBigInts {
31 x: i64,
32 y: i64,
33 },
34 Unsigned(u32),
35 NamedUnsigned {
36 x: u32,
37 },
38 Unit,
39 #[try_into(ignore)]
40 Unit2,
41 }
42
43 #[test]
44 fn test_try_into() {
45 let mut i = MixedInts::SmallInt(42);
46 assert_eq!(42i32, i.try_into().unwrap());
47 assert_eq!(&42i32, <_ as TryInto<&i32>>::try_into(&i).unwrap());
48 assert_eq!(
49 &mut 42i32,
50 <_ as TryInto<&mut i32>>::try_into(&mut i).unwrap()
51 );
52 assert_eq!(
53 i64::try_from(i).unwrap_err().to_string(),
54 "Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnoredField can be converted to i64"
55 );
56 assert_eq!(i64::try_from(i).unwrap_err().input, MixedInts::SmallInt(42));
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!(
76 i32::try_from(i).unwrap_err().to_string(),
77 "Only SmallInt can be converted to i32"
78 );
79 assert_eq!(42i64, i.try_into().unwrap());
80 assert_eq!(&42i64, <_ as TryInto<&i64>>::try_into(&i).unwrap());
81 assert_eq!(
82 &mut 42i64,
83 <_ as TryInto<&mut i64>>::try_into(&mut i).unwrap()
84 );
85 assert_eq!(
86 <(i32, i32)>::try_from(i).unwrap_err().to_string(),
87 "Only TwoSmallInts can be converted to (i32, i32)"
88 );
89 assert_eq!(
90 <(i64, i64)>::try_from(i).unwrap_err().to_string(),
91 "Only NamedBigInts can be converted to (i64, i64)"
92 );
93 assert_eq!(
94 u32::try_from(i).unwrap_err().to_string(),
95 "Only Unsigned, NamedUnsigned can be converted to u32"
96 );
97 assert_eq!(
98 <()>::try_from(i).unwrap_err().to_string(),
99 "Only Unit can be converted to ()"
100 );
101
102 let mut i = MixedInts::TwoSmallInts(42, 64);
103 assert_eq!(
104 i32::try_from(i).unwrap_err().to_string(),
105 "Only SmallInt can be converted to i32"
106 );
107 assert_eq!(
108 i64::try_from(i).unwrap_err().to_string(),
109 "Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnoredField can be converted to i64"
110 );
111 assert_eq!((42i32, 64i32), i.try_into().unwrap());
112 assert_eq!((&42i32, &64i32), (&i).try_into().unwrap());
113 assert_eq!((&mut 42i32, &mut 64i32), (&mut i).try_into().unwrap());
114 assert_eq!(
115 <(i64, i64)>::try_from(i).unwrap_err().to_string(),
116 "Only NamedBigInts can be converted to (i64, i64)"
117 );
118 assert_eq!(
119 u32::try_from(i).unwrap_err().to_string(),
120 "Only Unsigned, NamedUnsigned can be converted to u32"
121 );
122 assert_eq!(
123 <()>::try_from(i).unwrap_err().to_string(),
124 "Only Unit can be converted to ()"
125 );
126
127 let mut i = MixedInts::NamedBigInts { x: 42, y: 64 };
128 assert_eq!(
129 i32::try_from(i).unwrap_err().to_string(),
130 "Only SmallInt can be converted to i32"
131 );
132 assert_eq!(
133 i64::try_from(i).unwrap_err().to_string(),
134 "Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnoredField can be converted to i64"
135 );
136 assert_eq!(
137 <(i32, i32)>::try_from(i).unwrap_err().to_string(),
138 "Only TwoSmallInts can be converted to (i32, i32)"
139 );
140 assert_eq!((42i64, 64i64), i.try_into().unwrap());
141 assert_eq!((&42i64, &64i64), (&i).try_into().unwrap());
142 assert_eq!((&mut 42i64, &mut 64i64), (&mut i).try_into().unwrap());
143 assert_eq!(
144 u32::try_from(i).unwrap_err().to_string(),
145 "Only Unsigned, NamedUnsigned can be converted to u32"
146 );
147 assert_eq!(
148 <()>::try_from(i).unwrap_err().to_string(),
149 "Only Unit can be converted to ()"
150 );
151
152 let mut i = MixedInts::Unsigned(42);
153 assert_eq!(
154 i32::try_from(i).unwrap_err().to_string(),
155 "Only SmallInt can be converted to i32"
156 );
157 assert_eq!(
158 i64::try_from(i).unwrap_err().to_string(),
159 "Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnoredField can be converted to i64"
160 );
161 assert_eq!(
162 <(i32, i32)>::try_from(i).unwrap_err().to_string(),
163 "Only TwoSmallInts can be converted to (i32, i32)"
164 );
165 assert_eq!(
166 <(i64, i64)>::try_from(i).unwrap_err().to_string(),
167 "Only NamedBigInts can be converted to (i64, i64)"
168 );
169 assert_eq!(42u32, i.try_into().unwrap());
170 assert_eq!(&42u32, <_ as TryInto<&u32>>::try_into(&i).unwrap());
171 assert_eq!(
172 &mut 42u32,
173 <_ as TryInto<&mut u32>>::try_into(&mut i).unwrap()
174 );
175 assert_eq!(
176 <()>::try_from(i).unwrap_err().to_string(),
177 "Only Unit can be converted to ()"
178 );
179
180 let mut i = MixedInts::NamedUnsigned { x: 42 };
181 assert_eq!(
182 i32::try_from(i).unwrap_err().to_string(),
183 "Only SmallInt can be converted to i32"
184 );
185 assert_eq!(
186 i64::try_from(i).unwrap_err().to_string(),
187 "Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnoredField can be converted to i64"
188 );
189 assert_eq!(
190 i64::try_from(i).unwrap_err().to_string(),
191 "Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnoredField can be converted to i64"
192 );
193 assert_eq!(
194 <(i32, i32)>::try_from(i).unwrap_err().to_string(),
195 "Only TwoSmallInts can be converted to (i32, i32)"
196 );
197 assert_eq!(
198 <(i64, i64)>::try_from(i).unwrap_err().to_string(),
199 "Only NamedBigInts can be converted to (i64, i64)"
200 );
201 assert_eq!(42u32, i.try_into().unwrap());
202 assert_eq!(&42u32, <_ as TryInto<&u32>>::try_into(&i).unwrap());
203 assert_eq!(
204 &mut 42u32,
205 <_ as TryInto<&mut u32>>::try_into(&mut i).unwrap()
206 );
207 assert_eq!(
208 <()>::try_from(i).unwrap_err().to_string(),
209 "Only Unit can be converted to ()"
210 );
211
212 let i = MixedInts::Unit;
213 assert_eq!(
214 i32::try_from(i).unwrap_err().to_string(),
215 "Only SmallInt can be converted to i32"
216 );
217 assert_eq!(
218 i64::try_from(i).unwrap_err().to_string(),
219 "Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnoredField can be converted to i64"
220 );
221 assert_eq!(
222 <(i32, i32)>::try_from(i).unwrap_err().to_string(),
223 "Only TwoSmallInts can be converted to (i32, i32)"
224 );
225 assert_eq!(
226 <(i64, i64)>::try_from(i).unwrap_err().to_string(),
227 "Only NamedBigInts can be converted to (i64, i64)"
228 );
229 assert_eq!(
230 u32::try_from(i).unwrap_err().to_string(),
231 "Only Unsigned, NamedUnsigned can be converted to u32"
232 );
233 assert_eq!((), i.try_into().unwrap());
234 }