| 1 |
#![cfg_attr(not(feature = "std"), no_std)] |
| 2 |
#![allow(dead_code)] // some code is tested for type checking only |
| 3 |
|
| 4 |
mod structs { |
| 5 |
use derive_more::AddAssign; |
| 6 |
|
| 7 |
#[test] |
| 8 |
fn multi_field_tuple() { |
| 9 |
#[derive(AddAssign, Debug, PartialEq)] |
| 10 |
struct MyInts(i32, i32); |
| 11 |
|
| 12 |
let mut a = MyInts(12, 21); |
| 13 |
a += MyInts(1, 2); |
| 14 |
|
| 15 |
assert_eq!(a, MyInts(13, 23)); |
| 16 |
} |
| 17 |
|
| 18 |
#[test] |
| 19 |
fn multi_field_struct() { |
| 20 |
#[derive(AddAssign, Debug, PartialEq)] |
| 21 |
struct Point2D { |
| 22 |
x: i32, |
| 23 |
y: i32, |
| 24 |
} |
| 25 |
|
| 26 |
let mut a = Point2D { x: 12, y: 21 }; |
| 27 |
a += Point2D { x: 1, y: 2 }; |
| 28 |
|
| 29 |
assert_eq!(a, Point2D { x: 13, y: 23 }); |
| 30 |
} |
| 31 |
|
| 32 |
mod generic { |
| 33 |
use derive_more::AddAssign; |
| 34 |
|
| 35 |
trait Some { |
| 36 |
type Assoc; |
| 37 |
} |
| 38 |
|
| 39 |
impl<T> Some for T { |
| 40 |
type Assoc = u32; |
| 41 |
} |
| 42 |
|
| 43 |
#[test] |
| 44 |
fn multi_field_tuple() { |
| 45 |
#[derive(AddAssign)] |
| 46 |
struct Foo<A: Some, B>(A::Assoc, B); |
| 47 |
|
| 48 |
let mut a = Foo::<(), _>(12, 21); |
| 49 |
a += Foo::<(), _>(1, 2); |
| 50 |
|
| 51 |
assert_eq!((a.0, a.1), (13, 23)); |
| 52 |
} |
| 53 |
|
| 54 |
#[test] |
| 55 |
fn multi_field_struct() { |
| 56 |
#[derive(AddAssign)] |
| 57 |
struct Bar<A, B: Some> { |
| 58 |
b: B::Assoc, |
| 59 |
i: A, |
| 60 |
} |
| 61 |
|
| 62 |
let mut a = Bar::<_, ()> { b: 12, i: 21 }; |
| 63 |
a += Bar::<_, ()> { b: 1, i: 2 }; |
| 64 |
|
| 65 |
assert_eq!((a.b, a.i), (13, 23)); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
mod ignore { |
| 70 |
use core::marker::PhantomData; |
| 71 |
|
| 72 |
use derive_more::AddAssign; |
| 73 |
|
| 74 |
#[test] |
| 75 |
fn tuple() { |
| 76 |
#[derive(AddAssign)] |
| 77 |
struct TupleWithZst<T = ()>(i32, #[add_assign(ignore)] PhantomData<T>); |
| 78 |
|
| 79 |
let mut a: TupleWithZst = TupleWithZst(12, PhantomData); |
| 80 |
a += TupleWithZst(2, PhantomData); |
| 81 |
|
| 82 |
assert_eq!(a.0, 14); |
| 83 |
} |
| 84 |
|
| 85 |
#[test] |
| 86 |
fn struct_() { |
| 87 |
#[derive(AddAssign)] |
| 88 |
struct StructWithZst<T = ()> { |
| 89 |
x: i32, |
| 90 |
#[add_assign(skip)] |
| 91 |
_marker: PhantomData<T>, |
| 92 |
} |
| 93 |
|
| 94 |
let mut a: StructWithZst = StructWithZst { |
| 95 |
x: 12, |
| 96 |
_marker: PhantomData, |
| 97 |
}; |
| 98 |
a += StructWithZst { |
| 99 |
x: 2, |
| 100 |
_marker: PhantomData, |
| 101 |
}; |
| 102 |
|
| 103 |
assert_eq!(a.x, 14); |
| 104 |
} |
| 105 |
} |
| 106 |
} |