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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//! Various verbose shorthand types related to `PhantomData`.
//!
//! # Variance
//!
//! Variance can be very confusing to beginners. Generally,
//! when one talks about "subtype" and "supertype" in Rust,
//! it is specifically about lifetimes in the current version.
//!
//! Specifically, any reference type `&'any T` is a subtype of
//! the static reference type `&'static T`. A reference with a
//! shorter lifetime is a subtype of another reference with a
//! longer lifetime.
//!
//! ### Covariant
//!
//! When a `Foo<T>` is "covariant" over `T`, it *shares* the
//! same subtyping rules with `T`, i.e. `Foo<&'a T>` is a subtype
//! of `Foo<&'static T>`.
//! When a lifetime parameter is covariant, it suggests that a
//! shorter lifetime parameter is a subtype of a longer lifetime
//! parameter, i.e. `Foo<'a>` is a subtype of `Foo<'static>`.
//!
//! ### Contravariant
//!
//! Contravariant is rare, it *reverses* the normal subtyping
//! rules. `Foo<'static>` is a subtype of `Foo<'a>` if the lifetimes
//! are contravariant. It is the property of argument types of
//! functions. `fn(&'static ())` is the most strict and would be
//! the super type for all `fn(&'a ())`.
//!
//! ### Invariant
//!
//! If a parameter is invariant, it cannot be changed. Only equal
//! arguments are sub/super types of themselves. `T` is invariant
//! in `&mut T` because you can perform both read and write
//! operations on it, and you cannot write a `T2` with a shorter
//! lifetime than `T`, nor read a `T3` with a longer lifetime.
//! `T` is invariant in `fn(T) -> T` for similar reasons.
//!
//! ### Drop Check
//!
//! A difference between the [`CovariantOver`] type offered in this
//! crate and the [`Owns`] type is that `Owns` will cause the compiler
//! to use "drop check". Drop checking prevents use of dropped values
//! in another type's destructor. The examples below are taken and
//! adapted from the [respective Rustonomicon chapter]:
//!
//! Imagine a custom [`Box`](std::boxed::Box) type defined like this:
//!
//! ```
//! use std::ptr::NonNull;
//! struct MyBox<T> {
//! inner: NonNull<T>,
//! }
//!
//! impl<T> Drop for MyBox<T> {
//! fn drop(&mut self) { /* free memory.. */ }
//! }
//! ```
//!
//! Although [`NonNull`](std::ptr::NonNull) is covariant over `T`,
//! `MyBox` must use [`PhantomData`] to signify that it owns `T`,
//! otherwise it could allow access to dropped data in a destructor:
//!
//! ```
//! # use std::ptr::NonNull;
//! # struct MyBox<T> {
//! # inner: NonNull<T>,
//! # }
//! # impl<T> MyBox<T> {
//! # fn new(inner: T) -> Self { Self { inner: NonNull::dangling() } }
//! # }
//! struct Inspector<'a>(&'a u8);
//! impl<'a> Drop for Inspector<'a> {
//! fn drop(&mut self) {
//! println!("I was only {} days from retirement!", self.0);
//! }
//! }
//! struct World<'a> {
//! inspector: Option<MyBox<Inspector<'a>>>,
//! days: Box<u8>,
//! }
//!
//! fn main() {
//! let mut world = World {
//! inspector: None,
//! days: Box::new(1),
//! };
//! world.inspector = Some(MyBox::new(Inspector(&world.days)));
//! // Let's say `days` happens to get dropped first.
//! // Then when Inspector is dropped, it will try to read free'd memory!
//! }
//! ```
//!
//! In this example, marking `MyBox` with a [`PhantomData<T>`] or [`Owns<T>`]
//! resolves the unsoundness issue:
//!
//! ```compile_fail
//! # use std::ptr::NonNull;
//! # use phtm::Owns<T>
//! struct MyBox<T> {
//! inner: NonNull<T>,
//! _owns_t: Owns<T>,
//! }
//! # impl<T> MyBox<T> {
//! # fn new(inner: T) -> Self { Self { inner: NonNull::dangling() } }
//! # }
//! # struct Inspector<'a>(&'a u8);
//! # impl<'a> Drop for Inspector<'a> {
//! # fn drop(&mut self) {
//! # println!("I was only {} days from retirement!", self.0);
//! # }
//! # }
//! # struct World<'a> {
//! # inspector: Option<MyBox<Inspector<'a>>>,
//! # days: Box<u8>,
//! # }
//!
//! fn main() {
//! let mut world = World {
//! inspector: None,
//! days: Box::new(1),
//! };
//!
//! world.inspector = Some(MyBox::new(Inspector(&world.days)));
//! // ^ now fails to compile!
//! }
//! ```
//!
//! [respective Rustonomicon chapter]: https://doc.rust-lang.org/nomicon/dropck.html
//!
//! # Markers
//!
//! The [`Send`] and [`Sync`] markers can be controlled by adding
//! [`NotSendOrSync`] or [`NotSync`] to your type and/or writing
//! `unsafe impl`s of the markers to your type. The markers control
//! what types can be sent between threads safely. The non-atomic
//! reference counted type [`Rc`](std::rc::Rc) cannot be sent
//! between threads because there might be two threads holding the
//! same object and could increase the reference counter
//! non-atomically, causing a data race.
//!
//! Usually your type is automatically not [`Send`] when there are
//! fields of pointers or single-threaded containers such as `Rc`,
//! but there could be benefits of explicitly defining a type as not
//! [`Send`] or not [`Sync`].
//!
//! Doing so can prevent semver hazards when a public type suddenly
//! stops implementing any of these marker types, causing a breaking
//! change, while explicitly adding the marker types allows future
//! possibilities of having single-threaded containers without
//! bumping the major version.
// intra doc links need std
extern crate core;
use Cell;
pub use PhantomData;
pub use PhantomPinned;
/// Verbose version of `PhantomData`.
///
/// It is covariant over `T` with drop checking.
///
/// See the [crate root documentation] for details on variance
/// and drop checking.
///
/// [crate root documentation]: index.html
pub type Owns<T> = ;
/// Alias for phantom `&'a T`. Both parameters are covariant.
///
/// If you do not have a lifetime handy, you could use
/// [`HasImmPtrTo`].
///
/// See the [crate root documentation] for details on variance.
///
/// [crate root documentation]: index.html
pub type ImmutablyReferences<'a, T> = ;
/// Alias for `PhantomData<&'a mut T>`. `'a` is covariant
/// while `T` is invariant.
///
/// If you do not have a lifetime handy, you could use
/// [`HasMutPtrTo`].
///
/// See the [crate root documentation] for details on variance.
///
/// [crate root documentation]: index.html
pub type MutablyReferences<'a, T> = ;
/// Marks the containing type as having mutable pointers to `T`.
///
/// `T` is covariant, it also marks the containing type as not
/// [`Send`] and not [`Sync`].
///
/// See the [crate root documentation] for details on variance
/// and marker traits.
///
/// [crate root documentation]: index.html
pub type HasImmPtrTo<T> = ;
/// Marks the containing type as having mutable pointers to `T`.
///
/// `T` is invariant, it also marks the containing type as not
/// [`Send`] and not [`Sync`].
///
/// See the [crate root documentation] for details on variance
/// and marker traits.
///
/// [crate root documentation]: index.html
pub type HasMutPtrTo<T> = ;
/// Mark a type as covariant.
///
/// This marker does not "own" the type, i.e. `T` does not get
/// dropped when the containing type is dropped. If `T` could
/// be dropped, use [`Owns`] instead.
///
/// See the [crate root documentation] for details on variance.
///
/// [crate root documentation]: index.html
pub type CovariantOver<T> = ;
/// Marks a lifetime as covariant.
///
/// See the [crate root documentation] for details on variance.
///
/// [crate root documentation]: index.html
pub type CovariantOverLt<'co> = ;
/// Marks a type as contravariant.
///
/// See the [crate root documentation] for details on variance.
///
/// [crate root documentation]: index.html
pub type ContravariantOver<T> = ;
/// Marks a lifetime as contravariant.
///
/// See the [crate root documentation] for details on variance.
///
/// [crate root documentation]: index.html
pub type ContraVariantOverLt<'contra> = ;
/// Marks a type as invariant.
///
/// See the [crate root documentation] for details on variance.
///
/// [crate root documentation]: index.html
pub type InvariantOver<T> = ;
/// Marks a lifetime as invariant.
///
/// See the [crate root documentation] for details on variance.
///
/// [crate root documentation]: index.html
pub type InvariantOverLt<'inv> = ;
/// Marks the containing type as not [`Sync`].
///
/// See the [crate root documentation] for details on marker
/// traits.
///
/// [crate root documentation]: index.html
pub type NotSync = ;
/// Marks the containing type as not [`Send`] and not [`Sync`].
///
/// See the [crate root documentation] for details on marker
/// traits.
///
/// [crate root documentation]: index.html
pub type NotSendOrSync = ;