More visitor cleanups

Provide a non-mutable visitor as well, as the actual rendering loop doesn't need
to change the items fortunately.
This commit is contained in:
Simon Hausmann 2020-05-12 16:33:50 +02:00
parent f22b18584e
commit 5986d5f2b8
2 changed files with 38 additions and 4 deletions

View file

@ -186,14 +186,48 @@ impl ComponentUniquePtr {
/// ///
/// The state parametter returned by the visitor is passed to each children. /// The state parametter returned by the visitor is passed to each children.
pub fn visit_items<State>( pub fn visit_items<State>(
&mut self, &self,
mut visitor: impl FnMut(&mut Item, &State) -> State, mut visitor: impl FnMut(&Item, &State) -> State,
state: State, state: State,
) { ) {
self.visit_internal(&mut visitor, 0, &state) self.visit_internal(&mut visitor, 0, &state)
} }
fn visit_internal<State>( fn visit_internal<State>(
&self,
visitor: &mut impl FnMut(&Item, &State) -> State,
index: isize,
state: &State,
) {
let item_tree = unsafe { (self.vtable.as_ref().item_tree)(self.vtable.as_ptr()) };
match unsafe { &*item_tree.offset(index) } {
ItemTreeNode::Item { vtable, offset, children_index, chilren_count } => {
let item = unsafe {
Item::new(
NonNull::new_unchecked(*vtable as *mut _),
NonNull::new_unchecked(
(self.inner.as_ptr() as *mut u8).offset(*offset) as *mut _
),
)
};
let state = visitor(&item, state);
for c in *children_index..(*children_index + *chilren_count) {
self.visit_internal(visitor, c as isize, &state)
}
}
ItemTreeNode::DynamicTree { .. } => todo!(),
}
}
pub fn visit_items_mut<State>(
&mut self,
mut visitor: impl FnMut(&mut Item, &State) -> State,
state: State,
) {
self.visit_internal_mut(&mut visitor, 0, &state)
}
fn visit_internal_mut<State>(
&mut self, &mut self,
visitor: &mut impl FnMut(&mut Item, &State) -> State, visitor: &mut impl FnMut(&mut Item, &State) -> State,
index: isize, index: isize,
@ -212,7 +246,7 @@ impl ComponentUniquePtr {
}; };
let state = visitor(&mut item, state); let state = visitor(&mut item, state);
for c in *children_index..(*children_index + *chilren_count) { for c in *children_index..(*children_index + *chilren_count) {
self.visit_internal(visitor, c as isize, &state) self.visit_internal_mut(visitor, c as isize, &state)
} }
} }
ItemTreeNode::DynamicTree { .. } => todo!(), ItemTreeNode::DynamicTree { .. } => todo!(),

View file

@ -89,7 +89,7 @@ pub fn run_component<GraphicsBackend, GraphicsFactoryFunc>(
let rendering_cache = &mut main_window.rendering_cache; let rendering_cache = &mut main_window.rendering_cache;
// Generate cached rendering data once // Generate cached rendering data once
component.visit_items( component.visit_items_mut(
move |item, _| { move |item, _| {
let item_rendering_info = { let item_rendering_info = {
match item.rendering_info() { match item.rendering_info() {