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

#[macro_use]
extern crate derive_more;

use std::path::PathBuf;
use std::ptr;

#[derive(AsMut)]
struct SingleFieldTuple(String);

#[test]
fn single_field_tuple() {
    let mut item = SingleFieldTuple(String::from("test"));

    assert!(ptr::eq(&mut item.0, item.as_mut()));
}

#[derive(AsMut)]
#[as_mut(forward)]
struct SingleFieldForward(Vec<i32>);

#[test]
fn single_field_forward() {
    let mut item = SingleFieldForward(vec![]);
    let _: &mut [i32] = (&mut item).as_mut();
}

#[derive(AsMut)]
struct SingleFieldStruct {
    first: String,
}

#[test]
fn single_field_struct() {
    let mut item = SingleFieldStruct {
        first: String::from("test"),
    };

    assert!(ptr::eq(&mut item.first, item.as_mut()));
}

#[derive(AsMut)]
struct MultiFieldTuple(#[as_mut] String, #[as_mut] PathBuf, Vec<usize>);

#[test]
fn multi_field_tuple() {
    let mut item = MultiFieldTuple(String::from("test"), PathBuf::new(), vec![]);

    assert!(ptr::eq(&mut item.0, item.as_mut()));
    assert!(ptr::eq(&mut item.1, item.as_mut()));
}

#[derive(AsMut)]
struct MultiFieldStruct {
    #[as_mut]
    first: String,
    #[as_mut]
    second: PathBuf,
    third: Vec<usize>,
}

#[test]
fn multi_field_struct() {
    let mut item = MultiFieldStruct {
        first: String::from("test"),
        second: PathBuf::new(),
        third: vec![],
    };

    assert!(ptr::eq(&mut item.first, item.as_mut()));
    assert!(ptr::eq(&mut item.second, item.as_mut()));
}

#[derive(AsMut)]
struct SingleFieldGenericStruct<T> {
    first: T,
}

#[test]
fn single_field_generic_struct() {
    let mut item = SingleFieldGenericStruct {
        first: String::from("test"),
    };

    assert!(ptr::eq(&mut item.first, item.as_mut()));
}

#[derive(AsMut)]
struct MultiFieldGenericStruct<T> {
    #[as_mut]
    first: Vec<T>,
    #[as_mut]
    second: PathBuf,
    third: Vec<usize>,
}

#[test]
fn multi_field_generic_struct() {
    let mut item = MultiFieldGenericStruct {
        first: b"test".to_vec(),
        second: PathBuf::new(),
        third: vec![],
    };

    assert!(ptr::eq(&mut item.first, item.as_mut()));
    assert!(ptr::eq(&mut item.second, item.as_mut()));
}