I'm trying to make a list that will hold Box<dyn Fn(&E)> where E is specified as part of the type. This works until E contains a reference, at which point it starts asking for lifetimes that aren't relevant.
A simpler example:
pub struct CallbackTest<E> {
pub cb: Box<dyn Fn(&E)>,
}
impl<E> CallbackTest<E> {
pub fn new<F>(cb: F) -> Self
where
F: Fn(&E)
{
Self { cb: Box::new(cb) }
}
}
pub struct GameData { /* ... */ }
pub type TestRef = CallbackTest<(u32, &GameData)>;
This gives me a missing lifetime specifier error. I could put a lifetime parameter on TestRef to make it work, but that's not the correct lifetime. I don't want the &GameData to have to live for the entire lifetime of the CallbackTest, just during the function call.
EDIT: The &GameData is intentional. It's not a mistake. I hope my changes have made the goal behind this more obvious.
Any advice?
u32. In this situation, the compiler is right to ask for a lifetime.&u32was intentional. I've changed it to be a bit more explicit that I'm trying to pass a tuple with multiple values, one of which is a reference.&u32to have to live for the entire lifetime of theCallbackTest, just during the function call. Does @ÖmerErden's suggestion work for you?