pub unsafe trait Cov: Ctor {
// Required method
fn cov<'r, 'a, 'b>(r: &'r Self::Ty<'a>) -> &'r Self::Ty<'b>
where 'a: 'b;
}
Expand description
Trait for type constructors that produce types whose references are covariant over the lifetime parameter.
For most valid types, this could implemented by simply invoking cov!
:
struct FooCtor;
struct Foo<'a>(&'a u8);
impl Ctor for FooCtor {
type Ty<'a> = Foo<'a>;
}
cov!(FooCtor);
Safety
References to types produced by this type constructor must be covariant over the lifetime parameter.
This trait is unsafe
for the reason that it is trivial to write a type-checking
implementation of cov
for every type out there, simply by writing panic!()
.
Lifetime variance is hard to figure out in a complex type. It’s safest to use the cov!
macro for applicable types, unless you are really sure that you know better than the compiler.