Sub topic 11
Why GATs need where Self: 'a
This chapter assumes you can write a lending iterator with a lifetime GAT, as in the previous chapter, and that you know lifetime bounds such as T: 'a. It explains the where Self: 'a clause that the compiler requires on that GAT.
The clause in context
Here is the lending iterator again. The where Self: 'a clause appears in the trait and in the implementation.
trait LendingIterator {
type Item<'a>
where
Self: 'a;
fn next<'a>(&'a mut self) -> Option<Self::Item<'a>>;
}
struct WindowsMut<'t, T> {
slice: &'t mut [T],
start: usize,
size: usize,
}
impl<'t, T> LendingIterator for WindowsMut<'t, T> {
type Item<'a> = &'a mut [T] where Self: 'a;
fn next<'a>(&'a mut self) -> Option<Self::Item<'a>> {
let end = self.start + self.size;
if end > self.slice.len() {
return None;
}
let window = &mut self.slice[self.start..end];
self.start += 1;
Some(window)
}
}
fn main() {
let mut data = [1, 2, 3, 4];
let mut windows = WindowsMut { slice: &mut data, start: 0, size: 2 };
while let Some(w) = windows.next() {
w[1] += w[0];
}
println!("{:?}", data);
}The program prints [1, 3, 6, 10].
What the clause says
where Self: 'a means the implementing type must be valid for at least the lifetime 'a. In other words, Self outlives 'a. A GAT Item<'a> is only defined for lifetimes 'a that Self outlives.
Why the compiler demands it
Look at the type &'a mut [T] in the implementation. For this type to be well-formed, [T] must be valid for 'a, so T: 'a must hold. That follows from Self: 'a, because WindowsMut<'t, T> can only outlive 'a if 't and T do.
Now look at the trait declaration. Inside next, the argument &'a mut self implies Self: 'a. A bare declaration like type Item<'a>; has no such implied bound. Without the clause, nothing would stop a caller from writing Self::Item<'a> for a lifetime 'a that is longer than Self. Implementations such as &'a mut [T] could not be valid for such an 'a.
The compiler therefore closes the gap at the declaration. If a GAT with a lifetime parameter appears in a method signature that takes &'a self or &'a mut self, the compiler requires the matching bound. Otherwise it reports an error such as missing required bound on 'Item', and the help text suggests adding where Self: 'a.
Practical consequences
- Add the clause to every lifetime GAT that a method returns from a borrowed
self. The compiler usually tells you when you forget it. - Repeat the clause in each implementation, as the code above does with
type Item<'a> = &'a mut [T] where Self: 'a;. - In generic code, the clause is a requirement you inherit. A function that calls
Self::Item<'a>must be able to show thatSelf: 'a. This holds automatically when you have a&'a mut Ifor the same'a.
Type-parameter GATs
The clause is about lifetimes. A GAT with only type parameters, such as type Pointer<T> in a pointer family, needs no where Self: 'a, because no lifetime is introduced.
Exercise
A trait declares type Item<'a>; and a method fn next<'a>(&'a mut self) -> Option<Self::Item<'a>>;. The compiler reports "missing required bound on Item". What change fixes it, and why does an implementation like type Item<'a> = &'a mut [T] need it?
Show answer
Change the declaration to type Item<'a> where Self: 'a;. The type &'a mut [T] is only well-formed if T: 'a, which follows from Self: 'a. The trait declaration has no implied bound, so it must state Self: 'a to rule out lifetimes longer than Self.
Exercise
Does type Pointer<T>; in a PointerFamily trait need a where Self: 'a clause? Explain.
Show answer
No. The GAT has a type parameter and no lifetime parameter, so no lifetime 'a exists for Self to outlive.