Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Commit b8315bd

Browse files
authored
Merge pull request #7 from mkmik/lifetime
Simplify lifetime declaration for Matcher
2 parents 572e311 + 407b2ad commit b8315bd

File tree

2 files changed

+7
-15
lines changed

2 files changed

+7
-15
lines changed

src/matchers.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ use std::iter;
1010
// Matcher maps a node to a list of nodes. If the input node is not matched by the matcher or
1111
// the matcher does not select any subnodes of the input node, then the result is empty.
1212
pub trait Matcher {
13-
fn select<'a>(&self, node: &'a Value) -> Box<dyn Iterator<Item = &'a Value> + 'a>;
13+
fn select<'a>(&'a self, node: &'a Value) -> Box<dyn Iterator<Item = &'a Value> + 'a>;
1414
}
1515

1616
pub struct RootSelector {}
1717

1818
impl Matcher for RootSelector {
19-
fn select<'a>(&self, node: &'a Value) -> Box<dyn Iterator<Item = &'a Value> + 'a> {
19+
fn select<'a>(&'a self, node: &'a Value) -> Box<dyn Iterator<Item = &'a Value> + 'a> {
2020
Box::new(iter::once(node))
2121
}
2222
}
@@ -42,7 +42,7 @@ pub fn new_child_matcher(name: String) -> Child {
4242
}
4343

4444
impl Matcher for Child {
45-
fn select<'a>(&self, node: &'a Value) -> Box<dyn Iterator<Item = &'a Value> + 'a> {
45+
fn select<'a>(&'a self, node: &'a Value) -> Box<dyn Iterator<Item = &'a Value> + 'a> {
4646
if node.is_object() {
4747
let mapping = node.as_object().unwrap();
4848
if mapping.contains_key(&self.name) {
@@ -65,15 +65,7 @@ pub fn new_union(elements: Vec<Box<dyn Matcher>>) -> Union {
6565
}
6666

6767
impl Matcher for Union {
68-
fn select<'a, 'b>(&'a self, node: &'b Value) -> Box<dyn Iterator<Item = &'b Value> + 'b> {
69-
// union of matches of the matchers in the union
70-
let mut u = vec![];
71-
for m in &self.elements {
72-
let m_selection = m.select(node);
73-
for s in m_selection {
74-
u.push(s);
75-
}
76-
}
77-
Box::new(u.into_iter())
68+
fn select<'a>(&'a self, node: &'a Value) -> Box<dyn Iterator<Item = &'a Value> + 'a> {
69+
Box::new(self.elements.iter().flat_map(move |it| it.select(node)))
7870
}
7971
}

src/path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub enum FindError {
1212
}
1313

1414
pub trait Path {
15-
fn find<'a>(&self, document: &'a Value) -> Result<Vec<&'a Value>, FindError>;
15+
fn find<'a>(&'a self, document: &'a Value) -> Result<Vec<&'a Value>, FindError>;
1616
}
1717

1818
struct SelectorPath {
@@ -24,7 +24,7 @@ pub fn new<'a>(matchers: Vec<Box<dyn matchers::Matcher>>) -> impl Path + 'a {
2424
}
2525

2626
impl Path for SelectorPath {
27-
fn find<'a>(&self, document: &'a Value) -> Result<Vec<&'a Value>, FindError> {
27+
fn find<'a>(&'a self, document: &'a Value) -> Result<Vec<&'a Value>, FindError> {
2828
// pass nodes, starting with document alone, through each matcher in turn
2929
Ok((&self.matchers)
3030
.iter()

0 commit comments

Comments
 (0)