Skip to content
Open
Show file tree
Hide file tree
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
77 changes: 72 additions & 5 deletions src/trie_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,20 @@ impl<V: Clone + Send + Sync, A: Allocator> TrieRefOwned<V, A> {
Self { focus_node: Some(node.clone()), val_or_key, alloc }
}

/// Makes a `TrieRefOwned` from a node and a val
/// Makes a `TrieRefOwned` to an existing location, from the node below it and its value
///
/// `None` for the node means the location has nothing below it, not that it doesn't exist: the
/// `TrieRefOwned` is still valid, holds `val`, and gets an empty node. A caller with a location
/// that may not exist must check that first, and use [Self::new_invalid_in] if it doesn't.
pub(crate) fn new_with_node_and_val_in(focus_node: Option<TrieNodeODRc<V, A>>, val: Option<V>, alloc: A) -> Self {
match focus_node {
Some(_) => Self { focus_node: focus_node, val_or_key: ValOrKey { val: (VAL_SENTINEL, core::mem::ManuallyDrop::new(val)) }, alloc },
None => Self::new_invalid_in(alloc)
}
let focus_node = match focus_node {
Some(node) => node,
#[cfg(feature = "slim_ptrs")]
None => TrieNodeODRc::new_empty(),
#[cfg(not(feature = "slim_ptrs"))]
None => TrieNodeODRc::new_allocated_in(0, 0, alloc.clone()),
};
Self { focus_node: Some(focus_node), val_or_key: ValOrKey { val: (VAL_SENTINEL, core::mem::ManuallyDrop::new(val)) }, alloc }
}
/// Makes a new `TrieRef` that points to no trie node.
pub(crate) fn new_invalid_in(alloc: A) -> Self {
Expand Down Expand Up @@ -1545,4 +1553,63 @@ mod tests {
}
}
}

/// A `TrieRefOwned` to a location with nothing below it still exists and holds the location's
/// value: `TrieRefOwned::from` a childless map, and a write zipper's `get_trie_ref` at a leaf.
/// Both used to give an invalid ref and drop the value
#[test]
fn trie_ref_owned_childless_location() {
let mut m = PathMap::<u64>::new();
m.set_val_at([], 5);
for t in [TrieRef::from(m.clone()), TrieRefOwned::from(m.clone()).into()] {
assert!(t.path_exists());
assert_eq!(t.val(), Some(&5));
assert_eq!(t.child_count(), 0);
assert_eq!(t.val_at([1]), None);
assert!(!t.trie_ref_at_path([1]).path_exists());
let mut rz = t.fork_read_zipper().unwrap();
assert_eq!(rz.val(), Some(&5));
assert!(!rz.descend_first_byte().is_some());
#[cfg(feature = "graft_root_vals")]
assert_eq!(t.make_map().val_at([]), Some(&5));
assert!(t.make_map().read_zipper().child_count() == 0);
}

//An empty map: the root exists, with no value
let t = TrieRefOwned::from(PathMap::<u64>::new());
assert!(t.path_exists());
assert_eq!(t.val(), None);
assert_eq!(t.child_count(), 0);

//A write zipper at a leaf, at a dangling path, and at a path that doesn't exist
let mut m = PathMap::<u64>::new();
m.set_val_at([2], 66);
m.create_path([3, 4]);
let mut wz = m.write_zipper();
wz.descend_to([2]);
assert_eq!(wz.val(), Some(&66));
let t = wz.get_trie_ref();
assert!(t.path_exists());
#[cfg(feature = "graft_root_vals")]
assert_eq!(t.val(), Some(&66));
assert_eq!(t.child_count(), 0);
wz.reset();
wz.descend_to([3, 4]);
assert!(wz.path_exists());
let t = wz.get_trie_ref();
assert!(t.path_exists());
assert_eq!(t.val(), None);
wz.reset();
wz.descend_to([3, 5]);
assert!(!wz.path_exists());
let t = wz.get_trie_ref();
assert!(!t.path_exists());
assert_eq!(t.val(), None);
//A location with children is unchanged
wz.reset();
wz.descend_to([3]);
let t = wz.get_trie_ref();
assert!(t.path_exists());
assert_eq!(t.child_count(), 1);
}
}
3 changes: 3 additions & 0 deletions src/write_zipper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,9 @@ impl<'a, V: Clone + Send + Sync + Unpin, A: Allocator + 'a> WriteZipperCore<'a,
PathMap::new_with_root_in(root_node, root_val, self.alloc.clone())
}
fn get_trie_ref(&self) -> TrieRef<'_, V, A> {
if !self.path_exists() {
return TrieRefOwned::new_invalid_in(self.alloc.clone()).into()
}
#[cfg(not(feature = "graft_root_vals"))]
let root_val = None;
#[cfg(feature = "graft_root_vals")]
Expand Down