In Rust SDL2 I need to create a texture. The way to do this is to get a TextureCreator
from a Window
and then use
TextureCreator
to create the textures. The TextureCreator
lives independently from its Window
.
However the Texture
must not outlive the TextureCreator
. I don't know how to encapsulate this in a struct so that
the compiler knows that.
The code in main.rs
gives error [E0597]
(compiled with stable 1.18)
error[E0597]: `tc` does not live long enough
--> main.rs:19:19
|
19 | let tex = tc.create_texture_streaming(None, 128, 128).unwrap();
| ^^ does not live long enough
...
24 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the impl at 16:1...
--> main.rs:16:1
|
16 | / impl<'a> Thing<'a> {
17 | | pub fn new(c: &Canvas<Window>) -> Thing<'a> {
18 | | let tc = c.texture_creator();
19 | | let tex = tc.create_texture_streaming(None, 128, 128).unwrap();
... |
24 | | }
25 | | }
| |_^