Consider the following type family:
trait Throttler {
type Resource;
}
struct MyThrottler { }
struct Permit<'a> {
t: &'a MyThrottler,
}
impl<'a> Throttler for MyThrottler {
type Resource = Permit<'a>;
}
type AnyThrottler = Box<dyn for<'a> Throttler<Resource = Permit<'a>>>;
I want to express that implementers of Throttler
can return a Permit<'a>
with some lifetime, unknown at the time of declaration.
However, using MyThrottler
as an AnyThrottler
fails with:
let t: AnyThrottler = Box::new(MyThrottler {}) as AnyThrottler;
// Fails with: binding for associated type `Resource` references lifetime `'a`, which does not appear in the trait input types
I've been trying to come up with a workaround based on intermediate traits, but I always get stuck one place or the other. Is this something the type system can support at this time or is it a limitation?