diff --git a/src/lib.rs b/src/lib.rs index d6778f9..9d88149 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -380,7 +380,7 @@ impl<'a, K, V> NodeRef, K, V> { } /// Borrows exclusive access to the leaf portion of a leaf or internal node. - fn as_leaf_mut(&mut self) -> &mut LeafNode { + fn as_leaf_mut(&mut self) -> &'a mut LeafNode { let ptr = Self::as_leaf_ptr(self); // SAFETY: we have exclusive access to the entire node. unsafe { &mut *ptr } @@ -578,9 +578,12 @@ impl<'a, K: 'a, V: 'a> NodeRef, K, V> { } } -impl Handle, HandleType> { +impl<'a, BorrowType, K, V, HandleType> Handle, HandleType> +where + BorrowType: 'a, +{ /// Temporarily takes out another immutable handle on the same location. - pub(crate) fn reborrow(&self) -> Handle, K, V>, HandleType> { + pub(crate) fn reborrow(&self) -> Handle, K, V>, HandleType> { // We can't use Handle::new_kv or Handle::new_edge because we don't know our type Handle { node: self.node.reborrow(), @@ -630,7 +633,7 @@ impl NodeRef { } impl<'a, K: 'a, V: 'a> Handle, K, V>, marker::Value> { - pub(crate) unsafe fn value_mut(&mut self) -> &mut V { + pub(crate) unsafe fn value_mut(&mut self) -> &'a mut V { let leaf = self.node.as_leaf_mut(); let v = leaf.value.as_mut().unwrap(); v @@ -1037,15 +1040,15 @@ mod entry { pub(crate) _marker: PhantomData<&'a mut (K, V)>, } - impl<'a, K, V> OccupiedEntry<'a, K, V> { - pub fn get(&self) -> &V { + impl<'entry, K, V> OccupiedEntry<'entry, K, V> { + pub fn get(&self) -> &'entry V { unsafe { self.handle.reborrow().into_value_unchecked() } } - pub fn get_mut(&mut self) -> &mut V { + pub fn get_mut(&mut self) -> &'entry mut V { unsafe { self.handle.value_mut() } } - pub fn into_subtree(self) -> super::subtree::Subtree> { + pub fn into_subtree(self) -> super::subtree::Subtree> { Subtree::new_root(self.handle.node) } } @@ -1264,6 +1267,11 @@ mod subtree { } } + pub fn insert(&mut self, key_seq: impl Iterator + 'tree, val: V) -> &'tree mut V { + let mut entry = self.entry(key_seq).or_insert(val); + entry.get_mut() + } + pub fn get_subtree_mut( &'_ mut self, mut key_seq: Q, @@ -1626,6 +1634,12 @@ where let mut subtree = self.as_subtree_mut(); subtree.get_mut(key_seq) } + + pub fn insert<'a>(&'a mut self, key_seq: impl Iterator + 'a, val: V) -> &mut V { + let mut subtree = self.as_subtree_mut(); + let v = subtree.insert(key_seq, val); + v + } } unsafe fn slice_insert(slice: &mut [MaybeUninit], idx: usize, value: T) {