summaryrefslogtreecommitdiffstats
path: root/vendor/derive_more/tests/deref_mut.rs
blob: 51a95e4f2956ca50209332d26e9e1d76b4358a1a (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
#![allow(dead_code, unused_imports)]
#[macro_use]
extern crate derive_more;

#[derive(DerefMut)]
#[deref_mut(forward)]
struct MyBoxedInt(Box<i32>);
// Deref implementation is needed for DerefMut
impl ::core::ops::Deref for MyBoxedInt {
    type Target = <Box<i32> as ::core::ops::Deref>::Target;
    #[inline]
    fn deref(&self) -> &Self::Target {
        <Box<i32> as ::core::ops::Deref>::deref(&self.0)
    }
}

#[derive(DerefMut)]
struct NumRef<'a> {
    #[deref_mut(forward)]
    num: &'a mut i32,
}
// Deref implementation is needed for DerefMut
impl<'a> ::core::ops::Deref for NumRef<'a> {
    type Target = <&'a mut i32 as ::core::ops::Deref>::Target;
    #[inline]
    fn deref(&self) -> &Self::Target {
        <&'a mut i32 as ::core::ops::Deref>::deref(&self.num)
    }
}

#[derive(DerefMut)]
#[deref_mut(forward)]
struct NumRef2<'a> {
    num: &'a mut i32,
    #[deref_mut(ignore)]
    useless: bool,
}

// Deref implementation is needed for DerefMut
impl<'a> ::core::ops::Deref for NumRef2<'a> {
    type Target = <&'a mut i32 as ::core::ops::Deref>::Target;
    #[inline]
    fn deref(&self) -> &Self::Target {
        <&'a mut i32 as ::core::ops::Deref>::deref(&self.num)
    }
}

#[derive(DerefMut)]
struct MyInt(i32);

// Deref implementation is needed for DerefMutToInner
impl ::core::ops::Deref for MyInt {
    type Target = i32;
    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[derive(DerefMut)]
struct Point1D {
    x: i32,
}

// Deref implementation is needed for DerefMutToInner
impl ::core::ops::Deref for Point1D {
    type Target = i32;
    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.x
    }
}

#[derive(DerefMut)]
struct CoolVec {
    cool: bool,
    #[deref_mut]
    vec: Vec<i32>,
}
impl ::core::ops::Deref for CoolVec {
    type Target = Vec<i32>;
    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.vec
    }
}

#[derive(DerefMut)]
struct GenericVec<T>(Vec<T>);

impl<T> ::core::ops::Deref for GenericVec<T> {
    type Target = Vec<T>;
    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[test]
fn deref_mut_generic() {
    let mut gv = GenericVec::<i32>(vec![42]);
    assert!(gv.get_mut(0).is_some());
}

#[derive(DerefMut)]
struct GenericBox<T>(#[deref_mut(forward)] Box<T>);

impl<T> ::core::ops::Deref for GenericBox<T>
where
    Box<T>: ::core::ops::Deref,
{
    type Target = <Box<T> as ::core::ops::Deref>::Target;
    #[inline]
    fn deref(&self) -> &Self::Target {
        <Box<T> as ::core::ops::Deref>::deref(&self.0)
    }
}

#[test]
fn deref_mut_generic_forward() {
    let mut boxed = GenericBox(Box::new(1i32));
    *boxed = 3;
    assert_eq!(*boxed, 3i32);
}