about summary refs log tree commit diff
path: root/tests/ui/deriving/deriving-from.rs
blob: 75988ba974d597b326311f22c269db1039f97884 (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
//@ edition: 2021
//@ run-pass

#![feature(derive_from)]

use core::from::From;

#[derive(From)]
struct TupleSimple(u32);

#[derive(From)]
struct TupleNonPathType([u32; 4]);

#[derive(From)]
struct TupleWithRef<'a, T>(&'a T);

#[derive(From)]
struct TupleSWithBound<T: std::fmt::Debug>(T);

#[derive(From)]
struct RawIdentifier {
    r#use: u32,
}

#[derive(From)]
struct Field {
    foo: bool,
}

#[derive(From)]
struct Const<const C: usize> {
    foo: [u32; C],
}

fn main() {
    let a = 42u32;
    let b: [u32; 4] = [0, 1, 2, 3];
    let c = true;

    let s1: TupleSimple = a.into();
    assert_eq!(s1.0, a);

    let s2: TupleNonPathType = b.into();
    assert_eq!(s2.0, b);

    let s3: TupleWithRef<u32> = (&a).into();
    assert_eq!(s3.0, &a);

    let s4: TupleSWithBound<u32> = a.into();
    assert_eq!(s4.0, a);

    let s5: RawIdentifier = a.into();
    assert_eq!(s5.r#use, a);

    let s6: Field = c.into();
    assert_eq!(s6.foo, c);

    let s7: Const<4> = b.into();
    assert_eq!(s7.foo, b);
}