Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl<T> Tree<T> {
*id2 = offset_id(*id2);
});
}
self.vec.extend(other_tree.vec);
self.vec.append(&mut other_tree.vec);
unsafe { self.get_unchecked_mut(other_tree_root_id) }
}

Expand Down Expand Up @@ -822,6 +822,22 @@ pub mod iter;
/// };
/// # }
/// ```
/// Compose trees using the `@` marker:
/// ```
/// #[macro_use] extern crate ego_tree;
/// # fn main() {
/// let subtree = tree! {
/// "foo" => { "bar", "baz" }
/// };
/// let new_tree = tree! {
/// "root" => {
/// "child x",
/// "child y",
/// @ subtree,
/// }
/// };
/// # }
/// ```
#[macro_export]
macro_rules! tree {
(@ $n:ident { }) => { };
Expand Down Expand Up @@ -858,6 +874,13 @@ macro_rules! tree {
}
};

// Append subtree from expression.
(@ $n:ident { @ $subtree:expr $(, $($tail:tt)*)? }) => {{
$n.append_subtree($subtree);
$( tree!(@ $n { $($tail)* }); )?
}};


($root:expr) => { $crate::Tree::new($root) };

($root:expr => $children:tt) => {
Expand Down
24 changes: 24 additions & 0 deletions tests/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,27 @@ fn mixed() {

assert_eq!(manual_tree, macro_tree);
}

#[test]
fn subtree() {
let subtree = tree! {
'x' => { 'y' => {'z'}}
};
let tree = tree! {
'a' => {
'b',
'c' => {'d', 'e'},
@ subtree.clone(),
'f' => { @subtree },
}
};
let expected_tree = tree! {
'a' => {
'b',
'c' => {'d', 'e'},
'x' => { 'y' => {'z'}},
'f' => {'x' => { 'y' => {'z'}} },
}
};
assert_eq!(tree, expected_tree);
}