Hi, I would like to use an image in a struct, but how can I make it a &'static from the beginning
pub struct MapAsPng {
dimension: (u32, u32),
data: &'static [u8],
}
impl Default for MapAsPng {
fn default() -> Self {
let image = image::open("./map.png").expect("Could not find map.png in root");
let dimension = image.dimensions();
let data: &'static [u8] = image
.as_flat_samples_u8()
.expect("The Map Image was empty or does not exist")
.samples;
MapAsPng {
dimension,
data,
}
}
}
I am using the image crate
This is the error:
error[E0597]: `image` does not live long enough
--> src/mapsetup.rs:45:35
|
43 | let image = image::open("./map.png").expect("Could not find map.png in root");
| ----- binding `image` declared here
44 | let dimension = image.dimensions();
45 | let data: &'static [u8] = image
| ------------- ^^^^^ borrowed value does not live long enough
| |
| type annotation requires that `image` is borrowed for `'static`
...
54 | }
| - `image` dropped here while still borrowed
I guess image has to be static from the beginning but how could I do that(I would like to change the image that is going to be opened while the code is running in the future)
Thanks 🙂