-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BTree: refactor clone, mostly for readability #89513
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use super::node::{marker, NodeRef}; | ||
|
||
/// Simple tree around a raw `NodeRef`, that provides a destructor. | ||
/// Unlike smarter sister `BTreeMap`, it does not keep track of element count, | ||
/// does not allow replacing the root, therefore can be statically tyoed by the | ||
/// node type of the root, and cannot represent a tree without root node | ||
/// (its `Option` exists only to disable the `drop` method when needed). | ||
pub struct BareTree<K, V, RootType>(Option<NodeRef<marker::Owned, K, V, RootType>>); | ||
|
||
impl<K, V, RootType> Drop for BareTree<K, V, RootType> { | ||
fn drop(&mut self) { | ||
if let Some(root) = self.0.take() { | ||
let mut cur_edge = root.forget_type().into_dying().first_leaf_edge(); | ||
while let Some((next_edge, kv)) = unsafe { cur_edge.deallocating_next() } { | ||
unsafe { kv.drop_key_val() }; | ||
cur_edge = next_edge; | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<K, V> BareTree<K, V, marker::Leaf> { | ||
/// Returns a new tree consisting of a single leaf that is initially empty. | ||
pub fn new_leaf() -> Self { | ||
Self(Some(NodeRef::new_leaf())) | ||
} | ||
} | ||
|
||
impl<K, V> BareTree<K, V, marker::Internal> { | ||
/// Returns a new tree with an internal root, that initially has no elements | ||
/// and one child. | ||
pub fn new_internal(child: NodeRef<marker::Owned, K, V, marker::LeafOrInternal>) -> Self { | ||
Self(Some(NodeRef::new_internal(child))) | ||
} | ||
} | ||
|
||
impl<K, V, RootType> BareTree<K, V, RootType> { | ||
/// Mutably borrows the owned root node. | ||
pub fn borrow_mut(&mut self) -> NodeRef<marker::Mut<'_>, K, V, RootType> { | ||
self.0.as_mut().unwrap().borrow_mut() | ||
} | ||
|
||
/// Removes any static information asserting that the root is a `Leaf` or | ||
/// `Internal` node. | ||
pub fn forget_root_type(mut self) -> BareTree<K, V, marker::LeafOrInternal> { | ||
BareTree(self.0.take().map(NodeRef::forget_type)) | ||
} | ||
|
||
/// Consumes the tree, returning a `NodeRef` that still enforces ownership | ||
/// but lacks a destructor. | ||
pub fn into_inner(mut self) -> NodeRef<marker::Owned, K, V, RootType> { | ||
self.0.take().unwrap() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod append; | ||
mod bare_tree; | ||
mod borrow; | ||
mod dedup_sorted_iter; | ||
mod fix; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this doesn't replicate the continue-dropping-if-we-see-a-panic code, presumably because the currently only user only drops BareTrees if we're already panicking. But that seems like a footgun should we start using it somewhere that does need to keep dropping.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually because I didn't realize it belongs there.