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