I've defined the following traits:
trait Param {
type State: 'static; // The 'static bound on State is not relevant to this question.
type Worlds<'w>: FromIds<'w>;
fn init<'w>(worlds: Self::Worlds<'w>) -> Self::State;
}
and
trait FromIds<'w>: 'w {
type Ids: 'static;
fn from_ids(worlds: &'w Worlds, ids: &Self::Ids) -> Self;
}
My intention is to fetch World
s from Worlds
based on Ids
, where Ids
represents a nested tuple of WorldId
.
Param::Worlds
is structured as a nested tuple with the same hierarchy as that of the WorldId
nested tuple.
I want to store Ids
in a struct:
struct System<P: Param> {
state: Option<P::State>,
// I believe that ideally the 'static lifetime wouldn't need to be explicitly specified here,
// but without it, the compiler complains.
ids: <P::Worlds<'static> as FromIds<'static>>::Ids,
}
Then, I attempted to implement an initialization function for System
:
impl<P: Param> System<P> {
fn init<'w>(&mut self, worlds: &'w Worlds) {
let worlds = <P::Worlds<'w> as FromIds<'w>>::from_ids(worlds, &self.ids); // ERROR: mismatched types - expected P::Worlds<'w>::Ids but found P::Worlds<'static>::Ids
self.state = Some(P::init(worlds));
}
}
Given that Ids
is bounded by 'static
, I expected this code to work. However, I encounter a type error due to the mismatch between P::Worlds<'w>
and P::Worlds<'static>
.
I also tried approaches such as implementing a shrink
function within FromIds
or removing the lifetime parameter from it, but none of these attempts succeeded.
How can I resolve the type mismatch in this scenario? Any help or suggestions would be greatly appreciated.