From 3ced8feed55af3dbb5119820396d51741e8af043 Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Sun, 25 Jul 2021 02:21:25 -0400 Subject: [PATCH] avoid cloning selector to determine visibility --- src/common.rs | 28 +++++++++++------------- src/parse/mod.rs | 28 +++++------------------- src/selector/extend/extended_selector.rs | 5 +++++ 3 files changed, 23 insertions(+), 38 deletions(-) diff --git a/src/common.rs b/src/common.rs index 554a567..0a3624d 100644 --- a/src/common.rs +++ b/src/common.rs @@ -115,33 +115,31 @@ impl ListSeparator { #[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord, Copy)] pub(crate) struct Identifier(InternedString); +impl Identifier { + fn from_str(s: &str) -> Self { + if s.contains('_') { + Identifier(InternedString::get_or_intern(s.replace('_', "-"))) + } else { + Identifier(InternedString::get_or_intern(s)) + } + } +} + impl From for Identifier { fn from(s: String) -> Identifier { - Identifier(InternedString::get_or_intern(if s.contains('_') { - s.replace('_', "-") - } else { - s - })) + Self::from_str(&s) } } impl From<&String> for Identifier { fn from(s: &String) -> Identifier { - if s.contains('_') { - Identifier(InternedString::get_or_intern(s.replace('_', "-"))) - } else { - Identifier(InternedString::get_or_intern(s)) - } + Self::from_str(s) } } impl From<&str> for Identifier { fn from(s: &str) -> Identifier { - if s.contains('_') { - Identifier(InternedString::get_or_intern(s.replace('_', "-"))) - } else { - Identifier(InternedString::get_or_intern(s)) - } + Self::from_str(s) } } diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 237efd2..47bf1a3 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -672,13 +672,7 @@ impl<'a, 'b> Parser<'a, 'b> { } } - if !self - .super_selectors - .last() - .clone() - .into_selector() - .is_empty() - { + if !self.super_selectors.last().as_selector_list().is_empty() { body = vec![Stmt::RuleSet { selector: self.super_selectors.last().clone(), body, @@ -719,13 +713,7 @@ impl<'a, 'b> Parser<'a, 'b> { } } - if !self - .super_selectors - .last() - .clone() - .into_selector() - .is_empty() - { + if !self.super_selectors.last().as_selector_list().is_empty() { body = vec![Stmt::RuleSet { selector: self.super_selectors.last().clone(), body, @@ -860,14 +848,14 @@ impl<'a, 'b> Parser<'a, 'b> { } let compound = match complex.components.first() { - Some(ComplexSelectorComponent::Compound(c)) => c.clone(), + Some(ComplexSelectorComponent::Compound(c)) => c, Some(..) | None => todo!(), }; if compound.components.len() != 1 { return Err(( format!( "compound selectors may no longer be extended.\nConsider `@extend {}` instead.\nSee http://bit.ly/ExtendCompound for details.\n", - compound.components.into_iter().map(|x| x.to_string()).collect::>().join(", ") + compound.components.iter().map(|x| x.to_string()).collect::>().join(", ") ) , self.span_before).into()); } @@ -907,13 +895,7 @@ impl<'a, 'b> Parser<'a, 'b> { } } - if !self - .super_selectors - .last() - .clone() - .into_selector() - .is_empty() - { + if !self.super_selectors.last().as_selector_list().is_empty() { body = vec![Stmt::RuleSet { selector: self.super_selectors.last().clone(), body, diff --git a/src/selector/extend/extended_selector.rs b/src/selector/extend/extended_selector.rs index 6bacf06..86fb84e 100644 --- a/src/selector/extend/extended_selector.rs +++ b/src/selector/extend/extended_selector.rs @@ -2,6 +2,7 @@ use std::{ cell::RefCell, collections::{hash_set::IntoIter, HashSet}, hash::{Hash, Hasher}, + ops::Deref, ptr, rc::Rc, }; @@ -43,6 +44,10 @@ impl ExtendedSelector { }) } + pub fn as_selector_list(&self) -> impl Deref + '_ { + self.0.borrow() + } + pub fn set_inner(&mut self, selector: SelectorList) { self.0.replace(selector); }