From bb53aaab8a94a7e50fb5087d13895121e31d2010 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Mon, 25 May 2020 13:09:20 -0400 Subject: [PATCH] replace std::borrow::Cow with beef::Cow --- Cargo.toml | 1 + src/args.rs | 5 +++-- src/atrule/mod.rs | 10 ++++----- src/builtin/meta.rs | 2 +- src/lib.rs | 5 +++++ src/utils/strings.rs | 19 ++++++++-------- src/value/css_function.rs | 10 +++++---- src/value/mod.rs | 46 +++++++++++++++++++-------------------- src/value/ops.rs | 4 ++-- 9 files changed, 55 insertions(+), 47 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 65dea74..228aa55 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,7 @@ rand = { version = "0.7.3", optional = true } codemap = "0.1.3" peekmore = "0.4.0" wasm-bindgen = { version = "0.2.60", optional = true } +beef = "0.4.4" [features] default = ["commandline", "random"] diff --git a/src/args.rs b/src/args.rs index e25c4bb..0ae0583 100644 --- a/src/args.rs +++ b/src/args.rs @@ -5,6 +5,7 @@ use codemap::{Span, Spanned}; use peekmore::PeekMoreIterator; +use crate::Cow; use crate::common::Identifier; use crate::error::SassResult; use crate::scope::Scope; @@ -90,9 +91,9 @@ impl CallArgs { .iter() .map(|a| { span = span.merge(a.span); - Ok(a.node.to_css_string(a.span)?.into()) + Ok(a.node.to_css_string(a.span)?) }) - .collect::>>()? + .collect::>>>()? .join(", "), ); string.push(')'); diff --git a/src/atrule/mod.rs b/src/atrule/mod.rs index 097a811..f364173 100644 --- a/src/atrule/mod.rs +++ b/src/atrule/mod.rs @@ -10,7 +10,7 @@ use crate::utils::{ read_until_semicolon_or_closing_curly_brace, }; use crate::value::Value; -use crate::{RuleSet, Stmt, Token}; +use crate::{Cow, RuleSet, Stmt, Token}; use each_rule::{parse_each, Each}; use for_rule::For; @@ -36,8 +36,8 @@ mod while_rule; #[derive(Debug, Clone)] pub(crate) enum AtRule { - Warn(Spanned), - Debug(Spanned), + Warn(Spanned>), + Debug(Spanned>), Mixin(String, Box), Function(String, Box), Return(Vec), @@ -94,7 +94,7 @@ impl AtRule { devour_whitespace(toks); Spanned { node: AtRule::Warn(Spanned { - node: message.to_css_string(span)?.into(), + node: message.to_css_string(span)?, span, }), span, @@ -117,7 +117,7 @@ impl AtRule { devour_whitespace(toks); Spanned { node: AtRule::Debug(Spanned { - node: message.inspect(span)?.into(), + node: message.inspect(span)?, span, }), span, diff --git a/src/builtin/meta.rs b/src/builtin/meta.rs index 620198d..5033238 100644 --- a/src/builtin/meta.rs +++ b/src/builtin/meta.rs @@ -98,7 +98,7 @@ fn inspect(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> Sass Ok(Value::String( arg!(args, scope, super_selector, 0, "value") .inspect(args.span())? - .into(), + .into_owned(), QuoteKind::None, )) } diff --git a/src/lib.rs b/src/lib.rs index bb521f1..e94fb13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,6 +84,11 @@ grass input.scss use std::convert::TryFrom; use std::iter::Iterator; +#[cfg(target_pointer_width = "64")] +pub(crate) use beef::lean::Cow; +#[cfg(not(target_pointer_width = "64"))] +pub(crate) use beef::Cow; + use codemap::{Span, Spanned}; use peekmore::{PeekMore, PeekMoreIterator}; diff --git a/src/utils/strings.rs b/src/utils/strings.rs index f731e40..4573941 100644 --- a/src/utils/strings.rs +++ b/src/utils/strings.rs @@ -1,4 +1,5 @@ use std::iter::Iterator; +use std::borrow::Borrow; use codemap::{Span, Spanned}; @@ -216,12 +217,10 @@ pub(crate) fn eat_ident>( }; if kind == '{' { toks.next(); - text.push_str( - &match parse_interpolation(toks, scope, super_selector, pos)?.node { - Value::String(s, ..) => s, - v => v.to_css_string(span)?.into(), - }, - ); + match parse_interpolation(toks, scope, super_selector, pos)?.node { + Value::String(ref s, ..) => text.push_str(s), + v => text.push_str(v.to_css_string(span)?.borrow()), + } } else { return Err(("Expected identifier.", pos).into()); } @@ -300,10 +299,10 @@ pub(crate) fn parse_quoted_string>( if let Some(Token { kind: '{', pos }) = toks.peek().cloned() { toks.next(); let interpolation = parse_interpolation(toks, scope, super_selector, pos)?; - s.push_str(&match interpolation.node { - Value::String(s, ..) => s, - v => v.to_css_string(interpolation.span)?.into(), - }); + match interpolation.node { + Value::String(ref v, ..) => s.push_str(v), + v => s.push_str(v.to_css_string(interpolation.span)?.borrow()), + }; continue; } else { s.push('#'); diff --git a/src/value/css_function.rs b/src/value/css_function.rs index 3b9dab6..a8b92d0 100644 --- a/src/value/css_function.rs +++ b/src/value/css_function.rs @@ -1,3 +1,5 @@ +use std::borrow::Borrow; + use codemap::{Span, Spanned}; use peekmore::PeekMoreIterator; @@ -115,10 +117,10 @@ pub(crate) fn try_eat_url>( peek_counter += 1; let (interpolation, count) = peek_interpolation(toks, scope, super_selector, pos)?; peek_counter += count; - buf.push_str(&match interpolation.node { - Value::String(s, ..) => s, - v => v.to_css_string(interpolation.span)?.into(), - }); + match interpolation.node { + Value::String(ref s, ..) => buf.push_str(s), + v => buf.push_str(v.to_css_string(interpolation.span)?.borrow()), + }; } else { buf.push('#'); } diff --git a/src/value/mod.rs b/src/value/mod.rs index 703d10c..c45f3a4 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -1,4 +1,3 @@ -use std::borrow::Cow; use std::iter::Iterator; use codemap::{Span, Spanned}; @@ -8,6 +7,7 @@ use crate::common::{Brackets, ListSeparator, Op, QuoteKind}; use crate::error::SassResult; use crate::unit::Unit; use crate::utils::hex_char_for; +use crate::Cow; use css_function::is_special_function; pub(crate) use map::SassMap; @@ -128,12 +128,12 @@ impl Value { pub fn to_css_string(&self, span: Span) -> SassResult> { Ok(match self { - Self::Important => Cow::Borrowed("!important"), + Self::Important => Cow::const_str("!important"), Self::Dimension(num, unit) => match unit { Unit::Mul(..) => { return Err((format!("{}{} isn't a valid CSS value.", num, unit), span).into()); } - _ => Cow::Owned(format!("{}{}", num, unit)), + _ => Cow::owned(format!("{}{}", num, unit)), }, Self::Map(..) | Self::Function(..) => { return Err(( @@ -143,14 +143,14 @@ impl Value { .into()) } Self::List(vals, sep, brackets) => match brackets { - Brackets::None => Cow::Owned( + Brackets::None => Cow::owned( vals.iter() .filter(|x| !x.is_null(span).unwrap()) .map(|x| x.to_css_string(span)) .collect::>>>()? .join(sep.as_str()), ), - Brackets::Bracketed => Cow::Owned(format!( + Brackets::Bracketed => Cow::owned(format!( "[{}]", vals.iter() .filter(|x| !x.is_null(span).unwrap()) @@ -159,7 +159,7 @@ impl Value { .join(sep.as_str()), )), }, - Self::Color(c) => Cow::Owned(c.to_string()), + Self::Color(c) => Cow::owned(c.to_string()), Self::UnaryOp(..) | Self::BinaryOp(..) => { self.clone().eval(span)?.to_css_string(span)? } @@ -184,21 +184,21 @@ impl Value { } } } - Cow::Owned(buf) + Cow::owned(buf) } Self::String(string, QuoteKind::Quoted) => { let mut buf = String::with_capacity(string.len()); visit_quoted_string(&mut buf, false, string)?; - Cow::Owned(buf) + Cow::owned(buf) } - Self::True => Cow::Borrowed("true"), - Self::False => Cow::Borrowed("false"), - Self::Null => Cow::Borrowed(""), - Self::ArgList(args) => Cow::Owned( + Self::True => Cow::const_str("true"), + Self::False => Cow::const_str("false"), + Self::Null => Cow::const_str(""), + Self::ArgList(args) => Cow::owned( args.iter() .filter(|x| !x.is_null(span).unwrap()) - .map(|a| Ok(a.node.to_css_string(span)?.into())) - .collect::>>()? + .map(|a| Ok(a.node.to_css_string(span)?)) + .collect::>>>()? .join(", "), ), }) @@ -265,20 +265,20 @@ impl Value { pub fn inspect(&self, span: Span) -> SassResult> { Ok(match self { Value::List(v, _, brackets) if v.is_empty() => match brackets { - Brackets::None => Cow::Borrowed("()"), - Brackets::Bracketed => Cow::Borrowed("[]"), + Brackets::None => Cow::const_str("()"), + Brackets::Bracketed => Cow::const_str("[]"), }, Value::List(v, sep, brackets) if v.len() == 1 => match brackets { Brackets::None => match sep { ListSeparator::Space => v[0].inspect(span)?, - ListSeparator::Comma => Cow::Owned(format!("({},)", v[0].inspect(span)?)), + ListSeparator::Comma => Cow::owned(format!("({},)", v[0].inspect(span)?)), }, Brackets::Bracketed => match sep { - ListSeparator::Space => Cow::Owned(format!("[{}]", v[0].inspect(span)?)), - ListSeparator::Comma => Cow::Owned(format!("[{},]", v[0].inspect(span)?)), + ListSeparator::Space => Cow::owned(format!("[{}]", v[0].inspect(span)?)), + ListSeparator::Comma => Cow::owned(format!("[{},]", v[0].inspect(span)?)), }, }, - Self::List(vals, sep, brackets) => Cow::Owned(match brackets { + Self::List(vals, sep, brackets) => Cow::owned(match brackets { Brackets::None => vals .iter() .map(|x| x.inspect(span)) @@ -292,9 +292,9 @@ impl Value { .join(sep.as_str()), ), }), - Value::Function(f) => Cow::Owned(format!("get-function(\"{}\")", f.name())), - Value::Null => Cow::Borrowed("null"), - Value::Map(map) => Cow::Owned(format!( + Value::Function(f) => Cow::owned(format!("get-function(\"{}\")", f.name())), + Value::Null => Cow::const_str("null"), + Value::Map(map) => Cow::owned(format!( "({})", map.iter() .map(|(k, v)| Ok(format!( diff --git a/src/value/ops.rs b/src/value/ops.rs index 8dc061b..6b71dc5 100644 --- a/src/value/ops.rs +++ b/src/value/ops.rs @@ -268,7 +268,7 @@ impl Value { format!("{}{}", self.to_css_string(span)?, s), QuoteKind::Quoted, ), - Self::Null => Value::String(self.to_css_string(span)?.into(), QuoteKind::None), + Self::Null => Value::String(self.to_css_string(span)?.into_owned(), QuoteKind::None), _ => Value::String( format!( "{}{}", @@ -280,7 +280,7 @@ impl Value { }, Self::Null => match other { Self::Null => Self::Null, - _ => Value::String(other.to_css_string(span)?.into(), QuoteKind::None), + _ => Value::String(other.to_css_string(span)?.into_owned(), QuoteKind::None), }, Self::Dimension(num, unit) => match other { Self::Dimension(num2, unit2) => {