Sub topic 12
GATs with type parameters: pointer families
This chapter assumes you can write traits with plain associated types and read where clauses. It also assumes you know what Rc<T> and Box<T> do. Earlier chapters used GATs with a lifetime parameter. Here the GAT takes a type parameter.
The problem: abstracting over a type constructor
Rc and Box are not types. Rc<i32> is a type, and Rc alone is a type constructor: give it a T and it produces a type. A plain associated type can name one finished type, such as Rc<i32>. It cannot name "Rc, for any T you choose later".
Suppose you want a struct that works with either pointer without being written twice. You need a trait that stands for the whole constructor. A GAT with a type parameter does this.
A pointer family trait
use std::rc::Rc;
trait PointerFamily {
type Pointer<T>;
fn new<T>(value: T) -> Self::Pointer<T>;
}
struct RcFamily;
struct BoxFamily;
impl PointerFamily for RcFamily {
type Pointer<T> = Rc<T>;
fn new<T>(value: T) -> Rc<T> {
Rc::new(value)
}
}
impl PointerFamily for BoxFamily {
type Pointer<T> = Box<T>;
fn new<T>(value: T) -> Box<T> {
Box::new(value)
}
}
fn make<P: PointerFamily>(value: i32) -> P::Pointer<i32> {
P::new(value)
}
fn main() {
let a = make::<RcFamily>(5);
let b = make::<BoxFamily>(6);
println!("{} {}", *a, *b);
}The program prints 5 6.
type Pointer<T>; declares a family of types, one for each T. RcFamily and BoxFamily are empty marker structs. They exist only to carry the choice of family. Each implementation fills in the right-hand side: Rc<T> or Box<T>.
Inside make, P::Pointer<i32> is resolved once the caller picks P. make never mentions Rc or Box.
How the pieces connect
Adding bounds to the GAT
The generic code above can only create pointers. It cannot read through them, because the compiler knows nothing about P::Pointer<T>. You can add a bound directly on the GAT:
use std::ops::Deref;
use std::rc::Rc;
trait PointerFamily {
type Pointer<T>: Deref<Target = T>;
fn new<T>(value: T) -> Self::Pointer<T>;
}
struct RcFamily;
struct BoxFamily;
impl PointerFamily for RcFamily {
type Pointer<T> = Rc<T>;
fn new<T>(value: T) -> Rc<T> {
Rc::new(value)
}
}
impl PointerFamily for BoxFamily {
type Pointer<T> = Box<T>;
fn new<T>(value: T) -> Box<T> {
Box::new(value)
}
}
struct Pair<P: PointerFamily> {
left: P::Pointer<i32>,
right: P::Pointer<String>,
}
impl<P: PointerFamily> Pair<P> {
fn new(left: i32, right: &str) -> Self {
Pair {
left: P::new(left),
right: P::new(right.to_string()),
}
}
fn describe(&self) -> String {
format!("{} and {}", *self.left, *self.right)
}
}
fn main() {
println!("{}", Pair::<RcFamily>::new(1, "one").describe());
println!("{}", Pair::<BoxFamily>::new(2, "two").describe());
}The bound Deref<Target = T> applies to every instantiation of Pointer<T>. The compiler checks it for each implementation. That check is why *self.left compiles in describe. The program prints 1 and one and then 2 and two.
Pair<P> uses the same family for both fields, with different element types. A plain associated type could not do this, because it would fix one element type for the whole family.
Common errors
- Forgetting the parameter when you use it. Writing
P::Pointerinstead ofP::Pointer<i32>is an error. A GAT always needs its arguments. - Missing a bound the generic code needs. Without
Deref<Target = T>,*self.leftfails. Put the bound on the GAT, not on each function. - Trying to implement a family for a type with the wrong shape. Each
Pointer<T>must be valid for everyT. If the right-hand side needsT: Clone, the impl is rejected unless the trait also requires it.
Exercise
In the second example, you add type Pointer<T>: Deref<Target = T>; to the trait. Why can describe use *self.right on a P::Pointer<String> without knowing whether P is RcFamily or BoxFamily?
Show answer
The bound is part of the trait declaration, so the compiler assumes it holds for every P: PointerFamily and every T. It checks the bound once per implementation (Rc<T> and Box<T> both implement Deref<Target = T>). Generic code can therefore dereference the pointer without naming the concrete type.