summaryrefslogtreecommitdiffstats
path: root/third_party/rust/derive_more/tests/into.rs
blob: 075abd1c98ab9d72c3a6281fb24c07c422b46d96 (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
#![allow(dead_code)]
#[macro_use]
extern crate derive_more;

use std::borrow::Cow;

#[derive(Into)]
#[into(owned, ref, ref_mut)]
struct EmptyTuple();

#[derive(Into)]
#[into(owned, ref, ref_mut)]
struct EmptyStruct {}

#[derive(Into)]
#[into(owned, ref, ref_mut)]
struct EmptyUnit;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[derive(Into)]
#[into(owned(types(i64, i128)), ref, ref_mut)]
struct MyInt(i32);

#[test]
fn explicit_types_struct_owned_only() {
    assert_eq!(i32::from(MyInt(42)), 42i32);
    assert_eq!(<&i32>::from(&MyInt(42)), &42i32);
    assert_eq!(<&mut i32>::from(&mut MyInt(42)), &mut 42i32);
    assert_eq!(i64::from(MyInt(42)), 42i64);
    assert_eq!(i128::from(MyInt(42)), 42i128);
}

#[derive(Into)]
#[into(owned, ref, ref_mut)]
struct MyInts(i32, i32);

#[derive(Into)]
#[into(owned, ref, ref_mut)]
struct Point1D {
    x: i32,
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Into)]
#[into(owned, ref, ref_mut)]
struct Point2D {
    x: i32,
    y: i32,
}

#[derive(Into)]
#[into(owned, ref, ref_mut)]
struct Point2DWithIgnored {
    x: i32,
    y: i32,
    #[into(ignore)]
    useless: bool,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[derive(Into)]
#[into(owned(types(i64, i128)), ref, ref_mut, types(i32))]
struct MyIntExplicit(MyInt);

#[test]
fn explicit_types_struct_all() {
    let mut input = MyIntExplicit(MyInt(42));
    assert_eq!(MyInt::from(input), MyInt(42));
    assert_eq!(<&MyInt>::from(&input), &MyInt(42));
    assert_eq!(<&mut MyInt>::from(&mut input), &mut MyInt(42));
    assert_eq!(i32::from(input), 42i32);
    assert_eq!(<&i32>::from(&input), &42i32);
    assert_eq!(<&mut i32>::from(&mut input), &mut 42i32);
    assert_eq!(i64::from(input), 42i64);
    assert_eq!(i128::from(input), 42i128);
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[derive(Into)]
#[into(owned(types(i32, i64, i128)), ref(types(i32)), ref_mut(types(i32)))]
struct MyIntsExplicit(i32, MyInt, MyIntExplicit);

#[test]
fn explicit_types_struct_tupled() {
    let mut input = MyIntsExplicit(42i32, MyInt(42), MyIntExplicit(MyInt(42)));
    assert_eq!(
        <(i32, MyInt, MyIntExplicit)>::from(input),
        (42i32, MyInt(42), MyIntExplicit(MyInt(42))),
    );
    assert_eq!(
        <(&i32, &MyInt, &MyIntExplicit)>::from(&input),
        (&42i32, &MyInt(42), &MyIntExplicit(MyInt(42))),
    );
    assert_eq!(
        <(&mut i32, &mut MyInt, &mut MyIntExplicit)>::from(&mut input),
        (&mut 42i32, &mut MyInt(42), &mut MyIntExplicit(MyInt(42))),
    );
    assert_eq!(<(i32, i32, i32)>::from(input), (42i32, 42i32, 42i32));
    assert_eq!(<(&i32, &i32, &i32)>::from(&input), (&42i32, &42i32, &42i32));
    assert_eq!(
        <(&mut i32, &mut i32, &mut i32)>::from(&mut input),
        (&mut 42i32, &mut 42i32, &mut 42i32),
    );
    assert_eq!(<(i64, i64, i64)>::from(input), (42i64, 42i64, 42i64));
    assert_eq!(<(i128, i128, i128)>::from(input), (42i128, 42i128, 42i128));
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[derive(Into)]
#[into(owned, ref, ref_mut, types(i32))]
struct Point2DExplicit {
    x: MyInt,
    y: MyInt,
}

#[test]
fn explicit_types_point_2d() {
    let mut input = Point2DExplicit {
        x: MyInt(42),
        y: MyInt(42),
    };
    assert_eq!(<(i32, i32)>::from(input), (42i32, 42i32));
    assert_eq!(<(&i32, &i32)>::from(&input), (&42i32, &42i32));
    assert_eq!(
        <(&mut i32, &mut i32)>::from(&mut input),
        (&mut 42i32, &mut 42i32)
    );
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Into)]
#[into(owned(types("Cow<'_, str>")))]
struct Name(String);

#[test]
fn explicit_complex_types_name() {
    let name = "Ñolofinwë";
    let input = Name(name.to_owned());
    assert_eq!(String::from(input.clone()), name.to_owned());
    assert_eq!(Cow::from(input.clone()), Cow::Borrowed(name));
}