Sub topic 6
Contravariance: function arguments run the other way
This chapter assumes you know covariance and invariance from the previous chapters, and that you can read 'long: 'short as "'long outlives 'short".
The idea
Covariance keeps the direction of subtyping. If 'long: 'short, then &'long T is a subtype of &'short T. Contravariance reverses it. A type F<'a> is contravariant in 'a when F<'short> is a subtype of F<'long>.
Function arguments are the standard case. Take a function that accepts any &'short str. It can also be given a &'long str, because the longer borrow shrinks to fit. So a function that accepts the short lifetime can stand in for one that only needs to accept the long lifetime.
The table shows how fn(A) -> R behaves.
| Position | Variance |
|---|---|
| Argument type `A` | Contravariant |
| Return type `R` | Covariant |
| Argument of an argument, `fn(fn(A))` | Covariant (two flips) |
Seeing it compile
PhantomData<fn(&'a ())> makes a type contravariant in 'a. The type holds no data, so only its variance matters.
use std::marker::PhantomData;
struct Sink<'a>(PhantomData<fn(&'a ())>);
fn widen<'long: 'short, 'short>(x: Sink<'short>) -> Sink<'long> {
x
}
// Does not compile: Sink is contravariant, so it cannot move
// from the long lifetime to the short one.
// fn narrow<'long: 'short, 'short>(x: Sink<'long>) -> Sink<'short> {
// x
// }
fn main() {
let sink: Sink<'static> = Sink(PhantomData);
let _same = widen::<'static, 'static>(sink);
println!("widened");
}Compare this with Co<'a> from the covariance chapter. widen converts Sink<'short> into Sink<'long>, which is the opposite of what covariance allows. The commented-out narrow is the direction that covariant types accept and that Sink rejects.
Why the direction is sound
Suppose a Sink<'short> could be turned into a Sink<'long>. This is safe because the sink only consumes values. Anything it receives is a 'long borrow, which also satisfies 'short. The sink never hands out a reference, so it cannot return one that outlives its source.
The reverse conversion would be unsound. A Sink<'long> expects every input to live for 'long. If you passed it as a Sink<'short>, a caller could give it a short borrow that the sink then treats as long.
Contravariance is rare
Most types you write are covariant or invariant. Contravariance appears mainly with fn pointers, and in Rust it only ever applies to argument types. Trait objects like dyn Fn(&'a T) and generic parameters on traits are invariant, so closures in a Box do not behave like bare fn pointers.
If a type uses T in both an argument position and a return position, as in fn(T) -> T, the two directions conflict. The result is invariant in T.
Exercise
What is the variance of fn(&'a str) -> &'a str in 'a when it is written as a field of a struct with parameter 'a (that is, struct S<'a> { f: fn(&'a str) -> &'a str })? Explain why.
Show answer
Invariant. 'a appears in an argument position, which is contravariant, and in the return type, which is covariant. The two requirements conflict, so the compiler picks invariance.