diff options
Diffstat (limited to 'crates/ra_hir_def/src/attr.rs')
-rw-r--r-- | crates/ra_hir_def/src/attr.rs | 36 |
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 | ||
3 | use std::sync::Arc; | 3 | use std::{ops, sync::Arc}; |
4 | 4 | ||
5 | use hir_expand::hygiene::Hygiene; | 5 | use hir_expand::hygiene::Hygiene; |
6 | use mbe::ast_to_token_tree; | 6 | use mbe::ast_to_token_tree; |
@@ -13,6 +13,28 @@ use tt::Subtree; | |||
13 | 13 | ||
14 | use crate::path::Path; | 14 | use crate::path::Path; |
15 | 15 | ||
16 | #[derive(Default, Debug, Clone, PartialEq, Eq)] | ||
17 | pub struct Attrs { | ||
18 | entries: Option<Arc<[Attr]>>, | ||
19 | } | ||
20 | |||
21 | impl 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 | |||
32 | impl 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)] |
17 | pub struct Attr { | 39 | pub 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 { |