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