WayVR: Minor refactoring (#203)

Use iterators instead of closures.
This commit is contained in:
Aleksander
2025-04-30 00:49:48 +02:00
committed by GitHub
parent 9fb1d63477
commit 6b3f091552
2 changed files with 24 additions and 23 deletions

View File

@@ -48,7 +48,6 @@ macro_rules! gen_id {
}
//ThingVec
#[allow(dead_code, clippy::iter_not_returning_iterator)]
impl $container_name {
pub const fn new() -> Self {
Self {
@@ -57,25 +56,27 @@ macro_rules! gen_id {
}
}
pub fn iter(&self, callback: &dyn Fn($handle_name, &$instance_name)) {
for (idx, opt_cell) in self.vec.iter().enumerate() {
if let Some(cell) = opt_cell {
pub fn iter(&self) -> impl Iterator<Item = ($handle_name, &$instance_name)> {
self.vec.iter().enumerate().filter_map(|(idx, opt_cell)| {
opt_cell.as_ref().map(|cell| {
let handle = $container_name::get_handle(&cell, idx);
callback(handle, &cell.obj);
}
}
(handle, &cell.obj)
})
})
}
pub fn iter_mut(
&mut self,
callback: &mut dyn FnMut($handle_name, &mut $instance_name),
) {
for (idx, opt_cell) in self.vec.iter_mut().enumerate() {
if let Some(cell) = opt_cell {
let handle = $container_name::get_handle(&cell, idx);
callback(handle, &mut cell.obj);
}
}
) -> impl Iterator<Item = ($handle_name, &mut $instance_name)> {
self.vec
.iter_mut()
.enumerate()
.filter_map(|(idx, opt_cell)| {
opt_cell.as_mut().map(|cell| {
let handle = $container_name::get_handle(&cell, idx);
(handle, &mut cell.obj)
})
})
}
pub const fn get_handle(cell: &$cell_name, idx: usize) -> $handle_name {

View File

@@ -311,7 +311,7 @@ impl WayVR {
});
// Check for redraw events
self.state.displays.iter_mut(&mut |_, disp| {
for (_, disp) in self.state.displays.iter_mut() {
for disp_window in &disp.displayed_windows {
if self
.state
@@ -322,17 +322,17 @@ impl WayVR {
disp.wants_redraw = true;
}
}
});
}
// Tick all child processes
let mut to_remove: SmallVec<[(process::ProcessHandle, display::DisplayHandle); 2]> =
SmallVec::new();
self.state.processes.iter_mut(&mut |handle, process| {
for (handle, process) in self.state.processes.iter_mut() {
if !process.is_running() {
to_remove.push((handle, process.display_handle()));
}
});
}
for (p_handle, disp_handle) in &to_remove {
self.state.processes.remove(p_handle);
@@ -345,9 +345,9 @@ impl WayVR {
}
}
self.state.displays.iter_mut(&mut |handle, display| {
for (handle, display) in self.state.displays.iter_mut() {
display.tick(&self.state.config, &handle, &mut self.state.signals);
});
}
if !to_remove.is_empty() {
self.state.signals.send(WayVRSignal::BroadcastStateChanged(
@@ -591,11 +591,11 @@ impl WayVRState {
let mut process_names = Vec::<String>::new();
self.processes.iter_mut(&mut |_, process| {
for (_, process) in self.processes.iter_mut() {
if process.display_handle() == handle {
process_names.push(process.get_name());
}
});
}
if !display.displayed_windows.is_empty() || !process_names.is_empty() {
anyhow::bail!(