From a5b524d5590f84428c61895b418eda7024c8e600 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Mon, 4 Jul 2016 16:30:40 +0200 Subject: [PATCH] Move util::vec::ForgetfulSink to style::sink and simplify it --- components/script/dom/element.rs | 6 +-- components/script/layout_wrapper.rs | 6 +-- components/servo/Cargo.lock | 1 - components/style/dom.rs | 4 +- components/style/lib.rs | 1 + components/style/matching.rs | 2 +- components/style/selector_matching.rs | 9 ++-- components/style/sink.rs | 40 +++++++++++++++ components/util/Cargo.toml | 1 - components/util/lib.rs | 1 - components/util/vec.rs | 71 --------------------------- ports/cef/Cargo.lock | 1 - ports/geckolib/Cargo.lock | 1 - ports/geckolib/wrapper.rs | 3 +- 14 files changed, 58 insertions(+), 89 deletions(-) create mode 100644 components/style/sink.rs diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index 479a73dee86..d9535663d46 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -75,7 +75,6 @@ use selectors::matching::{DeclarationBlock, ElementFlags, matches}; use selectors::matching::{HAS_SLOW_SELECTOR, HAS_EDGE_CHILD_SELECTOR, HAS_SLOW_SELECTOR_LATER_SIBLINGS}; use selectors::matching::{common_style_affecting_attributes, rare_style_affecting_attributes}; use selectors::parser::{AttrSelector, NamespaceConstraint, parse_author_origin_selector_list_from_str}; -use smallvec::VecLike; use std::ascii::AsciiExt; use std::borrow::Cow; use std::cell::{Cell, Ref}; @@ -91,6 +90,7 @@ use style::properties::DeclaredValue; use style::properties::longhands::{self, background_image, border_spacing, font_family, overflow_x, font_size}; use style::properties::{PropertyDeclaration, PropertyDeclarationBlock, parse_style_attribute}; use style::selector_impl::{NonTSPseudoClass, ServoSelectorImpl}; +use style::sink::Push; use style::values::CSSFloat; use style::values::specified::{self, CSSColor, CSSRGBA, LengthOrPercentage}; @@ -275,7 +275,7 @@ pub trait LayoutElementHelpers { #[allow(unsafe_code)] unsafe fn synthesize_presentational_hints_for_legacy_attributes(&self, &mut V) - where V: VecLike>>; + where V: Push>>; #[allow(unsafe_code)] unsafe fn get_colspan(self) -> u32; #[allow(unsafe_code)] @@ -308,7 +308,7 @@ impl LayoutElementHelpers for LayoutJS { #[allow(unsafe_code)] unsafe fn synthesize_presentational_hints_for_legacy_attributes(&self, hints: &mut V) - where V: VecLike>> + where V: Push>> { #[inline] fn from_declaration(rule: PropertyDeclaration) -> DeclarationBlock> { diff --git a/components/script/layout_wrapper.rs b/components/script/layout_wrapper.rs index 681fdab9c46..779c37a5d52 100644 --- a/components/script/layout_wrapper.rs +++ b/components/script/layout_wrapper.rs @@ -49,7 +49,6 @@ use script_layout_interface::{HTMLCanvasData, LayoutNodeType, TrustedNodeAddress use script_layout_interface::{OpaqueStyleAndLayoutData, PartialStyleAndLayoutData}; use selectors::matching::{DeclarationBlock, ElementFlags}; use selectors::parser::{AttrSelector, NamespaceConstraint}; -use smallvec::VecLike; use std::marker::PhantomData; use std::mem::{transmute, transmute_copy}; use string_cache::{Atom, BorrowedAtom, BorrowedNamespace, Namespace}; @@ -63,6 +62,7 @@ use style::refcell::{Ref, RefCell, RefMut}; use style::restyle_hints::ElementSnapshot; use style::selector_impl::{NonTSPseudoClass, ServoSelectorImpl}; use style::servo::{PrivateStyleData, SharedStyleContext}; +use style::sink::Push; use url::Url; use util::str::is_whitespace; @@ -360,7 +360,7 @@ pub struct ServoLayoutElement<'le> { impl<'le> PresentationalHintsSynthetizer for ServoLayoutElement<'le> { fn synthesize_presentational_hints_for_legacy_attributes(&self, hints: &mut V) - where V: VecLike>> + where V: Push>> { unsafe { self.element.synthesize_presentational_hints_for_legacy_attributes(hints); @@ -995,5 +995,5 @@ impl <'le> ::selectors::Element for ServoThreadSafeLayoutElement<'le> { impl<'le> PresentationalHintsSynthetizer for ServoThreadSafeLayoutElement<'le> { fn synthesize_presentational_hints_for_legacy_attributes(&self, _hints: &mut V) - where V: VecLike>> {} + where V: Push>> {} } diff --git a/components/servo/Cargo.lock b/components/servo/Cargo.lock index b886ef4296f..4c68a576fe3 100644 --- a/components/servo/Cargo.lock +++ b/components/servo/Cargo.lock @@ -2471,7 +2471,6 @@ dependencies = [ "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", "serde_macros 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "xdg 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/components/style/dom.rs b/components/style/dom.rs index 4f7eeaff169..11cc5f93a0e 100644 --- a/components/style/dom.rs +++ b/components/style/dom.rs @@ -15,7 +15,7 @@ use restyle_hints::{ElementSnapshot, RESTYLE_DESCENDANTS, RESTYLE_LATER_SIBLINGS use selector_impl::{ElementExt, SelectorImplExt}; use selectors::Element; use selectors::matching::DeclarationBlock; -use smallvec::VecLike; +use sink::Push; use std::ops::BitOr; use std::sync::Arc; use string_cache::{Atom, Namespace}; @@ -194,7 +194,7 @@ pub trait TDocument : Sized + Copy + Clone { pub trait PresentationalHintsSynthetizer { fn synthesize_presentational_hints_for_legacy_attributes(&self, hints: &mut V) - where V: VecLike>>; + where V: Push>>; } pub trait TElement : Sized + Copy + Clone + ElementExt + PresentationalHintsSynthetizer { diff --git a/components/style/lib.rs b/components/style/lib.rs index ae1e8c84d7c..54510250eda 100644 --- a/components/style/lib.rs +++ b/components/style/lib.rs @@ -88,6 +88,7 @@ pub mod selector_impl; pub mod selector_matching; pub mod sequential; pub mod servo; +pub mod sink; pub mod stylesheets; pub mod traversal; #[macro_use] diff --git a/components/style/matching.rs b/components/style/matching.rs index 7f865cf9a72..d3dd3742d3d 100644 --- a/components/style/matching.rs +++ b/components/style/matching.rs @@ -17,6 +17,7 @@ use selectors::Element; use selectors::bloom::BloomFilter; use selectors::matching::{CommonStyleAffectingAttributeMode, CommonStyleAffectingAttributes}; use selectors::matching::{common_style_affecting_attributes, rare_style_affecting_attributes}; +use sink::ForgetfulSink; use smallvec::SmallVec; use std::collections::HashMap; use std::hash::{BuildHasherDefault, Hash, Hasher}; @@ -26,7 +27,6 @@ use string_cache::{Atom, Namespace}; use util::arc_ptr_eq; use util::cache::{LRUCache, SimpleHashCache}; use util::opts; -use util::vec::ForgetfulSink; fn create_common_style_affecting_attributes_from_element(element: &E) -> CommonStyleAffectingAttributes { diff --git a/components/style/selector_matching.rs b/components/style/selector_matching.rs index 35a38d5ed14..74718d4b69b 100644 --- a/components/style/selector_matching.rs +++ b/components/style/selector_matching.rs @@ -18,6 +18,7 @@ use selectors::bloom::BloomFilter; use selectors::matching::DeclarationBlock as GenericDeclarationBlock; use selectors::matching::{Rule, SelectorMap}; use selectors::parser::SelectorImpl; +use sink::Push; use smallvec::VecLike; use std::collections::HashMap; use std::hash::BuildHasherDefault; @@ -389,7 +390,7 @@ impl Stylist { applicable_declarations: &mut V) -> bool where E: Element + PresentationalHintsSynthetizer, - V: VecLike { + V: Push + VecLike { assert!(!self.is_device_dirty); assert!(style_attribute.is_none() || pseudo_element.is_none(), "Style attributes do not apply to pseudo-elements"); @@ -430,7 +431,8 @@ impl Stylist { // Step 4: Normal style attributes. style_attribute.map(|sa| { shareable = false; - applicable_declarations.push( + Push::push( + applicable_declarations, GenericDeclarationBlock::from_declarations(sa.normal.clone())) }); @@ -443,7 +445,8 @@ impl Stylist { // Step 6: `!important` style attributes. style_attribute.map(|sa| { shareable = false; - applicable_declarations.push( + Push::push( + applicable_declarations, GenericDeclarationBlock::from_declarations(sa.important.clone())) }); diff --git a/components/style/sink.rs b/components/style/sink.rs new file mode 100644 index 00000000000..33aa26ac1af --- /dev/null +++ b/components/style/sink.rs @@ -0,0 +1,40 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use smallvec::{Array, SmallVec}; +use std::marker::PhantomData; + +pub trait Push { + fn push(&mut self, value: T); +} + +impl Push for Vec { + fn push(&mut self, value: T) { + Vec::push(self, value); + } +} + +impl Push for SmallVec { + fn push(&mut self, value: A::Item) { + SmallVec::push(self, value); + } +} + +pub struct ForgetfulSink(bool, PhantomData); + +impl ForgetfulSink { + pub fn new() -> Self { + ForgetfulSink(true, PhantomData) + } + + pub fn is_empty(&self) -> bool { + self.0 + } +} + +impl Push for ForgetfulSink { + fn push(&mut self, _value: T) { + self.0 = false; + } +} diff --git a/components/util/Cargo.toml b/components/util/Cargo.toml index 83b23c78c87..b693a0f8c5b 100644 --- a/components/util/Cargo.toml +++ b/components/util/Cargo.toml @@ -31,7 +31,6 @@ rand = "0.3" rustc-serialize = "0.3" serde = {version = "0.7.11", optional = true} serde_macros = {version = "0.7.11", optional = true} -smallvec = "0.1" url = "1.0.0" [target.'cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android")))'.dependencies] diff --git a/components/util/lib.rs b/components/util/lib.rs index 46c194ca91a..3a5c362f443 100644 --- a/components/util/lib.rs +++ b/components/util/lib.rs @@ -27,7 +27,6 @@ extern crate num_traits; extern crate rand; extern crate rustc_serialize; #[cfg(feature = "servo")] extern crate serde; -extern crate smallvec; extern crate url; #[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android")))] extern crate xdg; diff --git a/components/util/vec.rs b/components/util/vec.rs index 639e1c260da..2aa6b7d4a41 100644 --- a/components/util/vec.rs +++ b/components/util/vec.rs @@ -2,10 +2,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use std::marker::PhantomData; -use std::ops; -use super::smallvec::VecLike; - // TODO(pcwalton): Speed up with SIMD, or better yet, find some way to not do this. pub fn byte_swap(data: &mut [u8]) { let length = data.len(); @@ -18,70 +14,3 @@ pub fn byte_swap(data: &mut [u8]) { i += 4; } } - -/// A `VecLike` that only tracks whether or not something was ever pushed to it. -pub struct ForgetfulSink { - empty: bool, - _data: PhantomData, -} - -impl ForgetfulSink { - pub fn new() -> ForgetfulSink { - ForgetfulSink { - empty: true, - _data: PhantomData, - } - } - - pub fn is_empty(&self) -> bool { - self.empty - } -} - -impl ops::Deref for ForgetfulSink { - type Target = [T]; - fn deref(&self) -> &[T] { - unreachable!() - } -} - -impl ops::DerefMut for ForgetfulSink { - fn deref_mut(&mut self) -> &mut [T] { - unreachable!() - } -} - -macro_rules! impl_index { - ($index_type: ty, $output_type: ty) => { - impl ops::Index<$index_type> for ForgetfulSink { - type Output = $output_type; - fn index(&self, _index: $index_type) -> &$output_type { - unreachable!() - } - } - - impl ops::IndexMut<$index_type> for ForgetfulSink { - fn index_mut(&mut self, _index: $index_type) -> &mut $output_type { - unreachable!() - } - } - } -} - -impl_index!(usize, T); -impl_index!(ops::Range, [T]); -impl_index!(ops::RangeFrom, [T]); -impl_index!(ops::RangeTo, [T]); -impl_index!(ops::RangeFull, [T]); - -impl VecLike for ForgetfulSink { - #[inline] - fn len(&self) -> usize { - unreachable!() - } - - #[inline] - fn push(&mut self, _value: T) { - self.empty = false; - } -} diff --git a/ports/cef/Cargo.lock b/ports/cef/Cargo.lock index 7150e137bd1..dc0b6af49f6 100644 --- a/ports/cef/Cargo.lock +++ b/ports/cef/Cargo.lock @@ -2340,7 +2340,6 @@ dependencies = [ "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", "serde_macros 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "xdg 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/ports/geckolib/Cargo.lock b/ports/geckolib/Cargo.lock index 7b94768e030..ddd1c911aab 100644 --- a/ports/geckolib/Cargo.lock +++ b/ports/geckolib/Cargo.lock @@ -577,7 +577,6 @@ dependencies = [ "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", "serde_macros 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "xdg 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/ports/geckolib/wrapper.rs b/ports/geckolib/wrapper.rs index 83dbac01064..2c32643c008 100644 --- a/ports/geckolib/wrapper.rs +++ b/ports/geckolib/wrapper.rs @@ -50,6 +50,7 @@ use style::properties::{PropertyDeclaration, PropertyDeclarationBlock}; use style::refcell::{Ref, RefCell, RefMut}; use style::restyle_hints::ElementSnapshot; use style::selector_impl::ElementExt; +use style::sink::Push; #[allow(unused_imports)] // Used in commented-out code. use url::Url; @@ -377,7 +378,7 @@ impl<'le> TElement for GeckoElement<'le> { impl<'le> PresentationalHintsSynthetizer for GeckoElement<'le> { fn synthesize_presentational_hints_for_legacy_attributes(&self, _hints: &mut V) - where V: VecLike>> + where V: Push>> { // FIXME(bholley) - Need to implement this. }