Sub topic 8
PhantomData: choosing the variance you want
This chapter assumes you know ownership and borrowing, basic lifetime annotations, generics with trait bounds, and the variance rules from the previous chapters: &'a T is covariant in 'a, and &mut T and Cell<T> are invariant in T.
Why you need PhantomData
Rust rejects a struct that declares a lifetime or type parameter and never uses it. Suppose you write a handle that holds a raw pointer, which carries no lifetime, but logically borrows data for 'a. The compiler reports error E0392, "parameter 'a is never used".
PhantomData<T> is a zero-sized type. It occupies no memory, but it tells the compiler to treat the struct as if it contained a T. That does two things. It satisfies the "unused parameter" check, and it sets the struct's variance, because variance is computed from field types and PhantomData<T> is covariant in T.
Picking the marker type
The type argument of PhantomData is the lever. Choose a type that has the variance you want:
| Marker | Variance in `'a` |
|---|---|
| `PhantomData<&'a ()>` | covariant |
| `PhantomData<Cell<&'a ()>>` | invariant |
| `PhantomData<fn(&'a ())>` | contravariant |
The first two rows are demonstrated below. The fn(&'a ()) row works because function arguments run the other way, as covered in the contravariance chapter.
use std::cell::Cell;
use std::marker::PhantomData;
struct Co<'a>(PhantomData<&'a ()>);
struct Inv<'a>(PhantomData<Cell<&'a ()>>);
fn shrink_co<'long: 'short, 'short>(x: Co<'long>) -> Co<'short> {
x
}
// Does not compile: Inv<'a> is invariant in 'a.
// fn shrink_inv<'long: 'short, 'short>(x: Inv<'long>) -> Inv<'short> {
// x
// }Co<'long> can be used where Co<'short> is expected, because 'long: 'short and the struct is covariant. Inv<'long> cannot, because Cell<&'a ()> is invariant in 'a. The marker changes only how the compiler checks your types. At runtime, both structs are zero-sized.
Choosing deliberately
Covariance is the default choice, and it is usually correct for types that only read borrowed data. Pick invariance when the type could write a shorter-lived reference into a place that others see as longer-lived. This is the same reason &mut &'a str is invariant: if it could shrink, a caller could store a short borrow in a slot that is expected to hold a long one, leaving a dangling reference.
Variance is not ownership
PhantomData<T> also tells the drop checker that the struct may own a T. If your type holds a raw pointer *const T and owns the pointed-to value, use PhantomData<T>. If it only borrows, use PhantomData<&'a T>. A bare *const T is covariant in T but says nothing about ownership. A *mut T is invariant in T. Choose the marker to match what the type really does, not only the variance you want.
Exercise
A struct Slot<'a> wraps a raw pointer and lets callers write a &'a str through it. Which PhantomData marker should it hold so that a Slot<'static> cannot be passed where a Slot<'short> is expected, and why?
Show answer
Use PhantomData<Cell<&'a str>> (or PhantomData<&'a mut &'a str>-style invariant markers). Cell<&'a str> is invariant in 'a, so Slot<'static> cannot shrink to Slot<'short>. If it could, a caller could write a short-lived &str into a slot that other code still treats as holding a 'static string, which would leave a dangling reference.