summaryrefslogtreecommitdiffstats
path: root/vendor/derive_more/tests/try_into.rs
blob: 837c8e991315961b75de6cd15ac88d123c24c065 (plain)
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
#![allow(dead_code)]

#[macro_use]
extern crate derive_more;

use std::convert::{TryFrom, TryInto};

// Ensure that the TryFrom macro is hygenic and doesn't break when `Result` has
// been redefined.
type Result = ();

#[derive(Clone, Copy, TryInto)]
#[try_into(owned, ref, ref_mut)]
enum MixedInts {
    SmallInt(i32),
    NamedBigInt {
        int: i64,
    },
    UnsignedWithIgnoredField(#[try_into(ignore)] bool, i64),
    NamedUnsignedWithIgnnoredField {
        #[try_into(ignore)]
        useless: bool,
        x: i64,
    },
    TwoSmallInts(i32, i32),
    NamedBigInts {
        x: i64,
        y: i64,
    },
    Unsigned(u32),
    NamedUnsigned {
        x: u32,
    },
    Unit,
    #[try_into(ignore)]
    Unit2,
}

#[test]
fn test_try_into() {
    let mut i = MixedInts::SmallInt(42);
    assert_eq!(Ok(42i32), i.try_into());
    assert_eq!(Ok(&42i32), (&i).try_into());
    assert_eq!(Ok(&mut 42i32), (&mut i).try_into());
    assert_eq!(
        i64::try_from(i),
        Err("Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnnoredField can be converted to i64")
    );
    assert_eq!(
        <(i32, i32)>::try_from(i),
        Err("Only TwoSmallInts can be converted to (i32, i32)")
    );
    assert_eq!(
        <(i64, i64)>::try_from(i),
        Err("Only NamedBigInts can be converted to (i64, i64)")
    );
    assert_eq!(
        u32::try_from(i),
        Err("Only Unsigned, NamedUnsigned can be converted to u32")
    );
    assert_eq!(<()>::try_from(i), Err("Only Unit can be converted to ()"));

    let mut i = MixedInts::NamedBigInt { int: 42 };
    assert_eq!(
        i32::try_from(i),
        Err("Only SmallInt can be converted to i32")
    );
    assert_eq!(Ok(42i64), i.try_into());
    assert_eq!(Ok(&42i64), (&i).try_into());
    assert_eq!(Ok(&mut 42i64), (&mut i).try_into());
    assert_eq!(
        <(i32, i32)>::try_from(i),
        Err("Only TwoSmallInts can be converted to (i32, i32)")
    );
    assert_eq!(
        <(i64, i64)>::try_from(i),
        Err("Only NamedBigInts can be converted to (i64, i64)")
    );
    assert_eq!(
        u32::try_from(i),
        Err("Only Unsigned, NamedUnsigned can be converted to u32")
    );
    assert_eq!(<()>::try_from(i), Err("Only Unit can be converted to ()"));

    let mut i = MixedInts::TwoSmallInts(42, 64);
    assert_eq!(
        i32::try_from(i),
        Err("Only SmallInt can be converted to i32")
    );
    assert_eq!(
        i64::try_from(i),
        Err("Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnnoredField can be converted to i64")
    );
    assert_eq!(Ok((42i32, 64i32)), i.try_into());
    assert_eq!(Ok((&42i32, &64i32)), (&i).try_into());
    assert_eq!(Ok((&mut 42i32, &mut 64i32)), (&mut i).try_into());
    assert_eq!(
        <(i64, i64)>::try_from(i),
        Err("Only NamedBigInts can be converted to (i64, i64)")
    );
    assert_eq!(
        u32::try_from(i),
        Err("Only Unsigned, NamedUnsigned can be converted to u32")
    );
    assert_eq!(<()>::try_from(i), Err("Only Unit can be converted to ()"));

    let mut i = MixedInts::NamedBigInts { x: 42, y: 64 };
    assert_eq!(
        i32::try_from(i),
        Err("Only SmallInt can be converted to i32")
    );
    assert_eq!(
        i64::try_from(i),
        Err("Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnnoredField can be converted to i64")
    );
    assert_eq!(
        <(i32, i32)>::try_from(i),
        Err("Only TwoSmallInts can be converted to (i32, i32)")
    );
    assert_eq!(Ok((42i64, 64i64)), i.try_into());
    assert_eq!(Ok((&42i64, &64i64)), (&i).try_into());
    assert_eq!(Ok((&mut 42i64, &mut 64i64)), (&mut i).try_into());
    assert_eq!(
        u32::try_from(i),
        Err("Only Unsigned, NamedUnsigned can be converted to u32")
    );
    assert_eq!(<()>::try_from(i), Err("Only Unit can be converted to ()"));

    let mut i = MixedInts::Unsigned(42);
    assert_eq!(
        i32::try_from(i),
        Err("Only SmallInt can be converted to i32")
    );
    assert_eq!(
        i64::try_from(i),
        Err("Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnnoredField can be converted to i64")
    );
    assert_eq!(
        <(i32, i32)>::try_from(i),
        Err("Only TwoSmallInts can be converted to (i32, i32)")
    );
    assert_eq!(
        <(i64, i64)>::try_from(i),
        Err("Only NamedBigInts can be converted to (i64, i64)")
    );
    assert_eq!(Ok(42u32), i.try_into());
    assert_eq!(Ok(&42u32), (&i).try_into());
    assert_eq!(Ok(&mut 42u32), (&mut i).try_into());
    assert_eq!(<()>::try_from(i), Err("Only Unit can be converted to ()"));

    let mut i = MixedInts::NamedUnsigned { x: 42 };
    assert_eq!(
        i32::try_from(i),
        Err("Only SmallInt can be converted to i32")
    );
    assert_eq!(
        i64::try_from(i),
        Err("Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnnoredField can be converted to i64")
    );
    assert_eq!(
        i64::try_from(i),
        Err("Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnnoredField can be converted to i64")
    );
    assert_eq!(
        <(i32, i32)>::try_from(i),
        Err("Only TwoSmallInts can be converted to (i32, i32)")
    );
    assert_eq!(
        <(i64, i64)>::try_from(i),
        Err("Only NamedBigInts can be converted to (i64, i64)")
    );
    assert_eq!(Ok(42u32), i.try_into());
    assert_eq!(Ok(&42u32), (&i).try_into());
    assert_eq!(Ok(&mut 42u32), (&mut i).try_into());
    assert_eq!(<()>::try_from(i), Err("Only Unit can be converted to ()"));

    let i = MixedInts::Unit;
    assert_eq!(
        i32::try_from(i),
        Err("Only SmallInt can be converted to i32")
    );
    assert_eq!(
        i64::try_from(i),
        Err("Only NamedBigInt, UnsignedWithIgnoredField, NamedUnsignedWithIgnnoredField can be converted to i64")
    );
    assert_eq!(
        <(i32, i32)>::try_from(i),
        Err("Only TwoSmallInts can be converted to (i32, i32)")
    );
    assert_eq!(
        <(i64, i64)>::try_from(i),
        Err("Only NamedBigInts can be converted to (i64, i64)")
    );
    assert_eq!(
        u32::try_from(i),
        Err("Only Unsigned, NamedUnsigned can be converted to u32")
    );
    assert_eq!(Ok(()), i.try_into());
}