Replace some use of variant with covariant (#322)

pull/324/head
chubei 3 years ago committed by GitHub
parent 358e6a61d5
commit c6b4bf831e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -47,7 +47,7 @@ all the details on variance and drop check.
To fix the first problem, we can use `NonNull<T>`. Note that `NonNull<T>` is a To fix the first problem, we can use `NonNull<T>`. Note that `NonNull<T>` is a
wrapper around a raw pointer that declares that: wrapper around a raw pointer that declares that:
* We are variant over `T` * We are covariant over `T`
* Our pointer is never null * Our pointer is never null
To fix the second problem, we can include a `PhantomData` marker containing an To fix the second problem, we can include a `PhantomData` marker containing an

@ -39,7 +39,7 @@ struct Iter<'a, T: 'a> {
} }
``` ```
and that's it. The lifetime will be bounded, and your iterator will be variant and that's it. The lifetime will be bounded, and your iterator will be covariant
over `'a` and `T`. Everything Just Works. over `'a` and `T`. Everything Just Works.
Another important example is Vec, which is (approximately) defined as follows: Another important example is Vec, which is (approximately) defined as follows:

@ -443,7 +443,7 @@ struct MyType<'a, 'b, A: 'a, B: 'b, C, D, E, F, G, H, In, Out, Mixed> {
f: Vec<F>, // covariant over F f: Vec<F>, // covariant over F
g: Cell<G>, // invariant over G g: Cell<G>, // invariant over G
h1: H, // would also be variant over H except... h1: H, // would also be covariant over H except...
h2: Cell<H>, // invariant over H, because invariance wins all conflicts h2: Cell<H>, // invariant over H, because invariance wins all conflicts
i: fn(In) -> Out, // contravariant over In, covariant over Out i: fn(In) -> Out, // contravariant over In, covariant over Out

Loading…
Cancel
Save