Skip to content

Commit 6ba7b7c

Browse files
committed
Implement HasAttrs for Annotatable
1 parent bb4a79b commit 6ba7b7c

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/libsyntax/ext/base.rs

+19-16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub use self::SyntaxExtension::*;
1212

1313
use ast;
1414
use ast::{Name, PatKind};
15+
use attr::HasAttrs;
1516
use codemap;
1617
use codemap::{CodeMap, Span, ExpnId, ExpnInfo, NO_EXPANSION};
1718
use errors::DiagnosticBuilder;
@@ -41,29 +42,31 @@ pub enum Annotatable {
4142
ImplItem(P<ast::ImplItem>),
4243
}
4344

44-
impl Annotatable {
45-
pub fn attrs(&self) -> &[ast::Attribute] {
45+
impl HasAttrs for Annotatable {
46+
fn attrs(&self) -> &[ast::Attribute] {
4647
match *self {
47-
Annotatable::Item(ref i) => &i.attrs,
48-
Annotatable::TraitItem(ref ti) => &ti.attrs,
49-
Annotatable::ImplItem(ref ii) => &ii.attrs,
48+
Annotatable::Item(ref item) => &item.attrs,
49+
Annotatable::TraitItem(ref trait_item) => &trait_item.attrs,
50+
Annotatable::ImplItem(ref impl_item) => &impl_item.attrs,
5051
}
5152
}
5253

53-
pub fn fold_attrs(self, attrs: Vec<ast::Attribute>) -> Annotatable {
54+
fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self {
5455
match self {
55-
Annotatable::Item(i) => Annotatable::Item(i.map(|i| ast::Item {
56-
attrs: attrs,
57-
..i
58-
})),
59-
Annotatable::TraitItem(i) => Annotatable::TraitItem(i.map(|ti| {
60-
ast::TraitItem { attrs: attrs, ..ti }
61-
})),
62-
Annotatable::ImplItem(i) => Annotatable::ImplItem(i.map(|ii| {
63-
ast::ImplItem { attrs: attrs, ..ii }
64-
})),
56+
Annotatable::Item(item) => Annotatable::Item(item.map_attrs(f)),
57+
Annotatable::TraitItem(trait_item) => Annotatable::TraitItem(trait_item.map_attrs(f)),
58+
Annotatable::ImplItem(impl_item) => Annotatable::ImplItem(impl_item.map_attrs(f)),
6559
}
6660
}
61+
}
62+
63+
impl Annotatable {
64+
pub fn attrs(&self) -> &[ast::Attribute] {
65+
HasAttrs::attrs(self)
66+
}
67+
pub fn fold_attrs(self, attrs: Vec<ast::Attribute>) -> Annotatable {
68+
self.map_attrs(|_| attrs)
69+
}
6770

6871
pub fn expect_item(self) -> P<ast::Item> {
6972
match self {

0 commit comments

Comments
 (0)