-
Notifications
You must be signed in to change notification settings - Fork 123
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
Refactor TreeArena #827
base: main
Are you sure you want to change the base?
Refactor TreeArena #827
Conversation
Document tests more. Fix a lifetime error in unsafe API. Remove the `mem::swap` tests. While the action they were testing was possible, sharing data between two separate arenas isn't meant to be part of the API and is unlikely to happen in the wild.
Rename a lot of methods. Rework documentation. Unify safe and unsafe APIs.
fn mem_swap() { | ||
let mut tree_a: TreeArena<char> = TreeArena::new(); | ||
let mut roots_a = tree_a.root_token_mut(); | ||
roots_a.insert_child(1_u64, 'a'); | ||
let mut node_1_a = roots_a.get_child_mut(1_u64).expect("No child 1 found"); | ||
node_1_a.children.insert_child(2_u64, 'b'); | ||
node_1_a.children.insert_child(3_u64, 'c'); | ||
let mut node_3_a = node_1_a | ||
.children | ||
.get_child_mut(3_u64) | ||
.expect("No child 3 found"); | ||
|
||
node_3_a.children.insert_child(4_u64, 'd'); | ||
|
||
let mut tree_b: TreeArena<char> = TreeArena::new(); | ||
let mut roots_b = tree_b.root_token_mut(); | ||
roots_b.insert_child(4_u64, 'e'); | ||
let mut node_4_b = roots_b.get_child_mut(4_u64).expect("No child 4 found"); | ||
node_4_b.children.insert_child(3_u64, 'f'); | ||
node_4_b.children.insert_child(2_u64, 'g'); | ||
let mut node_2_b = node_4_b | ||
.children | ||
.get_child_mut(2_u64) | ||
.expect("No child 2 found"); | ||
node_2_b.children.insert_child(1_u64, 'h'); | ||
|
||
mem::swap(&mut node_1_a.children, &mut node_4_b.children); | ||
|
||
// node 1 from tree a now believes it is node 4 from tree b | ||
assert_eq!(node_1_a.id(), 4_u64, "Node 1 id should be 4"); | ||
// however it still contains the item from tree a | ||
assert_eq!(*node_1_a.item, 'a', "Node 1 item should be 'a'"); | ||
// and we can access the nodes in tree b, that node 4 was able to | ||
assert_eq!( | ||
*node_1_a.children.get_child(2_u64).unwrap().item, | ||
'g', | ||
"Node 2 item in tree b should be g" | ||
); |
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's not clear to me that this test is sufficiently useless to justify removing. This is both:
- an operation which can be performed safely
- A weird thing to do.
and so I think we should test that it doesn't cause UB.
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.
Yes this was my motivation for having the test, or something similar, as mem::swap
should be safe, but wanted to make sure that doing something with it wouldn't allow any of the preconditions to be broken (specifically that you couldn't access a parent from a child)
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.
I guess I'll re-add it with a comment, then. I'll try to make a shorter version.
Make tests more spread out and more readable.
Add comments detailing the state of the arena being tested.
Fix a lifetime error in unsafe API.
Remove the
mem::swap
test. While the operation it was testing was possible, sharing data between two separate arenas isn't meant to be part of the API and is unlikely to happen in the wild.Rename Arena[Ref/Mut]Children to Arena[Ref/Mut]List
Rename a lot of methods.
Rework documentation.
Unify safe and unsafe APIs.