Skip to content

Commit d07e795

Browse files
committed
test(deep_causality_ast): minor lints and optimizations.
Signed-off-by: Marvin Hansen <[email protected]>
1 parent a59043c commit d07e795

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

deep_causality_ast/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ license.workspace = true
77

88
repository = "https://github.com/deepcausality/deep_causality.rs"
99
authors = ["Marvin Hansen <[email protected]>", ]
10-
description = "AST data structure for for deep_causality crate."
10+
description = "AST data structure for deep_causality crate."
1111
documentation = "https://docs.rs/deep_causality"
1212
categories = ["data-structures", "science"]
1313
keywords = ["data-structures", "ast"]

deep_causality_ast/src/const_tree/accessors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl<T> ConstTree<T> {
7878
let mut max_depth = 0;
7979
let mut queue = VecDeque::new();
8080

81-
// Always start with the root node at depth 1.
81+
// Start with the root node at depth 1.
8282
queue.push_back((self, 1));
8383

8484
while let Some((current_node, current_depth)) = queue.pop_front() {

deep_causality_ast/src/const_tree/map.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,19 @@ impl<T: Clone> ConstTree<ConstTree<T>> {
2525
/// assert_eq!(*joined.children()[0].value(), 2);
2626
/// ```
2727
pub fn join(self) -> ConstTree<T> {
28-
// The root of the outer tree has a value which is the inner tree.
29-
let inner_tree = self.value().clone();
28+
// Consume the outer node when possible to avoid unnecessary clones.
29+
let outer_node = Arc::try_unwrap(self.node).unwrap_or_else(|arc| (*arc).clone());
3030

31-
// The new children are the children of the inner tree,
32-
// plus the result of recursively joining the children of the outer tree.
31+
// Take the inner tree (value of the outer root).
32+
let inner_tree = outer_node.value;
33+
34+
// Start with inner_tree's existing children.
3335
let mut new_children = inner_tree.children().to_vec();
34-
let joined_outer_children = self.children().iter().map(|c| c.clone().join());
35-
new_children.extend(joined_outer_children);
3636

37+
// Recursively join each outer child into a flat ConstTree<T>.
38+
new_children.extend(outer_node.children.into_iter().map(|c| c.join()));
39+
40+
// Build the resulting tree using the inner root value.
3741
ConstTree::with_children(inner_tree.value().clone(), new_children)
3842
}
3943
}

deep_causality_ast/tests/const_tree/const_tree_tests.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4+
*/
5+
16
#![allow(dead_code)]
27
#![allow(unused_imports)]
38

@@ -283,6 +288,16 @@ fn test_into_map() {
283288
assert_eq!(*tree_clone.children()[0].value(), 20);
284289
}
285290

291+
#[test]
292+
fn test_consuming_iterator_ok_branch() {
293+
let tree = ConstTree::with_children(1, vec![ConstTree::new(2), ConstTree::new(3)]);
294+
295+
// Call into_iter on the original tree, ensuring no other clones exist.
296+
// This should trigger the Ok branch of Arc::try_unwrap for the root node.
297+
let values: Vec<_> = tree.into_iter().collect();
298+
assert_eq!(values, vec![1, 2, 3]);
299+
}
300+
286301
#[test]
287302
fn test_consuming_iterator_shared_arc() {
288303
let tree = ConstTree::with_children(1, vec![ConstTree::new(2), ConstTree::new(3)]);

0 commit comments

Comments
 (0)