Hello,
I've got a problem with my game engine. I think it is due to the lifetime 'b but i don't know how to make a correct lifetime in this case.
pub struct TextureManager<'a> {
pub txt_map: HashMap<&'a str,piston_window::G2dTexture>
}
impl <'a> TextureManager<'a> {
fn new(width: u32,height: u32) -> TextureManager<'a> {
TextureManager{txt_map: HashMap::new()}
}
fn register(&mut self,window: &mut PistonWindow,name: &'a str) -> &piston_window::G2dTexture {
self.txt_map.insert(name,piston_window::Texture::from_path(
&mut window.factory,
&find_folder::Search::ParentsThenKids(3, 3)
.for_folder("assets").unwrap().join(name),
piston_window::Flip::None,
&piston_window::TextureSettings::new()
).unwrap());
return self.get(name);
}
fn get(&self,stre: &'a str) -> &piston_window::G2dTexture {
self.txt_map.get(stre).unwrap()
}
fn create<'b>(&mut self,txt_name: &'b str,pos_x: f64,pos_y: f64,width: f64,height: f64) -> Sprite<'b,'a> {
Sprite::new(self,txt_name,pos_x,pos_y,width,height)
}
}
pub struct Sprite<'a,'b> {
pub manager: &'b TextureManager<'b>,
pub texture_name: &'a str,
pub pos_x: f64,
pub pos_y: f64,
pub width: f64,
pub height: f64,
pub layer: u8
}
impl <'a,'b>Sprite<'a,'b> {
pub fn new(manager: &'b mut TextureManager<'b>,texture_name: &'a str,pos_x: f64,pos_y: f64,width: f64,height: f64) -> Sprite<'a,'b> {
Sprite{manager,texture_name,pos_x,pos_y,width,height,layer: 0}
}
pub fn draw(&mut self,w: &mut piston_window::PistonWindow,e: &piston_window::Event) {
w.draw_2d(e, |c, gl| {
Image::new().rect([self.pos_x,self.pos_y,self.width,self.height])
.draw(self.manager.get(self.texture_name), &piston_window::DrawState::new_alpha(), c.transform, gl);
});
}
pub fn set_pos(&mut self,pos_x: f64,pos_y: f64) {
self.pos_x=pos_x;
self.pos_y=pos_y;
}
pub fn set_size(&mut self,width: f64,height: f64) {
self.width=width;
self.height=height;
}
}
And the error:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements
--> src/main.rs:137:9
|
137 | Sprite::new(self,txt_name,pos_x,pos_y,width,height)
| ^^^^^^^^^^^
|
Thanks in advance and sorry for the inconvenience.
ccgauche.