aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/attr.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-22 08:27:47 +0000
committerAleksey Kladov <[email protected]>2019-11-22 08:27:47 +0000
commite42f9627664cc3c44094e1c4f985270fbfd592b1 (patch)
tree81603da9c34f301cf82e1ceae80313b8eb9e10c4 /crates/ra_hir_def/src/attr.rs
parenta1346bba5c457d1aa0a35f44231bed8b494b7d60 (diff)
Encapsulate Attrs
Diffstat (limited to 'crates/ra_hir_def/src/attr.rs')
-rw-r--r--crates/ra_hir_def/src/attr.rs36
1 files changed, 30 insertions, 6 deletions
diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs
index 0e961ca12..7a9d0fdf4 100644
--- a/crates/ra_hir_def/src/attr.rs
+++ b/crates/ra_hir_def/src/attr.rs
@@ -1,6 +1,6 @@
1//! A higher level attributes based on TokenTree, with also some shortcuts. 1//! A higher level attributes based on TokenTree, with also some shortcuts.
2 2
3use std::sync::Arc; 3use std::{ops, sync::Arc};
4 4
5use hir_expand::hygiene::Hygiene; 5use hir_expand::hygiene::Hygiene;
6use mbe::ast_to_token_tree; 6use mbe::ast_to_token_tree;
@@ -13,6 +13,28 @@ use tt::Subtree;
13 13
14use crate::path::Path; 14use crate::path::Path;
15 15
16#[derive(Default, Debug, Clone, PartialEq, Eq)]
17pub struct Attrs {
18 entries: Option<Arc<[Attr]>>,
19}
20
21impl ops::Deref for Attrs {
22 type Target = [Attr];
23
24 fn deref(&self) -> &[Attr] {
25 match &self.entries {
26 Some(it) => &*it,
27 None => &[],
28 }
29 }
30}
31
32impl Attrs {
33 pub fn has_atom(&self, atom: &str) -> bool {
34 self.iter().any(|it| it.is_simple_atom(atom))
35 }
36}
37
16#[derive(Debug, Clone, PartialEq, Eq)] 38#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct Attr { 39pub struct Attr {
18 pub(crate) path: Path, 40 pub(crate) path: Path,
@@ -43,13 +65,15 @@ impl Attr {
43 Some(Attr { path, input }) 65 Some(Attr { path, input })
44 } 66 }
45 67
46 pub fn from_attrs_owner(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Option<Arc<[Attr]>> { 68 pub fn from_attrs_owner(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Attrs {
47 let mut attrs = owner.attrs().peekable(); 69 let mut attrs = owner.attrs().peekable();
48 if attrs.peek().is_none() { 70 let entries = if attrs.peek().is_none() {
49 // Avoid heap allocation 71 // Avoid heap allocation
50 return None; 72 None
51 } 73 } else {
52 Some(attrs.flat_map(|ast| Attr::from_src(ast, hygiene)).collect()) 74 Some(attrs.flat_map(|ast| Attr::from_src(ast, hygiene)).collect())
75 };
76 Attrs { entries }
53 } 77 }
54 78
55 pub fn is_simple_atom(&self, name: &str) -> bool { 79 pub fn is_simple_atom(&self, name: &str) -> bool {