Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 52 additions & 10 deletions src/write_zipper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,11 +1420,10 @@ impl <'a, 'path, V: Clone + Send + Sync + Unpin, A: Allocator + 'a> WriteZipperC
self.focus_stack.root_mut()
} else {
match self.focus_stack.top_mut().unwrap().node_into_child_mut(node_key) {
Some((consumed_bytes, child_node)) => {
debug_assert_eq!(consumed_bytes, node_key.len());
Some(child_node)
},
None => None
Some((consumed_bytes, child_node)) if consumed_bytes == node_key.len() => Some(child_node),
//A child that ends short of the focus is a node *above* it: the focus is below a dangling
// path held as an empty node, so it doesn't exist
_ => None
}
}
}
Expand Down Expand Up @@ -1455,11 +1454,10 @@ impl <'a, 'path, V: Clone + Send + Sync + Unpin, A: Allocator + 'a> WriteZipperC
Some(OpaqueTrieNodeRef( unsafe{ self.focus_stack.root_unchecked() } ))
} else {
match self.focus_stack.top().unwrap().node_get_child(node_key) {
Some((consumed_bytes, child_node)) => {
debug_assert_eq!(consumed_bytes, node_key.len());
Some(OpaqueTrieNodeRef(child_node))
},
None => None
Some((consumed_bytes, child_node)) if consumed_bytes == node_key.len() => Some(OpaqueTrieNodeRef(child_node)),
//A child that ends short of the focus is a node *above* it: the focus is below a dangling
// path held as an empty node, so it doesn't exist
_ => None
}
}
}
Expand Down Expand Up @@ -4441,6 +4439,50 @@ mod tests {
}
}

/// `try_borrow_focus` / `try_borrow_focus_mut` at a missing focus below a dangling path held as an
/// empty node are `None`, as the read zipper's are. They used to hand out the empty node above the
/// focus as the focus's own (a debug assertion in debug builds)
#[test]
fn write_zipper_try_borrow_focus_below_dangling() {
let mut m = PathMap::<u64>::new();
m.set_val_at(&[1u8], 0);
m.create_path(&[0u8]);
let mut wz = m.write_zipper();
wz.descend_to(&[0u8, 0]);
assert!(!wz.path_exists());
assert!(wz.try_borrow_focus().is_none());
assert!(wz.core().try_borrow_focus_mut().is_none());
drop(wz);
assert!(m.read_zipper_at_path(&[0u8, 0]).try_borrow_focus().is_none());

//Below a ZipperHead writer's stump
let mut m = PathMap::<u64>::new();
{ let zh = m.zipper_head(); let _w = zh.write_zipper_at_exclusive_path(&[0u8]).unwrap(); }
let mut wz = m.write_zipper();
wz.descend_to(&[0u8, 7, 7]);
assert!(!wz.path_exists());
assert!(wz.try_borrow_focus().is_none());
assert!(wz.core().try_borrow_focus_mut().is_none());
//At the stump itself, the node is the focus's
wz.reset();
wz.descend_to(&[0u8]);
assert!(wz.path_exists());
assert!(wz.try_borrow_focus().is_some());
assert!(wz.core().try_borrow_focus_mut().is_some());
drop(wz);

//`graft_masked_branches` at a missing focus below the stump grafts at the focus, not into the
// stump's node
let src = PathMap::from_iter([(&[5u8][..], 50u64), (&[6u8][..], 60)]);
let mut wz = m.write_zipper();
wz.descend_to(&[0u8, 7]);
wz.graft_masked_branches(&src.read_zipper(), ByteMask::from_iter([5u8]), false);
drop(wz);
assert_eq!(m.val_at(&[0u8, 7, 5]), Some(&50));
assert_eq!(m.val_at(&[0u8, 5]), None);
assert_eq!(m.val_at(&[0u8, 7, 6]), None);
}

/// A write after `prune_path` (or `meet_into(.., true)`) must reach the focus node
#[test]
fn write_zipper_write_after_prune_path_below_a_graft() {
Expand Down