Skip to content

Commit

Permalink
Fix peek in VecSplice (#187)
Browse files Browse the repository at this point in the history
* Fix peek in `VecSplice`

* Rename `peek(_mut)` to `last_mutated(_mut)` in `VecSplice` and fixed index access
  • Loading branch information
Philipp-M authored Mar 11, 2024
1 parent fbe553f commit 50d51fa
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/xilem_core/src/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ macro_rules! generate_viewsequence_trait {

fn mark(&mut self, changeflags: $changeflags, _cx: &mut $cx) -> $changeflags
{
self.peek_mut().map(|pod| pod.mark(changeflags)).unwrap_or_default()
self.last_mutated_mut().map(|pod| pod.mark(changeflags)).unwrap_or_default()
}

fn delete(&mut self, n: usize, _cx: &mut $cx) {
Expand Down
16 changes: 12 additions & 4 deletions crates/xilem_core/src/vec_splice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,20 @@ impl<'a, 'b, T> VecSplice<'a, 'b, T> {
&mut self.v[ix]
}

pub fn peek(&self) -> Option<&T> {
self.v.last()
pub fn last_mutated(&self) -> Option<&T> {
if self.ix == 0 {
None
} else {
self.v.get(self.ix - 1)
}
}

pub fn peek_mut(&mut self) -> Option<&mut T> {
self.v.last_mut()
pub fn last_mutated_mut(&mut self) -> Option<&mut T> {
if self.ix == 0 {
None
} else {
self.v.get_mut(self.ix - 1)
}
}

pub fn len(&self) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion crates/xilem_web/src/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<'a, 'b, 'c> ElementsSplice for ChildrenSplice<'a, 'b, 'c> {
self.node_list = Some(self.parent.child_nodes());
self.node_list.as_ref().unwrap()
};
let cur_child = self.children.peek_mut().unwrap_throw();
let cur_child = self.children.last_mutated_mut().unwrap_throw();
let old_child = node_list.get(self.child_idx).unwrap_throw();
self.parent
.replace_child(cur_child.0.as_node_ref(), &old_child)
Expand Down
2 changes: 1 addition & 1 deletion src/view/tree_structure_tracking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<'a, 'b> ElementsSplice for TreeStructureSplice<'a, 'b> {
fn mark(&mut self, changeflags: ChangeFlags, cx: &mut Cx) -> ChangeFlags {
if changeflags.contains(ChangeFlags::tree_structure()) {
let current_id = self.current_child_id.take().unwrap();
let new_id = self.splice.peek().unwrap().id();
let new_id = self.splice.last_mutated().unwrap().id();
if current_id != new_id {
cx.tree_structure
.change_child(cx.element_id(), self.splice.len() - 1, new_id);
Expand Down

0 comments on commit 50d51fa

Please sign in to comment.