Use HashMap entry API in a cleaner way

Careful use of the right `VacantEntry` and `OccupiedEntry` methods makes
this possible without hacks or lifetime errors.
This commit is contained in:
Ian Douglas Scott 2024-12-23 16:05:28 -08:00 committed by Victoria Brekenfeld
parent 32f9ff3cac
commit 3f5c64f50f

View file

@ -75,7 +75,7 @@ use tracing::{error, trace, warn};
use std::{ use std::{
borrow::BorrowMut, borrow::BorrowMut,
collections::{HashMap, HashSet}, collections::{hash_map, HashMap, HashSet},
mem, mem,
sync::{ sync::{
atomic::{AtomicBool, Ordering}, atomic::{AtomicBool, Ordering},
@ -1060,18 +1060,15 @@ impl SurfaceThreadState {
}) || mirrored_output.current_scale().fractional_scale() }) || mirrored_output.current_scale().fractional_scale()
!= self.output.current_scale().fractional_scale() != self.output.current_scale().fractional_scale()
}) { }) {
let mirroring_state = { let mirroring_state = match self.mirroring_textures.entry(self.target_node) {
let entry = self.mirroring_textures.entry(self.target_node); hash_map::Entry::Occupied(occupied) => occupied.into_mut(),
let mut new_state = None; hash_map::Entry::Vacant(vacant) => {
if matches!(entry, std::collections::hash_map::Entry::Vacant(_)) { vacant.insert(MirroringState::new_with_renderer(
new_state = Some(MirroringState::new_with_renderer(
&mut renderer, &mut renderer,
compositor.format(), compositor.format(),
mirrored_output, mirrored_output,
)?); )?)
} }
// I really want a failable initializer...
entry.or_insert_with(|| new_state.unwrap())
}; };
mirroring_state mirroring_state