Replaced some Options with OnceLocks.

This commit is contained in:
Filipe Rodrigues 2025-09-14 10:03:21 +01:00
parent a06a826bfa
commit 7485c2ee6a
Signed by: zenithsiz
SSH Key Fingerprint: SHA256:Mb5ppb3Sh7IarBO/sBTXLHbYEOz37hJAlslLQPPAPaU
2 changed files with 16 additions and 11 deletions

View File

@ -6,7 +6,12 @@ use {
::image::DynamicImage,
app_error::Context,
image::imageops,
std::{self, mem, path::Path, sync::Arc},
std::{
self,
mem,
path::Path,
sync::{Arc, OnceLock},
},
zsw_util::{AppError, Loadable, loadable::Loader},
zsw_wgpu::Wgpu,
zutil_cloned::cloned,
@ -25,10 +30,10 @@ pub struct PanelFadeImages {
pub next: Option<PanelFadeImage>,
/// Texture sampler
pub image_sampler: Option<wgpu::Sampler>,
pub image_sampler: OnceLock<wgpu::Sampler>,
/// Texture bind group
pub image_bind_group: Option<wgpu::BindGroup>,
pub image_bind_group: OnceLock<wgpu::BindGroup>,
/// Next image
pub next_image: Loadable<ImageLoadRes, NextImageLoader>,
@ -63,8 +68,8 @@ impl PanelFadeImages {
prev: None,
cur: None,
next: None,
image_sampler: None,
image_bind_group: None,
image_sampler: OnceLock::new(),
image_bind_group: OnceLock::new(),
next_image: Loadable::new(NextImageLoader),
}
}
@ -79,7 +84,7 @@ impl PanelFadeImages {
mem::swap(&mut self.cur, &mut self.next);
mem::swap(&mut self.prev, &mut self.cur);
self.prev = None;
self.image_bind_group = None;
self.image_bind_group = OnceLock::new();
self.load_missing(playlist_player, wgpu);
Ok(())
@ -99,7 +104,7 @@ impl PanelFadeImages {
mem::swap(&mut self.prev, &mut self.cur);
mem::swap(&mut self.cur, &mut self.next);
self.next = None;
self.image_bind_group = None;
self.image_bind_group = OnceLock::new();
self.load_missing(playlist_player, wgpu);
Ok(())
@ -170,7 +175,7 @@ impl PanelFadeImages {
Slot::Cur => self.cur = Some(image),
Slot::Next => self.next = Some(image),
}
self.image_bind_group = None;
self.image_bind_group = OnceLock::new();
}
}

View File

@ -219,12 +219,12 @@ impl PanelsRenderer {
let panel_images = panel_state.images_mut();
let image_sampler = panel_images
.image_sampler
.get_or_insert_with(|| self::create_image_sampler(wgpu));
.get_or_init(|| self::create_image_sampler(wgpu));
let [prev, cur, next] = [&panel_images.prev, &panel_images.cur, &panel_images.next]
.map(|img| img.as_ref().map_or(&wgpu.empty_texture_view, |img| &img.texture_view));
let image_bind_group = panel_images.image_bind_group.get_or_insert_with(|| {
let image_bind_group = panel_images.image_bind_group.get_or_init(|| {
let fade_image_bind_group_layout = shared
.fade_image_bind_group_layout
.get_or_init(|| self::create_fade_image_bind_group_layout(wgpu));
@ -237,7 +237,7 @@ impl PanelsRenderer {
image_sampler,
)
});
render_pass.set_bind_group(1, &*image_bind_group, &[]);
render_pass.set_bind_group(1, image_bind_group, &[]);
},
PanelState::Slide(_panel_state) => (),
}