A covariant subtype gets a different type-ID, but can be converted freely from one to the other. This means that taking a type-ID of a generic type argument in a type's new method is not guaranteed to give the same type-ID that the type will eventually be called with. The solution is to force the generic type argument to be invariant using PhantomData. See this bug in qcell crate.
It would be helpful if this were documented to avoid similar bugs occurring in other crates.
use std::any::TypeId;
fn main() {
println!(
"{:?}, {:?}",
TypeId::of::<fn(&'_ ())>(),
TypeId::of::<fn(&'static ())>(),
);
}
A covariant subtype gets a different type-ID, but can be converted freely from one to the other. This means that taking a type-ID of a generic type argument in a type's
newmethod is not guaranteed to give the same type-ID that the type will eventually be called with. The solution is to force the generic type argument to be invariant using PhantomData. See this bug inqcellcrate.It would be helpful if this were documented to avoid similar bugs occurring in other crates.