mirror of
https://github.com/servo/servo.git
synced 2025-07-23 07:13:52 +01:00
style: Use an enumerated array for per-pseudo maps.
This avoids random HashMaps. MozReview-Commit-ID: LQeZrLsoOnl Signed-off-by: Emilio Cobos Álvarez <emilio@crisal.io>
This commit is contained in:
parent
69f02f4f7f
commit
faa8646e0f
6 changed files with 294 additions and 155 deletions
|
@ -9,7 +9,7 @@
|
|||
use cssparser::{Parser as CssParser, ParserInput};
|
||||
use selectors::Element;
|
||||
use selectors::parser::SelectorList;
|
||||
use std::fmt::Debug;
|
||||
use std::fmt::{self, Debug};
|
||||
use style_traits::ParseError;
|
||||
use stylesheets::{Origin, Namespaces, UrlExtraData};
|
||||
|
||||
|
@ -121,20 +121,81 @@ pub trait ElementExt: Element<Impl=SelectorImpl> + Debug {
|
|||
fn matches_user_and_author_rules(&self) -> bool;
|
||||
}
|
||||
|
||||
impl SelectorImpl {
|
||||
/// A helper to traverse each precomputed pseudo-element, executing `fun` on
|
||||
/// it.
|
||||
///
|
||||
/// The optimization comment in `each_eagerly_cascaded_pseudo_element` also
|
||||
/// applies here.
|
||||
#[inline]
|
||||
pub fn each_precomputed_pseudo_element<F>(mut fun: F)
|
||||
where F: FnMut(PseudoElement),
|
||||
{
|
||||
Self::each_simple_pseudo_element(|pseudo| {
|
||||
if pseudo.is_precomputed() {
|
||||
fun(pseudo)
|
||||
}
|
||||
})
|
||||
/// A per-functional-pseudo map, from a given pseudo to a `T`.
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
pub struct PerPseudoElementMap<T> {
|
||||
entries: [Option<T>; SIMPLE_PSEUDO_COUNT],
|
||||
}
|
||||
|
||||
impl<T> Default for PerPseudoElementMap<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
entries: PseudoElement::simple_pseudo_none_array(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Debug for PerPseudoElementMap<T>
|
||||
where
|
||||
T: Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str("[")?;
|
||||
let mut first = true;
|
||||
for entry in self.entries.iter() {
|
||||
if !first {
|
||||
f.write_str(", ")?;
|
||||
}
|
||||
first = false;
|
||||
entry.fmt(f)?;
|
||||
}
|
||||
f.write_str("]")
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> PerPseudoElementMap<T> {
|
||||
/// Get an entry in the map.
|
||||
pub fn get(&self, pseudo: &PseudoElement) -> Option<&T> {
|
||||
let index = match pseudo.simple_index() {
|
||||
Some(i) => i,
|
||||
None => return None,
|
||||
};
|
||||
self.entries[index].as_ref()
|
||||
}
|
||||
|
||||
/// Clear this enumerated array.
|
||||
pub fn clear(&mut self) {
|
||||
*self = Self::default();
|
||||
}
|
||||
|
||||
/// Set an entry value.
|
||||
///
|
||||
/// Returns an error if the element is not a simple pseudo.
|
||||
pub fn set(&mut self, pseudo: &PseudoElement, value: T) -> Result<(), ()> {
|
||||
let index = match pseudo.simple_index() {
|
||||
Some(i) => i,
|
||||
None => return Err(()),
|
||||
};
|
||||
self.entries[index] = Some(value);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get an entry for `pseudo`, or create it with calling `f`.
|
||||
pub fn get_or_insert_with<F>(
|
||||
&mut self,
|
||||
pseudo: &PseudoElement,
|
||||
f: F,
|
||||
) -> Result<&mut T, ()>
|
||||
where
|
||||
F: FnOnce() -> T,
|
||||
{
|
||||
let index = match pseudo.simple_index() {
|
||||
Some(i) => i,
|
||||
None => return Err(()),
|
||||
};
|
||||
if self.entries[index].is_none() {
|
||||
self.entries[index] = Some(f());
|
||||
}
|
||||
Ok(self.entries[index].as_mut().unwrap())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue