@@ -10,13 +10,13 @@ use std::iter;
10
10
// Matcher maps a node to a list of nodes. If the input node is not matched by the matcher or
11
11
// the matcher does not select any subnodes of the input node, then the result is empty.
12
12
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 > ;
14
14
}
15
15
16
16
pub struct RootSelector { }
17
17
18
18
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 > {
20
20
Box :: new ( iter:: once ( node) )
21
21
}
22
22
}
@@ -42,7 +42,7 @@ pub fn new_child_matcher(name: String) -> Child {
42
42
}
43
43
44
44
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 > {
46
46
if node. is_object ( ) {
47
47
let mapping = node. as_object ( ) . unwrap ( ) ;
48
48
if mapping. contains_key ( & self . name ) {
@@ -65,15 +65,7 @@ pub fn new_union(elements: Vec<Box<dyn Matcher>>) -> Union {
65
65
}
66
66
67
67
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) ) )
78
70
}
79
71
}
0 commit comments