-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
189 lines (165 loc) · 6.38 KB
/
Copy pathmain.rs
File metadata and controls
189 lines (165 loc) · 6.38 KB
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
//! Sometimes, we want to detect whether T implements traits such as [`Clone`],
//! [`Send`], [`Sync`], and so on at runtime.
//! To do that, we can take advantage of Rust's function lookup order.
/// When someone calls [`ImplDetector::is_clone`], Rust looks for a callable
/// function in the following order:
/// - An inherent function
/// - A trait function
///
/// If the type implements `Clone`, Rust chooses the inherent function because
/// of this lookup order. Otherwise, it chooses the trait function because the
/// inherent function has a `T: Clone` bound.
///
/// See https://doc.rust-lang.org/reference/expressions/method-call-expr.html
/// (The document describes methods, but the same rule appears to apply to
/// associated functions as well.)
///
/// More specific rules are described here:
/// 1. https://rust-lang.github.io/rfcs/0195-associated-items.html#via-an-id_segment-prefix
/// 2. https://rust-lang.github.io/rfcs/0195-associated-items.html#via-a-type_segment-prefix
/// (1) says that starting with `ID_SEGMENT` is equivalent to starting with `TYPE_SEGMENT`:
/// `A::b` is equivalent to `<A>::b`.
/// (2) says that inherent members take precedence over in-scope traits.
pub struct ImplDetector<T>(std::marker::PhantomData<T>);
// === ImplDetector for `Clone` ===
pub trait NotClone {
const IS_CLONE: bool = false;
fn is_clone() -> bool {
false
}
}
impl<T> NotClone for ImplDetector<T> {}
impl<T: Clone> ImplDetector<T> {
pub const IS_CLONE: bool = true;
pub fn is_clone() -> bool {
true
}
}
// === ImplDetector for `Send` ===
pub trait NotSend {
const IS_SEND: bool = false;
fn is_send() -> bool {
false
}
}
impl<T> NotSend for ImplDetector<T> {}
impl<T: Send> ImplDetector<T> {
pub const IS_SEND: bool = true;
pub fn is_send() -> bool {
true
}
}
// === ImplDetector for `Sync` ===
pub trait NotSync {
const IS_SYNC: bool = false;
fn is_sync() -> bool {
false
}
}
impl<T> NotSync for ImplDetector<T> {}
impl<T: Sync> ImplDetector<T> {
pub const IS_SYNC: bool = true;
pub fn is_sync() -> bool {
true
}
}
// === ImplDetector for `EqualType` ===
pub trait EqualType<T> {
const IS_EQUAL_TYPE: bool = false;
fn is_equal_type() -> bool {
false
}
}
impl<T> EqualType<T> for ImplDetector<T> {}
impl<T> ImplDetector<(T, T)> {
pub const IS_EQUAL_TYPE: bool = true;
pub fn is_equal_type() -> bool {
true
}
}
#[allow(dead_code)]
fn main() {
#[derive(Clone)]
struct Cloneable;
struct UnCloneable;
struct SendSync(i32); // i32 is both Send and Sync.
struct SyncNotSend(std::sync::MutexGuard<'static, i32>); // MutexGuard is Sync, but not Send.
struct SendNotSync(std::cell::Cell<i32>); // Cell is Send, but not Sync.
struct NotSendNotSync(*mut i32); // Raw pointer is neither Send nor Sync.
struct A;
struct B;
// Using syntax that begins with an identifier: `ID::...`
{
// === Detects `Clone` ===
assert!(ImplDetector::<Cloneable>::is_clone());
assert!(!ImplDetector::<UnCloneable>::is_clone());
assert!(!ImplDetector::<UnCloneable>::is_clone());
const _: () = {
assert!(ImplDetector::<Cloneable>::IS_CLONE);
assert!(!ImplDetector::<UnCloneable>::IS_CLONE);
};
// === Detects `Send` and `Sync` ===
assert!(ImplDetector::<SendSync>::is_send());
assert!(ImplDetector::<SendSync>::is_sync());
assert!(!ImplDetector::<SyncNotSend>::is_send());
assert!(ImplDetector::<SyncNotSend>::is_sync());
assert!(ImplDetector::<SendNotSync>::is_send());
assert!(!ImplDetector::<SendNotSync>::is_sync());
assert!(!ImplDetector::<NotSendNotSync>::is_send());
assert!(!ImplDetector::<NotSendNotSync>::is_sync());
const _: () = {
assert!(ImplDetector::<SendSync>::IS_SEND);
assert!(ImplDetector::<SendSync>::IS_SYNC);
assert!(!ImplDetector::<SyncNotSend>::IS_SEND);
assert!(ImplDetector::<SyncNotSend>::IS_SYNC);
assert!(ImplDetector::<SendNotSync>::IS_SEND);
assert!(!ImplDetector::<SendNotSync>::IS_SYNC);
assert!(!ImplDetector::<NotSendNotSync>::IS_SEND);
assert!(!ImplDetector::<NotSendNotSync>::IS_SYNC);
};
// === Detects `EqualType` ===
assert!(ImplDetector::<(A, A)>::is_equal_type());
assert!(!ImplDetector::<(A, B)>::is_equal_type());
const _: () = {
assert!(ImplDetector::<(A, A)>::IS_EQUAL_TYPE);
assert!(!ImplDetector::<(A, B)>::IS_EQUAL_TYPE);
};
}
// Using syntax that begins with a type: `<Type>::...`
{
// === Detects `Clone` ===
assert!(<ImplDetector::<Cloneable>>::is_clone());
assert!(!<ImplDetector::<UnCloneable>>::is_clone());
assert!(!<ImplDetector::<UnCloneable>>::is_clone());
const _: () = {
assert!(<ImplDetector::<Cloneable>>::IS_CLONE);
assert!(!<ImplDetector::<UnCloneable>>::IS_CLONE);
};
// === Detects `Send` and `Sync` ===
assert!(<ImplDetector::<SendSync>>::is_send());
assert!(<ImplDetector::<SendSync>>::is_sync());
assert!(!<ImplDetector::<SyncNotSend>>::is_send());
assert!(<ImplDetector::<SyncNotSend>>::is_sync());
assert!(<ImplDetector::<SendNotSync>>::is_send());
assert!(!<ImplDetector::<SendNotSync>>::is_sync());
assert!(!<ImplDetector::<NotSendNotSync>>::is_send());
assert!(!<ImplDetector::<NotSendNotSync>>::is_sync());
const _: () = {
assert!(<ImplDetector::<SendSync>>::IS_SEND);
assert!(<ImplDetector::<SendSync>>::IS_SYNC);
assert!(!<ImplDetector::<SyncNotSend>>::IS_SEND);
assert!(<ImplDetector::<SyncNotSend>>::IS_SYNC);
assert!(<ImplDetector::<SendNotSync>>::IS_SEND);
assert!(!<ImplDetector::<SendNotSync>>::IS_SYNC);
assert!(!<ImplDetector::<NotSendNotSync>>::IS_SEND);
assert!(!<ImplDetector::<NotSendNotSync>>::IS_SYNC);
};
// === Detects `EqualType` ===
assert!(<ImplDetector::<(A, A)>>::is_equal_type());
assert!(!<ImplDetector::<(A, B)>>::is_equal_type());
const _: () = {
assert!(<ImplDetector::<(A, A)>>::IS_EQUAL_TYPE);
assert!(!<ImplDetector::<(A, B)>>::IS_EQUAL_TYPE);
};
}
}