@hierarchon I have a question with (of course), lifetimes and the borrow checker lol.

@hierarchon So, I'm trying (again) to make a game, and I want a structure that stores textures that I can look up with copyable keys. But load() here borrows self and never returns it and I don't understand why.


@cambrian_era hm, why are you making making 'man a type-level parameter of ResourceManager? it only appears as a lifetime bound on &mut self in the load() method, so you could just do

fn load<'man>(&'man mut self, path: &Path) -> i64;


fn load(&mut self, path: &Path) -> i64

with elision

@hierarchon So, if I recall, it was to resolve this down on load_texture:
cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements

note: expected `sdl2::render::Texture<'res>`
found `sdl2::render::Texture<'_>`rustc(E0495)

@cambrian_era aha, I have an idea for what it is. instead of using &'man mut self, try &'res mut self?

@hierarchon That resolves the conflicting lifetimes requirements but we're back to it complaining that the lifetime needs to be 'static and that I'm dropping while borrowed after I load.

@cambrian_era hm, that makes me think that the problem might be in your *surrounding* code. can you paste the entire error message?

So, as far as I can tell, the relevant code is:

let mut texman = TextureManager::new(Rc::new(texture_creator));

let playerTexture = texman.load(&Path::new(&out_dir).join("./src/rsrc/images/Player.png"));

`texman` does not live long enough

borrowed value does not live long enough
argument requires that `texman` is borrowed for `'static`
`texman` dropped here while still borrowed

And then..
cannot move out of `texman` because it is borrowed

move out of `texman` occurs here
I get THIS part.

@cambrian_era can you show me a screenshot? it'd be easier if I understand what 'dropped here' and such refers to

but my *guess* is that when you invoke `resources.insert(texman)`, you're moving the texture manager, which means that the lifetime of &'res can't last beyond the resources.insert() call. so if you reference playerTexture after calling `resources.insert(texman)`, you lose.

@hierarchon No, you're right. It is being moved... that's why playerTexture is supposed to just be an i64, because if I just put references into the ECS stuff it'd immediately break.

@cambrian_era yeah, I'm not sure, then. this is always one part of rust where I'm not super clear on things, sorry :<

@hierarchon No problem. I can always get things started with sort of trivial stuff, like, I can load a sprite and have it move around. But when I try to organize things it all breaks.

Sign in to participate in the conversation
Radical Town

A cool and chill place for cool and chill people.