Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Refactor TreeArena #827

wants to merge 5 commits into from

Conversation

PoignardAzur
Copy link
Contributor

@PoignardAzur PoignardAzur commented Jan 16, 2025

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.

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.
@PoignardAzur PoignardAzur changed the title Edit TreeArena tests Refactor TreeArena Jan 16, 2025
Comment on lines -99 to -136
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"
);
Copy link
Member

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:

  1. an operation which can be performed safely
  2. A weird thing to do.

and so I think we should test that it doesn't cause UB.

Copy link
Contributor

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)

Copy link
Contributor Author

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.

.github/workflows/ci.yml Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants