Hi Often times, I have the following pattern in code:
// A struct which owns some data and some references
struct Analysis<'f> {
cache: HashMap<&'f str, u64>
}
// A struct, which borrows Analysis
struct Codegen<'a, 'f: 'a> { // Note: I have to declare two lifetimes here
analysis: &'a Analysis<'f>, // This is what bothers me!
node_types: Vec<(Text<'f>, bool)>,
}
That is, I want to store a field of type Foo, which also has a lifetime. So the field has two lifeimes: &'a Foo<'b>, and I have to declare both lifetimes for the containing struct. Is it possible some how to parameterize the struct over a single lifetime instead? I would love something like this:
If Analysis and Text are variant, you should be able to use a single lifetime for all those lifetimes there. I think the multiple lifetimes (with explicit outlives relationship specified) are needed if the types are invariant.
Yeah, a trait object can be a suitable technique here. You can also make the type generic over the trait and avoid the virtual dispatch overhead. This does add a generic type parameter, but it hides the lifetime parameter(s) of the underlying type:
Type inference typically makes the use sites fairly concise since the exact type are inferred, but that's also the case with generic lifetime parameters. So I don't know if you think this is an improvement or not.
You could go even further and abstract over an owned or borrowed Foo, which adds another type parameter to Context but removes the lifetime parameter.