aboutsummaryrefslogtreecommitdiff
path: root/crates/libsyntax2/src/ast/mod.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-22 15:01:51 +0100
committerAleksey Kladov <[email protected]>2018-08-22 15:01:51 +0100
commit69a524fbef067d9ee265fa93606c3ae743656c2a (patch)
treea575a4f1d094bb7410cc95bdd113b3d7fe1eba3a /crates/libsyntax2/src/ast/mod.rs
parent8e3bec11ebd1fce78701670cf9189b5a9d0d68f8 (diff)
dedupe
Diffstat (limited to 'crates/libsyntax2/src/ast/mod.rs')
-rw-r--r--crates/libsyntax2/src/ast/mod.rs43
1 files changed, 21 insertions, 22 deletions
diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs
index 1784b871e..ba80fc64e 100644
--- a/crates/libsyntax2/src/ast/mod.rs
+++ b/crates/libsyntax2/src/ast/mod.rs
@@ -9,7 +9,7 @@ use {
9}; 9};
10pub use self::generated::*; 10pub use self::generated::*;
11 11
12pub trait AstNode<'a>: Clone + Copy { 12pub trait AstNode<'a>: Clone + Copy + 'a {
13 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> 13 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self>
14 where Self: Sized; 14 where Self: Sized;
15 fn syntax(self) -> SyntaxNodeRef<'a>; 15 fn syntax(self) -> SyntaxNodeRef<'a>;
@@ -17,34 +17,23 @@ pub trait AstNode<'a>: Clone + Copy {
17 17
18pub trait NameOwner<'a>: AstNode<'a> { 18pub trait NameOwner<'a>: AstNode<'a> {
19 fn name(self) -> Option<Name<'a>> { 19 fn name(self) -> Option<Name<'a>> {
20 self.syntax() 20 child_opt(self)
21 .children()
22 .filter_map(Name::cast)
23 .next()
24 } 21 }
25} 22}
26 23
27pub trait TypeParamsOwner<'a>: AstNode<'a> { 24pub trait TypeParamsOwner<'a>: AstNode<'a> {
28 fn type_param_list(self) -> Option<TypeParamList<'a>> { 25 fn type_param_list(self) -> Option<TypeParamList<'a>> {
29 self.syntax() 26 child_opt(self)
30 .children()
31 .filter_map(TypeParamList::cast)
32 .next()
33 } 27 }
34 28
35 fn where_clause(self) -> Option<WhereClause<'a>> { 29 fn where_clause(self) -> Option<WhereClause<'a>> {
36 self.syntax() 30 child_opt(self)
37 .children()
38 .filter_map(WhereClause::cast)
39 .next()
40 } 31 }
41} 32}
42 33
43pub trait AttrsOwner<'a>: AstNode<'a> { 34pub trait AttrsOwner<'a>: AstNode<'a> {
44 fn attrs(&self) -> Box<Iterator<Item=Attr<'a>> + 'a> { 35 fn attrs(self) -> Box<Iterator<Item=Attr<'a>> + 'a> {
45 let it = self.syntax().children() 36 Box::new(children(self))
46 .filter_map(Attr::cast);
47 Box::new(it)
48 } 37 }
49} 38}
50 39
@@ -118,22 +107,22 @@ impl<'a> NameRef<'a> {
118} 107}
119 108
120impl<'a> ImplItem<'a> { 109impl<'a> ImplItem<'a> {
121 pub fn target_type(&self) -> Option<TypeRef<'a>> { 110 pub fn target_type(self) -> Option<TypeRef<'a>> {
122 match self.target() { 111 match self.target() {
123 (Some(t), None) | (_, Some(t)) => Some(t), 112 (Some(t), None) | (_, Some(t)) => Some(t),
124 _ => None, 113 _ => None,
125 } 114 }
126 } 115 }
127 116
128 pub fn target_trait(&self) -> Option<TypeRef<'a>> { 117 pub fn target_trait(self) -> Option<TypeRef<'a>> {
129 match self.target() { 118 match self.target() {
130 (Some(t), Some(_)) => Some(t), 119 (Some(t), Some(_)) => Some(t),
131 _ => None, 120 _ => None,
132 } 121 }
133 } 122 }
134 123
135 fn target(&self) -> (Option<TypeRef<'a>>, Option<TypeRef<'a>>) { 124 fn target(self) -> (Option<TypeRef<'a>>, Option<TypeRef<'a>>) {
136 let mut types = self.syntax().children().filter_map(TypeRef::cast); 125 let mut types = children(self);
137 let first = types.next(); 126 let first = types.next();
138 let second = types.next(); 127 let second = types.next();
139 (first, second) 128 (first, second)
@@ -141,10 +130,20 @@ impl<'a> ImplItem<'a> {
141} 130}
142 131
143impl<'a> Module<'a> { 132impl<'a> Module<'a> {
144 pub fn has_semi(&self) -> bool { 133 pub fn has_semi(self) -> bool {
145 match self.syntax().last_child() { 134 match self.syntax().last_child() {
146 None => false, 135 None => false,
147 Some(node) => node.kind() == SEMI, 136 Some(node) => node.kind() == SEMI,
148 } 137 }
149 } 138 }
150} 139}
140
141fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option<C> {
142 children(parent).next()
143}
144
145fn children<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> impl Iterator<Item=C> + 'a {
146 parent.syntax()
147 .children()
148 .filter_map(C::cast)
149}