aboutsummaryrefslogtreecommitdiff
path: root/tests/pinned_drop.rs
blob: b0677e2773311d6dae0b56cbadf1ecf25af19f83 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#![warn(rust_2018_idioms, single_use_lifetimes)]
#![allow(dead_code)]

use pin_project::{pin_project, pinned_drop};
use std::pin::Pin;

#[test]
fn safe_project() {
    #[pin_project(PinnedDrop)]
    pub struct Struct<'a> {
        was_dropped: &'a mut bool,
        #[pin]
        field: u8,
    }

    #[pinned_drop]
    impl PinnedDrop for Struct<'_> {
        fn drop(self: Pin<&mut Self>) {
            **self.project().was_dropped = true;
        }
    }

    let mut was_dropped = false;
    drop(Struct { was_dropped: &mut was_dropped, field: 42 });
    assert!(was_dropped);
}

#[test]
fn mut_self_argument() {
    #[pin_project(PinnedDrop)]
    struct Struct {
        data: usize,
    }

    #[pinned_drop]
    impl PinnedDrop for Struct {
        fn drop(mut self: Pin<&mut Self>) {
            let _: &mut _ = &mut self.data;
        }
    }
}

#[test]
fn self_in_vec() {
    #[pin_project(PinnedDrop)]
    struct Struct {
        data: usize,
    }

    #[pinned_drop]
    impl PinnedDrop for Struct {
        fn drop(self: Pin<&mut Self>) {
            let _: Vec<_> = vec![self.data];
        }
    }
}

#[test]
fn self_in_macro_containing_fn() {
    #[pin_project(PinnedDrop)]
    pub struct Struct {
        data: usize,
    }

    macro_rules! emit {
        ($($tt:tt)*) => {
            $($tt)*
        };
    }

    #[pinned_drop]
    impl PinnedDrop for Struct {
        fn drop(self: Pin<&mut Self>) {
            let _ = emit!({
                impl Struct {
                    pub fn f(self) {}
                }
            });
            let _ = self.data;
        }
    }
}

#[test]
fn self_call() {
    #[pin_project(PinnedDrop)]
    pub struct Struct {
        data: usize,
    }

    trait Trait {
        fn self_ref(&self) {}
        fn self_pin_ref(self: Pin<&Self>) {}
        fn self_mut(&mut self) {}
        fn self_pin_mut(self: Pin<&mut Self>) {}
        fn assoc_fn(_this: Pin<&mut Self>) {}
    }

    impl Trait for Struct {}

    #[pinned_drop]
    impl PinnedDrop for Struct {
        fn drop(mut self: Pin<&mut Self>) {
            self.self_ref();
            self.as_ref().self_pin_ref();
            self.self_mut();
            self.as_mut().self_pin_mut();
            Self::assoc_fn(self.as_mut());
            <Self>::assoc_fn(self.as_mut());
        }
    }
}

#[test]
fn self_expr() {
    #[pin_project(PinnedDrop)]
    pub struct Struct {
        x: usize,
    }

    #[pinned_drop]
    impl PinnedDrop for Struct {
        fn drop(mut self: Pin<&mut Self>) {
            let _: Self = Self { x: 0 };
        }
    }

    #[pin_project(PinnedDrop)]
    pub struct TupleStruct(usize);

    #[pinned_drop]
    impl PinnedDrop for TupleStruct {
        fn drop(mut self: Pin<&mut Self>) {
            let _: Self = Self(0);
        }
    }
}

#[rustversion::since(1.37)]
#[test]
fn self_expr_enum() {
    #[pin_project(PinnedDrop)]
    pub enum Enum {
        StructVariant { x: usize },
        TupleVariant(usize),
    }

    #[pinned_drop]
    impl PinnedDrop for Enum {
        fn drop(mut self: Pin<&mut Self>) {
            let _: Self = Self::StructVariant { x: 0 };
            let _: Self = Self::TupleVariant(0);
        }
    }
}

#[test]
fn self_pat() {
    #[pin_project(PinnedDrop)]
    pub struct Struct {
        x: usize,
    }

    #[pinned_drop]
    impl PinnedDrop for Struct {
        #[allow(irrefutable_let_patterns)]
        #[allow(clippy::match_single_binding)]
        fn drop(mut self: Pin<&mut Self>) {
            match *self {
                Self { x: _ } => {}
            }
            if let Self { x: _ } = *self {}
            let Self { x: _ } = *self;
        }
    }

    #[pin_project(PinnedDrop)]
    pub struct TupleStruct(usize);

    #[pinned_drop]
    impl PinnedDrop for TupleStruct {
        #[allow(irrefutable_let_patterns)]
        fn drop(mut self: Pin<&mut Self>) {
            match *self {
                Self(_) => {}
            }
            if let Self(_) = *self {}
            let Self(_) = *self;
        }
    }
}

#[rustversion::since(1.37)]
#[test]
fn self_pat_enum() {
    #[pin_project(PinnedDrop)]
    pub enum Enum {
        StructVariant { x: usize },
        TupleVariant(usize),
    }

    #[pinned_drop]
    impl PinnedDrop for Enum {
        fn drop(mut self: Pin<&mut Self>) {
            match *self {
                Self::StructVariant { x: _ } => {}
                Self::TupleVariant(_) => {}
            }
            if let Self::StructVariant { x: _ } = *self {}
            if let Self::TupleVariant(_) = *self {}
        }
    }
}

// See also `ui/pinned_drop/self.rs`.
#[rustversion::since(1.40)] // https://github.com/rust-lang/rust/pull/64690
#[test]
fn self_in_macro_def() {
    #[pin_project(PinnedDrop)]
    pub struct Struct {
        x: usize,
    }

    #[pinned_drop]
    impl PinnedDrop for Struct {
        fn drop(self: Pin<&mut Self>) {
            macro_rules! t {
                () => {{
                    let _ = self;
                }};
            }
            t!();
        }
    }
}