@@ -61,25 +61,51 @@ Variance of types is automatically determined as follows
61
61
| ` [T] ` and ` [T; n] ` | | covariant |
62
62
| ` fn() -> T ` | | covariant |
63
63
| ` fn(T) -> () ` | | contravariant |
64
- | ` fn(T) -> T ` | | invariant |
65
64
| ` std::cell::UnsafeCell<T> ` | | invariant |
66
65
| ` std::marker::PhantomData<T> ` | | covariant |
67
66
| ` dyn Trait<T> + 'a ` | covariant | invariant |
68
67
69
- The variance of other ` struct ` , ` enum ` , ` union ` , and tuple types is decided by
68
+ The variance of other ` struct ` , ` enum ` , and ` union ` types is decided by
70
69
looking at the variance of the types of their fields. If the parameter is used
71
70
in positions with different variances then the parameter is invariant. For
72
- example the following struct is covariant in ` 'a ` and ` T ` and invariant in ` 'b `
71
+ example the following struct is covariant in ` 'a ` and ` T ` and invariant in ` 'b ` , ` 'c ` ,
73
72
and ` U ` .
74
73
75
74
``` rust
76
75
use std :: cell :: UnsafeCell ;
77
- struct Variance <'a , 'b , T , U : 'a > {
76
+ struct Variance <'a , 'b , ' c , T , U : 'a > {
78
77
x : & 'a U , // This makes `Variance` covariant in 'a, and would
79
78
// make it covariant in U, but U is used later
80
79
y : * const T , // Covariant in T
81
80
z : UnsafeCell <& 'b f64 >, // Invariant in 'b
82
81
w : * mut U , // Invariant in U, makes the whole struct invariant
82
+
83
+ f : fn (& 'c ()) -> & 'c () // Both co- and contravariant, makes 'c invariant
84
+ // in the struct.
85
+ }
86
+ ```
87
+
88
+ When used outside of an ` struct ` , ` enum ` , or ` union ` , the variance for parameters is checked at each location separately.
89
+
90
+ ``` rust
91
+ # use std :: cell :: UnsafeCell ;
92
+ fn generic_tuple <'short , 'long : 'short >(
93
+ // 'long is used inside of a tuple in both a co- and invariant position.
94
+ x : (& 'long u32 , UnsafeCell <& 'long u32 >),
95
+ ) {
96
+ // As the variance at these positions is computed separately,
97
+ // we can freely shrink 'long in the covariant position.
98
+ let _ : (& 'short u32 , UnsafeCell <& 'long u32 >) = x ;
99
+ }
100
+
101
+ fn takes_fn_ptr <'short , 'middle : 'short >(
102
+ // 'middle is used in both a co- and contravariant position.
103
+ f : fn (& 'middle ()) -> & 'middle (),
104
+ ) {
105
+ // As the variance at these positions is computed separately,
106
+ // we can freely shrink 'middle in the covariant position
107
+ // and extend it in the contravariant position.
108
+ let _ : fn (& 'static ()) -> & 'short () = f ;
83
109
}
84
110
```
85
111
0 commit comments