Skip to content

Don't heap-allocate a tokenizer for top-level parsing. #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "cssparser"
version = "0.12.0"
version = "0.12.1"
authors = [ "Simon Sapin <[email protected]>" ]

description = "Rust implementation of CSS Syntax Level 3"
Expand Down
16 changes: 7 additions & 9 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ pub struct SourcePosition {
}


/// Like std::borrow::Cow, except:
///
/// * The Owned variant is boxed
/// * The Borrowed variant contains a mutable reference.
/// Like std::borrow::Cow, except the borrowed variant contains a mutable
/// reference.
enum MaybeOwned<'a, T: 'a> {
Owned(Box<T>),
Owned(T),
Borrowed(&'a mut T),
}

Expand All @@ -36,7 +34,7 @@ impl<'a, T> ops::Deref for MaybeOwned<'a, T> {

fn deref<'b>(&'b self) -> &'b T {
match *self {
MaybeOwned::Owned(ref pointer) => &**pointer,
MaybeOwned::Owned(ref t) => t,
MaybeOwned::Borrowed(ref pointer) => &**pointer,
}
}
Expand All @@ -45,15 +43,15 @@ impl<'a, T> ops::Deref for MaybeOwned<'a, T> {
impl<'a, T> ops::DerefMut for MaybeOwned<'a, T> {
fn deref_mut<'b>(&'b mut self) -> &'b mut T {
match *self {
MaybeOwned::Owned(ref mut pointer) => &mut **pointer,
MaybeOwned::Owned(ref mut t) => t,
MaybeOwned::Borrowed(ref mut pointer) => &mut **pointer,
}
}
}

impl<'a, T> Clone for MaybeOwned<'a, T> where T: Clone {
fn clone(&self) -> MaybeOwned<'a, T> {
MaybeOwned::Owned(Box::new((**self).clone()))
MaybeOwned::Owned((**self).clone())
}
}

Expand Down Expand Up @@ -171,7 +169,7 @@ impl<'i, 't> Parser<'i, 't> {
#[inline]
pub fn new(input: &'i str) -> Parser<'i, 'i> {
Parser {
tokenizer: MaybeOwned::Owned(Box::new(Tokenizer::new(input))),
tokenizer: MaybeOwned::Owned(Tokenizer::new(input)),
at_start_of: None,
stop_before: Delimiter::None,
}
Expand Down