Hello,
this is my first time posting here with a problem I am not able to solve myself and I really hope you can help me.
I have minimized the code as good as I could. My basic idea behind this is that I have a GUIHandler struct that keeps a vec with all GUI trait objects. When I create the first MainGUI everything works. But when I want to add a GUI in the InputHandlers test() function the lifetime mismatch error pops up. I want to create the GUI in the test() function, because I would like to open a GUI when I press a key.
I am out of ideas how to solve this.
Playgound
Code:
fn main() {
let tex = Texture { name: "test.png".to_string() };
let img = Image { tex: &tex };
let mut gui_handler = GUIHandler { vec: vec![Box::new(MainGUI { img })] };
let i = InputHandler{};
i.test(&mut gui_handler,&tex);
}
trait GUI {
}
struct GUIHandler<'a> {
vec: Vec<Box<dyn GUI + 'a>>
}
struct Texture {
name: String
}
struct Image<'a>{
tex : &'a Texture
}
struct MainGUI<'a> {
img: Image<'a>
}
impl <'a> GUI for MainGUI<'a>{
}
struct InputHandler {
}
impl InputHandler {
pub fn test(&self, gui_handler:&mut GUIHandler, tex:&Texture) {
gui_handler.vec.push(Box::new(MainGUI { img: Image { tex } }))
}
}
I hope you can help and show me what I am doing wrong.