| 1 |
#![no_std] |
| 2 |
#![allow(dead_code)] // some code is tested for type checking only |
| 3 |
|
| 4 |
use derive_more::with_trait::{ |
| 5 |
Add, AddAssign, Constructor, Deref, DerefMut, Display, From, FromStr, Index, |
| 6 |
IndexMut, Into, IntoIterator, Mul, MulAssign, Not, Sum, TryInto, |
| 7 |
}; |
| 8 |
|
| 9 |
#[derive( |
| 10 |
AddAssign, |
| 11 |
MulAssign, |
| 12 |
Add, |
| 13 |
Mul, |
| 14 |
Not, |
| 15 |
Index, |
| 16 |
Display, |
| 17 |
FromStr, |
| 18 |
Into, |
| 19 |
From, |
| 20 |
IndexMut, |
| 21 |
Sum, |
| 22 |
Deref, |
| 23 |
DerefMut, |
| 24 |
Constructor |
| 25 |
)] |
| 26 |
#[into(owned, ref, ref_mut)] |
| 27 |
struct MyInts(u64); |
| 28 |
|
| 29 |
#[derive(Deref, DerefMut)] |
| 30 |
#[deref(forward)] |
| 31 |
#[deref_mut(forward)] |
| 32 |
struct MyBoxedInt<'a>(&'a mut u64); |
| 33 |
|
| 34 |
#[derive( |
| 35 |
From, |
| 36 |
FromStr, |
| 37 |
Display, |
| 38 |
Index, |
| 39 |
Not, |
| 40 |
Add, |
| 41 |
Mul, |
| 42 |
Sum, |
| 43 |
IndexMut, |
| 44 |
AddAssign, |
| 45 |
Deref, |
| 46 |
DerefMut, |
| 47 |
IntoIterator, |
| 48 |
Constructor |
| 49 |
)] |
| 50 |
#[deref(forward)] |
| 51 |
#[deref_mut(forward)] |
| 52 |
#[into_iterator(owned, ref, ref_mut)] |
| 53 |
struct Wrapped<T: Clone>(T); |
| 54 |
|
| 55 |
#[derive(Deref, DerefMut)] |
| 56 |
struct Wrapped2<T: Clone>(T); |
| 57 |
|
| 58 |
#[derive(From, Not, Add, Mul, AddAssign, Constructor, Sum)] |
| 59 |
struct WrappedDouble<T: Clone, U: Clone>(T, U); |
| 60 |
|
| 61 |
#[derive(Add, Not, TryInto)] |
| 62 |
#[try_into(owned, ref, ref_mut)] |
| 63 |
enum MixedInts { |
| 64 |
SmallInt(i32), |
| 65 |
BigInt(i64), |
| 66 |
TwoSmallInts(i32, i32), |
| 67 |
NamedSmallInts { x: i32, y: i32 }, |
| 68 |
UnsignedOne(u32), |
| 69 |
UnsignedTwo(u32), |
| 70 |
} |
| 71 |
|
| 72 |
#[derive(Not, Add)] |
| 73 |
enum EnumWithUnit { |
| 74 |
SmallInt(i32), |
| 75 |
Unit, |
| 76 |
} |
| 77 |
|
| 78 |
#[rustversion::nightly] |
| 79 |
mod error { |
| 80 |
use derive_more::with_trait::{Display, Error, From}; |
| 81 |
|
| 82 |
#[derive(Default, Debug, Display, Error)] |
| 83 |
struct Simple; |
| 84 |
|
| 85 |
#[derive(Default, Debug, Display, Error)] |
| 86 |
struct WithSource { |
| 87 |
source: Simple, |
| 88 |
} |
| 89 |
#[derive(Default, Debug, Display, Error)] |
| 90 |
struct WithExplicitSource { |
| 91 |
#[error(source)] |
| 92 |
explicit_source: Simple, |
| 93 |
} |
| 94 |
|
| 95 |
#[derive(Default, Debug, Display, Error)] |
| 96 |
struct Tuple(Simple); |
| 97 |
|
| 98 |
#[derive(Default, Debug, Display, Error)] |
| 99 |
struct WithoutSource(#[error(not(source))] i32); |
| 100 |
#[derive(Debug, Display, Error, From)] |
| 101 |
enum CompoundError { |
| 102 |
Simple, |
| 103 |
WithSource { |
| 104 |
source: Simple, |
| 105 |
}, |
| 106 |
WithExplicitSource { |
| 107 |
#[error(source)] |
| 108 |
explicit_source: WithSource, |
| 109 |
}, |
| 110 |
Tuple(WithExplicitSource), |
| 111 |
WithoutSource(#[error(not(source))] Tuple), |
| 112 |
} |
| 113 |
|
| 114 |
#[test] |
| 115 |
fn assert() { |
| 116 |
assert!(Simple.source().is_none()); |
| 117 |
assert!(WithSource::default().source().is_some()); |
| 118 |
assert!(WithExplicitSource::default().source().is_some()); |
| 119 |
assert!(Tuple::default().source().is_some()); |
| 120 |
assert!(Tuple::default().source().is_some()); |
| 121 |
assert!(WithoutSource::default().source().is_none()); |
| 122 |
assert!(CompoundError::Simple.source().is_none()); |
| 123 |
assert!(CompoundError::from(Simple).source().is_some()); |
| 124 |
assert!(CompoundError::from(WithSource::default()) |
| 125 |
.source() |
| 126 |
.is_some()); |
| 127 |
assert!(CompoundError::from(WithExplicitSource::default()) |
| 128 |
.source() |
| 129 |
.is_some()); |
| 130 |
assert!(CompoundError::from(Tuple::default()).source().is_none()); |
| 131 |
} |
| 132 |
} |