-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathpinned_drop.rs
131 lines (114 loc) · 2.68 KB
/
pinned_drop.rs
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
#![warn(unsafe_code)]
#![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 Foo<'a> {
was_dropped: &'a mut bool,
#[pin]
field: u8,
}
#[pinned_drop]
impl PinnedDrop for Foo<'_> {
fn drop(self: Pin<&mut Self>) {
**self.project().was_dropped = true;
}
}
let mut was_dropped = false;
drop(Foo { was_dropped: &mut was_dropped, field: 42 });
assert!(was_dropped);
}
#[test]
fn test_mut_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 test_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 test_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) {}
}
});
self.data;
}
}
}
#[test]
fn test_call_self() {
#[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 test_self_match() {
#[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(0);
}
}
}