Skip to content

Commit 5cf34eb

Browse files
committed
Use CompactCowStr instead of Cow<str>
1 parent 5daadfd commit 5cf34eb

File tree

5 files changed

+55
-58
lines changed

5 files changed

+55
-58
lines changed

src/parser.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5+
use compact_cow_str::CompactCowStr;
56
use std::ops::Range;
67
use std::ascii::AsciiExt;
78
use std::ops::BitOr;
8-
use std::borrow::Cow;
99
use tokenizer::{self, Token, NumericValue, PercentageValue, Tokenizer, SourceLocation};
1010

1111

@@ -440,7 +440,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
440440

441441
/// Parse a <ident-token> and return the unescaped value.
442442
#[inline]
443-
pub fn expect_ident(&mut self) -> Result<Cow<'i, str>, BasicParseError<'i>> {
443+
pub fn expect_ident(&mut self) -> Result<CompactCowStr<'i>, BasicParseError<'i>> {
444444
match try!(self.next()) {
445445
Token::Ident(value) => Ok(value),
446446
t => Err(BasicParseError::UnexpectedToken(t))
@@ -458,7 +458,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
458458

459459
/// Parse a <string-token> and return the unescaped value.
460460
#[inline]
461-
pub fn expect_string(&mut self) -> Result<Cow<'i, str>, BasicParseError<'i>> {
461+
pub fn expect_string(&mut self) -> Result<CompactCowStr<'i>, BasicParseError<'i>> {
462462
match try!(self.next()) {
463463
Token::QuotedString(value) => Ok(value),
464464
t => Err(BasicParseError::UnexpectedToken(t))
@@ -467,7 +467,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
467467

468468
/// Parse either a <ident-token> or a <string-token>, and return the unescaped value.
469469
#[inline]
470-
pub fn expect_ident_or_string(&mut self) -> Result<Cow<'i, str>, BasicParseError<'i>> {
470+
pub fn expect_ident_or_string(&mut self) -> Result<CompactCowStr<'i>, BasicParseError<'i>> {
471471
match try!(self.next()) {
472472
Token::Ident(value) => Ok(value),
473473
Token::QuotedString(value) => Ok(value),
@@ -477,7 +477,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
477477

478478
/// Parse a <url-token> and return the unescaped value.
479479
#[inline]
480-
pub fn expect_url(&mut self) -> Result<Cow<'i, str>, BasicParseError<'i>> {
480+
pub fn expect_url(&mut self) -> Result<CompactCowStr<'i>, BasicParseError<'i>> {
481481
match try!(self.next()) {
482482
Token::UnquotedUrl(value) => Ok(value),
483483
Token::Function(ref name) if name.eq_ignore_ascii_case("url") => {
@@ -491,7 +491,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
491491

492492
/// Parse either a <url-token> or a <string-token>, and return the unescaped value.
493493
#[inline]
494-
pub fn expect_url_or_string(&mut self) -> Result<Cow<'i, str>, BasicParseError<'i>> {
494+
pub fn expect_url_or_string(&mut self) -> Result<CompactCowStr<'i>, BasicParseError<'i>> {
495495
match try!(self.next()) {
496496
Token::UnquotedUrl(value) => Ok(value),
497497
Token::QuotedString(value) => Ok(value),
@@ -607,7 +607,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
607607
///
608608
/// If the result is `Ok`, you can then call the `Parser::parse_nested_block` method.
609609
#[inline]
610-
pub fn expect_function(&mut self) -> Result<Cow<'i, str>, BasicParseError<'i>> {
610+
pub fn expect_function(&mut self) -> Result<CompactCowStr<'i>, BasicParseError<'i>> {
611611
match try!(self.next()) {
612612
Token::Function(name) => Ok(name),
613613
t => Err(BasicParseError::UnexpectedToken(t))

src/rules_and_declarations.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
// https://drafts.csswg.org/css-syntax/#parsing
66

7+
use compact_cow_str::CompactCowStr;
78
use parser::{parse_until_before, parse_until_after, parse_nested_block};
89
use std::ascii::AsciiExt;
910
use std::ops::Range;
10-
use std::borrow::Cow;
1111
use super::{Token, Parser, Delimiter, SourcePosition, ParseError, BasicParseError};
1212

1313

@@ -72,7 +72,7 @@ pub trait DeclarationParser<'i> {
7272
/// If `!important` can be used in a given context,
7373
/// `input.try(parse_important).is_ok()` should be used at the end
7474
/// of the implementation of this method and the result should be part of the return value.
75-
fn parse_value<'t>(&mut self, name: Cow<'i, str>, input: &mut Parser<'i, 't>)
75+
fn parse_value<'t>(&mut self, name: CompactCowStr<'i>, input: &mut Parser<'i, 't>)
7676
-> Result<Self::Declaration, ParseError<'i, Self::Error>>;
7777
}
7878

@@ -112,7 +112,7 @@ pub trait AtRuleParser<'i> {
112112
/// The given `input` is a "delimited" parser
113113
/// that ends wherever the prelude should end.
114114
/// (Before the next semicolon, the next `{`, or the end of the current block.)
115-
fn parse_prelude<'t>(&mut self, name: Cow<'i, str>, input: &mut Parser<'i, 't>)
115+
fn parse_prelude<'t>(&mut self, name: CompactCowStr<'i>, input: &mut Parser<'i, 't>)
116116
-> Result<AtRuleType<Self::Prelude, Self::AtRule>, ParseError<'i, Self::Error>> {
117117
let _ = name;
118118
let _ = input;
@@ -407,7 +407,7 @@ pub struct PreciseParseError<'i, E: 'i> {
407407
pub span: Range<SourcePosition>,
408408
}
409409

410-
fn parse_at_rule<'i: 't, 't, P, E>(start_position: SourcePosition, name: Cow<'i, str>,
410+
fn parse_at_rule<'i: 't, 't, P, E>(start_position: SourcePosition, name: CompactCowStr<'i>,
411411
input: &mut Parser<'i, 't>, parser: &mut P)
412412
-> Result<<P as AtRuleParser<'i>>::AtRule, PreciseParseError<'i, E>>
413413
where P: AtRuleParser<'i, Error = E> {

src/size_of_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ macro_rules! size_of_test {
3232
}
3333

3434
// These assume 64-bit
35-
size_of_test!(token, Token, 56);
35+
size_of_test!(token, Token, 40);
3636
size_of_test!(numeric_value, NumericValue, 16);
3737
size_of_test!(percentage_value, PercentageValue, 16);
3838
size_of_test!(std_cow_str, Cow<'static, str>, 32);

src/tests.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
extern crate test;
77

88
use encoding_rs;
9-
use std::borrow::Cow::{self, Borrowed};
109
use rustc_serialize::json::{self, Json, ToJson};
1110

1211
#[cfg(feature = "bench")]
@@ -17,7 +16,7 @@ use super::{Parser, Delimiter, Token, NumericValue, PercentageValue, SourceLocat
1716
AtRuleType, AtRuleParser, QualifiedRuleParser, ParserInput,
1817
parse_one_declaration, parse_one_rule, parse_important,
1918
stylesheet_encoding, EncodingSupport,
20-
TokenSerializationType,
19+
TokenSerializationType, CompactCowStr,
2120
Color, RGBA, parse_nth, UnicodeRange, ToCss};
2221

2322
macro_rules! JArray {
@@ -290,12 +289,12 @@ fn unquoted_url_escaping() {
290289
)\
291290
");
292291
let mut input = ParserInput::new(&serialized);
293-
assert_eq!(Parser::new(&mut input).next(), Ok(token))
292+
assert_eq!(Parser::new(&mut input).next(), Ok(token));
294293
}
295294

296295
#[test]
297296
fn test_expect_url() {
298-
fn parse<'a>(s: &mut ParserInput<'a>) -> Result<Cow<'a, str>, BasicParseError<'a>> {
297+
fn parse<'a>(s: &mut ParserInput<'a>) -> Result<CompactCowStr<'a>, BasicParseError<'a>> {
299298
Parser::new(s).expect_url()
300299
}
301300
let mut input = ParserInput::new("url()");
@@ -453,15 +452,15 @@ fn line_numbers() {
453452
let mut input = ParserInput::new("foo bar\nbaz\r\n\n\"a\\\r\nb\"");
454453
let mut input = Parser::new(&mut input);
455454
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 1 });
456-
assert_eq!(input.next_including_whitespace(), Ok(Token::Ident(Borrowed("foo"))));
455+
assert_eq!(input.next_including_whitespace(), Ok(Token::Ident("foo".into())));
457456
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 4 });
458457
assert_eq!(input.next_including_whitespace(), Ok(Token::WhiteSpace(" ")));
459458
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 5 });
460-
assert_eq!(input.next_including_whitespace(), Ok(Token::Ident(Borrowed("bar"))));
459+
assert_eq!(input.next_including_whitespace(), Ok(Token::Ident("bar".into())));
461460
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 8 });
462461
assert_eq!(input.next_including_whitespace(), Ok(Token::WhiteSpace("\n")));
463462
assert_eq!(input.current_source_location(), SourceLocation { line: 2, column: 1 });
464-
assert_eq!(input.next_including_whitespace(), Ok(Token::Ident(Borrowed("baz"))));
463+
assert_eq!(input.next_including_whitespace(), Ok(Token::Ident("baz".into())));
465464
assert_eq!(input.current_source_location(), SourceLocation { line: 2, column: 4 });
466465
let position = input.position();
467466

@@ -470,7 +469,7 @@ fn line_numbers() {
470469

471470
assert_eq!(input.source_location(position), SourceLocation { line: 2, column: 4 });
472471

473-
assert_eq!(input.next_including_whitespace(), Ok(Token::QuotedString(Borrowed("ab"))));
472+
assert_eq!(input.next_including_whitespace(), Ok(Token::QuotedString("ab".into())));
474473
assert_eq!(input.current_source_location(), SourceLocation { line: 5, column: 3 });
475474
assert!(input.next_including_whitespace().is_err());
476475
}
@@ -679,7 +678,7 @@ impl<'i> DeclarationParser<'i> for JsonParser {
679678
type Declaration = Json;
680679
type Error = ();
681680

682-
fn parse_value<'t>(&mut self, name: Cow<'i, str>, input: &mut Parser<'i, 't>)
681+
fn parse_value<'t>(&mut self, name: CompactCowStr<'i>, input: &mut Parser<'i, 't>)
683682
-> Result<Json, ParseError<'i, ()>> {
684683
let mut value = vec![];
685684
let mut important = false;
@@ -720,7 +719,7 @@ impl<'i> AtRuleParser<'i> for JsonParser {
720719
type AtRule = Json;
721720
type Error = ();
722721

723-
fn parse_prelude<'t>(&mut self, name: Cow<'i, str>, input: &mut Parser<'i, 't>)
722+
fn parse_prelude<'t>(&mut self, name: CompactCowStr<'i>, input: &mut Parser<'i, 't>)
724723
-> Result<AtRuleType<Vec<Json>, Json>, ParseError<'i, ()>> {
725724
Ok(AtRuleType::OptionalBlock(vec![
726725
"at-rule".to_json(),

0 commit comments

Comments
 (0)