Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
44 changes: 44 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ impl<T> Node<T> {
value,
}
}

fn map_value<U>(self, transform: impl FnOnce(T) -> U) -> Node<U> {
Node {
parent: self.parent,
prev_sibling: self.prev_sibling,
next_sibling: self.next_sibling,
children: self.children,
value: transform(self.value),
}
}

fn map_value_ref<U>(&self, transform: impl FnOnce(&T) -> U) -> Node<U> {
Node {
parent: self.parent,
prev_sibling: self.prev_sibling,
next_sibling: self.next_sibling,
children: self.children,
value: transform(&self.value),
}
}
}

/// Node reference.
Expand Down Expand Up @@ -232,6 +252,30 @@ impl<T> Tree<T> {
self.vec.extend(other_tree.vec);
unsafe { self.get_unchecked_mut(other_tree_root_id) }
}

/// Maps a `Tree<T>` to `Tree<U>` by applying a function to all node values,
/// copying over the tree's structure and node ids untouched, consuming `self`.
pub fn map_values<U>(self, transform: impl Fn(T) -> U) -> Tree<U> {
Tree {
vec: self
.vec
.into_iter()
.map(|node| node.map_value(&transform))
.collect(),
}
}

/// Maps a `&Tree<T>` to `Tree<U>` by applying a function to all node values,
/// copying over the tree's structure and node ids untouched.
pub fn map_value_refs<U>(&self, transform: impl Fn(&T) -> U) -> Tree<U> {
Tree {
vec: self
.vec
.iter()
.map(|node| node.map_value_ref(&transform))
.collect(),
}
}
}

impl<'a, T: 'a> NodeRef<'a, T> {
Expand Down
52 changes: 52 additions & 0 deletions tests/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,58 @@ fn insert_id_before() {
assert_eq!(3, tree.root().children().count());
}

#[test]
fn test_map_values() {
let str_tree = tree! {
"root" => {
"a" => {
"child 1",
},
"b" => {
"child 2",
},
}
};

let identity_mapped_tree = str_tree.clone().map_values(|value| value);

// If we pass the identity function to `.map_values()`,
// then we expect the tree to effectively remain untouched:
assert_eq!(str_tree, identity_mapped_tree);

let string_tree = str_tree.clone().map_values(|value| value.to_owned());

// A `&str` will produce the same output for `.to_string()` as its equivalent `String`,
// so the output of `.to_string()` should match for corresponding trees as well:
assert_eq!(str_tree.to_string(), string_tree.to_string());
}

#[test]
fn test_map_value_refs() {
let str_tree = tree! {
"root" => {
"a" => {
"child 1",
},
"b" => {
"child 2",
},
}
};

let identity_mapped_tree = str_tree.map_value_refs(|&value| value);

// If we pass the identity function to `.map_values()`,
// then we expect the tree to effectively remain untouched:
assert_eq!(str_tree, identity_mapped_tree);

let string_tree = str_tree.map_value_refs(|&value| value.to_owned());

// A `&str` will produce the same output for `.to_string()` as its equivalent `String`,
// so the output of `.to_string()` should match for corresponding trees as well:
assert_eq!(str_tree.to_string(), string_tree.to_string());
}

#[test]
fn test_display() {
let tree = tree! {
Expand Down