Skip to content

Commit d00110b

Browse files
committed
release 0.8.6
1 parent 9b3c880 commit d00110b

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "matchit"
3-
version = "0.8.5"
3+
version = "0.8.6"
44
license = "MIT AND BSD-3-Clause"
55
authors = ["Ibraheem Ahmed <ibraheem@ibraheem.ca>"]
66
edition = "2021"

src/tree.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,29 @@ impl<T> Node<T> {
555555

556556
value.map(UnsafeCell::into_inner)
557557
}
558+
559+
/// Iterates over the tree and calls the given visitor function
560+
/// with fully resolved path and its value.
561+
pub fn for_each<V: FnMut(String, T)>(self, mut visitor: V) {
562+
let mut queue = VecDeque::from([(self.prefix.clone(), self)]);
563+
564+
// Perform a BFS on the routing tree.
565+
while let Some((mut prefix, mut node)) = queue.pop_front() {
566+
denormalize_params(&mut prefix, &node.remapping);
567+
568+
if let Some(value) = node.value.take() {
569+
let path = String::from_utf8(prefix.unescaped().to_vec()).unwrap();
570+
visitor(path, value.into_inner());
571+
}
572+
573+
// Traverse the child nodes.
574+
for child in node.children {
575+
let mut prefix = prefix.clone();
576+
prefix.append(&child.prefix);
577+
queue.push_back((prefix, child));
578+
}
579+
}
580+
}
558581
}
559582

560583
/// A wildcard node that was skipped during a tree search.
@@ -594,7 +617,7 @@ impl<T> Node<T> {
594617
if *path == *node.prefix {
595618
// Found the matching value.
596619
if let Some(ref value) = node.value {
597-
// Remap the keys of any route parameters we accumulated during the
620+
// Remap the keys of any route parameters we accumulated during the search.
598621
params.for_each_key_mut(|(i, param)| param.key = &node.remapping[i]);
599622
return Ok((value, params));
600623
}
@@ -669,7 +692,7 @@ impl<T> Node<T> {
669692
// Store the parameter value.
670693
params.push(b"", path);
671694

672-
// Remap the keys of any route parameters we accumulated during the
695+
// Remap the keys of any route parameters we accumulated during the search.
673696
params
674697
.for_each_key_mut(|(i, param)| param.key = &node.remapping[i]);
675698

@@ -741,7 +764,7 @@ impl<T> Node<T> {
741764
// Store the parameter value.
742765
params.push(b"", path);
743766

744-
// Remap the keys of any route parameters we accumulated during the
767+
// Remap the keys of any route parameters we accumulated during the search.
745768
params.for_each_key_mut(|(i, param)| param.key = &node.remapping[i]);
746769

747770
return Ok((value, params));
@@ -758,7 +781,7 @@ impl<T> Node<T> {
758781
None => return Err(MatchError::NotFound),
759782
};
760783

761-
// Remap the keys of any route parameters we accumulated during the
784+
// Remap the keys of any route parameters we accumulated during the search.
762785
params.for_each_key_mut(|(i, param)| param.key = &node.remapping[i]);
763786

764787
// Store the final catch-all parameter (`{*...}`).
@@ -809,31 +832,6 @@ impl<T> Node<T> {
809832
}
810833
}
811834

812-
impl<T> Node<T> {
813-
/// Iterates over the tree and calls the given visitor function
814-
/// with fully resolved path and its value.
815-
pub fn for_each<V: FnMut(String, T)>(self, mut visitor: V) {
816-
let mut queue = VecDeque::from([(self.prefix.clone(), self)]);
817-
818-
// Perform a BFS on the routing tree.
819-
while let Some((mut prefix, mut node)) = queue.pop_front() {
820-
denormalize_params(&mut prefix, &node.remapping);
821-
822-
if let Some(value) = node.value.take() {
823-
let path = String::from_utf8(prefix.unescaped().to_vec()).unwrap();
824-
visitor(path, value.into_inner());
825-
}
826-
827-
// Traverse the child nodes.
828-
for child in node.children {
829-
let mut prefix = prefix.clone();
830-
prefix.append(&child.prefix);
831-
queue.push_back((prefix, child));
832-
}
833-
}
834-
}
835-
}
836-
837835
/// An ordered list of route parameters keys for a specific route.
838836
///
839837
/// To support conflicting routes like `/{a}/foo` and `/{b}/bar`, route parameters

0 commit comments

Comments
 (0)