From e42f9627664cc3c44094e1c4f985270fbfd592b1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 11:27:47 +0300 Subject: Encapsulate Attrs --- crates/ra_hir_def/src/attr.rs | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir_def/src/attr.rs') 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 @@ //! A higher level attributes based on TokenTree, with also some shortcuts. -use std::sync::Arc; +use std::{ops, sync::Arc}; use hir_expand::hygiene::Hygiene; use mbe::ast_to_token_tree; @@ -13,6 +13,28 @@ use tt::Subtree; use crate::path::Path; +#[derive(Default, Debug, Clone, PartialEq, Eq)] +pub struct Attrs { + entries: Option>, +} + +impl ops::Deref for Attrs { + type Target = [Attr]; + + fn deref(&self) -> &[Attr] { + match &self.entries { + Some(it) => &*it, + None => &[], + } + } +} + +impl Attrs { + pub fn has_atom(&self, atom: &str) -> bool { + self.iter().any(|it| it.is_simple_atom(atom)) + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct Attr { pub(crate) path: Path, @@ -43,13 +65,15 @@ impl Attr { Some(Attr { path, input }) } - pub fn from_attrs_owner(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Option> { + pub fn from_attrs_owner(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Attrs { let mut attrs = owner.attrs().peekable(); - if attrs.peek().is_none() { + let entries = if attrs.peek().is_none() { // Avoid heap allocation - return None; - } - Some(attrs.flat_map(|ast| Attr::from_src(ast, hygiene)).collect()) + None + } else { + Some(attrs.flat_map(|ast| Attr::from_src(ast, hygiene)).collect()) + }; + Attrs { entries } } pub fn is_simple_atom(&self, name: &str) -> bool { -- cgit v1.2.3