I will gladly explain the approach for life, but it does not seem viable.
When is X subtype of Y (denoted by X <: Y )?
The question asked for generics includes variance .
Dispersion answers the question: for the generic type G<X> value X <: Y means the ratio of G<X> to G<Y> .
- Covariance:
X <: Y => G<X> <: G<Y> - Invariance:
X == Y => G<X> <: G<Y> - Contravariance:
X <: Y => G<Y> <: G<X>
Cell<X> is invariant wrt X , therefore phantom: PhantomData<Cell<&'a Parent>>, makes Child<'a> invariant wrt 'a .
PhantomData is a way to trick you into talking about variance by simply describing it in the types that you already know.
This works, but not so fast, because we can create a situation where the lifetimes are completely equal, and then the test is compiled again!
let (parent, parent2) = (Parent::new(1), Parent::new(1)); let (child, child2) = (parent.child(2), parent2.child(2));
bluss source share