@@ -10,6 +10,7 @@ while also preventing their misuse, Rust uses **subtyping** and **variance**.
10
10
Let's start with an example.
11
11
12
12
``` rust
13
+ // Note: debug expects two parameters with the *same* lifetime
13
14
fn debug <'a >(a : & 'a str , b : & 'a str ) {
14
15
println! (" a = {:?} b = {:?}" , a , b );
15
16
}
@@ -45,38 +46,18 @@ Let's try using subtyping with our lifetimes.
45
46
46
47
Subtyping is the idea that one type can be used in place of another.
47
48
48
- Let's define that ` Sub ` is a subtype of ` Super ` (we'll be using the notation ` Sub: Super ` throughout this chapter).
49
+ Let's define that ` Sub ` is a subtype of ` Super ` (we'll be using the notation ` Sub < : Super ` throughout this chapter).
49
50
50
51
What this is suggesting to us is that the set of * requirements* that ` Super ` defines
51
52
are completely satisfied by ` Sub ` . ` Sub ` may then have more requirements.
52
53
53
- An example of simple subtyping that exists in the language is [ supertraits] [ supertraits ] :
54
-
55
- [ supertraits ] : https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#using-supertraits-to-require-one-traits-functionality-within-another-trait
56
-
57
- ``` rust
58
- use std :: fmt;
59
-
60
- pub trait Error : fmt :: Display {
61
- fn source (& self ) -> Option <& (dyn Error + 'static )>;
62
- fn description (& self ) -> & str ;
63
- fn cause (& self ) -> Option <& dyn Error >;
64
- }
65
- ```
66
-
67
- Here, we have that ` Error: fmt::Display ` (` Error ` is a * subtype* of ` Display ` ),
68
- because it has all the requirements of ` fmt::Display ` , plus the ` source ` /` description ` /` cause ` functions.
69
-
70
- However, subtyping in traits is not that interesting.
71
- Here in the Rustonomicon, we're going to focus more with how subtyping interacts with lifetimes.
72
-
73
- Let's define a lifetime to be the simple requirement:
54
+ Now, in order to use subtyping with lifetimes, we need to define the requirement of a lifetime:
74
55
75
56
> ` 'a ` defines a region of code.
76
57
77
58
Now that we have a defined set of requirements for lifetimes, we can define how they relate to each other:
78
59
79
- > ` 'long : 'short ` if and only if ` 'long ` defines a region of code that ** completely contains** ` 'short ` .
60
+ > ` 'long < : 'short ` if and only if ` 'long ` defines a region of code that ** completely contains** ` 'short ` .
80
61
81
62
` 'long ` may define a region larger than ` 'short ` , but that still fits our definition.
82
63
@@ -88,7 +69,7 @@ And unless you write unsafe code, the compiler will automatically handle all the
88
69
> But this is the Rustonomicon. We're writing unsafe code,
89
70
so we need to understand how this stuff really works, and how we can mess it up.
90
71
91
- Going back to our example above, we can say that ` 'static : 'world ` .
72
+ Going back to our example above, we can say that ` 'static < : 'world ` .
92
73
For now, let's also accept the idea that subtypes of lifetimes can be passed through references
93
74
(more on this in [ Variance] ( #variance ) ),
94
75
_ e.g._ ` &'static str ` is a subtype of ` &'world str ` , then we can let a ` &'static str ` "downgrade" into a ` &'world str ` .
@@ -111,7 +92,7 @@ fn main() {
111
92
112
93
## Variance
113
94
114
- Above, we glossed over the fact that ` 'static : 'b ` implied that ` &'static T : &'b T ` . This uses a property known as _ variance_ .
95
+ Above, we glossed over the fact that ` 'static < : 'b ` implied that ` &'static T < : &'b T ` . This uses a property known as _ variance_ .
115
96
It's not always as simple as this example, though. To understand that, let's try to extend this example a bit:
116
97
117
98
``` rust,compile_fail
@@ -141,43 +122,39 @@ The problem is that we cannot assume that `&mut &'static str` and `&mut &'b str`
141
122
This means that ` &mut &'static str ` ** cannot** be a * subtype* of ` &mut &'b str ` ,
142
123
even if ` 'static ` is a subtype of ` 'b ` .
143
124
144
- Variance is the concept that Rust borrows to define relationships about subtypes through their * type constructor* s.
145
- A type constructor is any generic item in Rust.
146
- For instance ` Vec ` is a type constructor that takes a type ` T ` and returns
147
- ` Vec<T> ` . ` & ` and ` &mut ` are type constructors that take two inputs: a
148
- lifetime, and a type to point to.
125
+ Variance is the concept that Rust borrows to define relationships about subtypes through their generic parameters.
149
126
150
- > NOTE: For convenience we will often refer to ` F<T> ` as a type constructor just so
127
+ > NOTE: For convenience we will define a generic type ` F<T> ` so
151
128
> that we can easily talk about ` T ` . Hopefully this is clear in context.
152
129
153
- A type constructor F 's * variance* is how the subtyping of its inputs affects the
130
+ The type ` F ` 's * variance* is how the subtyping of its inputs affects the
154
131
subtyping of its outputs. There are three kinds of variance in Rust. Given two
155
132
types ` Sub ` and ` Super ` , where ` Sub ` is a subtype of ` Super ` :
156
133
157
- * F is ** covariant** if ` F<Sub> ` is a subtype of ` F<Super> ` (the subtype property is passed through)
158
- * F is ** contravariant** if ` F<Super> ` is a subtype of ` F<Sub> ` (the subtype property is "inverted")
159
- * F is ** invariant** otherwise (no subtyping relationship exists)
134
+ * ` F ` is ** covariant** if ` F<Sub> ` is a subtype of ` F<Super> ` (the subtype property is passed through)
135
+ * ` F ` is ** contravariant** if ` F<Super> ` is a subtype of ` F<Sub> ` (the subtype property is "inverted")
136
+ * ` F ` is ** invariant** otherwise (no subtyping relationship exists)
160
137
161
138
If we remember from the above examples,
162
- it was ok for us to treat ` &'a T ` as a subtype of ` &'b T ` if ` 'a: 'b ` ,
139
+ it was ok for us to treat ` &'a T ` as a subtype of ` &'b T ` if ` 'a < : 'b ` ,
163
140
therefore we can say that ` &'a T ` is * covariant* over ` 'a ` .
164
141
165
- Also, we saw that it was not ok for us to treat ` &mut &'a T ` as a subtype of ` &mut &'b T ` ,
142
+ Also, we saw that it was not ok for us to treat ` &mut &'a U ` as a subtype of ` &mut &'b U ` ,
166
143
therefore we can say that ` &mut T ` is * invariant* over ` T `
167
144
168
- Here is a table of some other type constructors and their variances:
145
+ Here is a table of some other generic types and their variances:
169
146
170
- | | | 'a | T | U |
171
- | ---| --- --------------| :---------:| :-----------------:| :---------:|
172
- | | ` &'a T ` | covariant | covariant | |
173
- | | ` &'a mut T ` | covariant | invariant | |
174
- | | ` Box<T> ` | | covariant | |
175
- | | ` Vec<T> ` | | covariant | |
176
- | | ` UnsafeCell<T> ` | | invariant | |
177
- | | ` Cell<T> ` | | invariant | |
178
- | | ` fn(T) -> U ` | | ** contra** variant | covariant |
179
- | | ` *const T ` | | covariant | |
180
- | | ` *mut T ` | | invariant | |
147
+ | | 'a | T | U |
148
+ | -----------------| :---------:| :-----------------:| :---------:|
149
+ | ` &'a T ` | covariant | covariant | |
150
+ | ` &'a mut T ` | covariant | invariant | |
151
+ | ` Box<T> ` | | covariant | |
152
+ | ` Vec<T> ` | | covariant | |
153
+ | ` UnsafeCell<T> ` | | invariant | |
154
+ | ` Cell<T> ` | | invariant | |
155
+ | ` fn(T) -> U ` | | ** contra** variant | covariant |
156
+ | ` *const T ` | | covariant | |
157
+ | ` *mut T ` | | invariant | |
181
158
182
159
Some of these can be explained simply in relation to the others:
183
160
@@ -260,7 +237,7 @@ fn debug<T: std::fmt::Debug>(a: T, b: T) {
260
237
where similarly ` a ` and ` b ` must have the same type ` T ` .
261
238
But since ` &'a T ` * is* covariant over ` 'a ` , we are allowed to perform subtyping.
262
239
So the compiler decides that ` &'static str ` can become ` &'b str ` if and only if
263
- ` &'static str ` is a subtype of ` &'b str ` , which will hold if ` 'static: 'b ` .
240
+ ` &'static str ` is a subtype of ` &'b str ` , which will hold if ` 'static < : 'b ` .
264
241
This is true, so the compiler is happy to continue compiling this code.
265
242
266
243
As it turns out, the argument for why it's ok for Box (and Vec, HashMap, etc.) to be covariant is pretty similar to the argument for why it's ok for lifetimes to be covariant: as soon as you try to stuff them in something like a mutable reference, they inherit invariance and you're prevented from doing anything bad.
0 commit comments