Lifetime issue with buffer when creating structure
โ Rust ๐ 2025-07-30 ๐ค surdeus ๐๏ธ 10Hello! I am trying to work with softbuffer and faced this wierd lifetime issue when I tried creating separate struct for Buffer and Window:
se std::{num::NonZeroU32, sync::Arc};
use winit::window::Window;
type Surface = softbuffer::Surface<Arc<Window>, Arc<Window>>;
type Buffer<'a> = softbuffer::Buffer<'a, Arc<Window>, Arc<Window>>;
struct Canvas<'a> {
surface: Surface,
window: Window,
buffer: Option<Buffer<'a>>
}
impl<'a> Canvas<'a> {
fn new(surface: Surface, window: Window) -> Self {
Self { surface, window, buffer: None }
}
fn update(&mut self) {
let width = self.window.inner_size().width;
let height = self.window.inner_size().height;
self.surface.resize(NonZeroU32::new(width).expect("Window size must be positive!"), NonZeroU32::new(height).expect("WIndow size must be positive!"));
let buffer: = self.surface.buffer_mut().expect("Failed creating framebuffer");
self.buffer = Some(buffer);
}
}
But compiler says:
[rustc] lifetime may not live long enough assignment requires that `'1` must outlive `'a` [:20] let's call the lifetime of this reference `'1` [:15] lifetime `'a` defined here
I am confused because I can get Buffer and use it in code as long as I donโt drop surface, but I cannot add it to struct which will hold it some time till it will be replaced with next update().
Can someone please explain what am I doing wrong?
2 posts - 2 participants
๐ท๏ธ Rust_feed