Make box covariant by ElchananHaas · Pull Request #304 · fitzgen/bumpalo (original) (raw)

This CR makes the box covariant in 'a to allow for more use cases. This addresses #170.

Making it covariant should be OK for the same reasons as the standard library box is safe. Quoting the RustNomicon (https://doc.rust-lang.org/nomicon/subtyping.html)

As it turns out, the argument for why it's ok for Box (and Vec, HashMap, etc.) to be covariant is pretty similar to the argument for why it's ok for lifetimes to be covariant: as soon as you try to stuff them in something like a mutable reference, they inherit invariance and you're prevented from doing anything bad.

The Bumpalo Vec is already covariant.

I tested this code by running

cargo test --features boxed 

I checked that all tests passed. I also added a unit test for covariance. I wasn't able to come up with a great test, but I verified that the test fails if I make the lifetime invariant.

I additionally ran

cargo miri test --features boxed

miri passed all tests. It complains about some memory leaks in raw_vec, but that should be unrelated.