stylo: Add uses of fallible Vec, SmallVec and HashMap facilities.

Bug: 1395064
Reviewed-by: emilio
This commit is contained in:
Julian Seward 2017-09-10 12:25:01 +02:00 committed by Emilio Cobos Álvarez
parent 815fb6b058
commit c85633f48e
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
10 changed files with 125 additions and 57 deletions

View file

@ -9,8 +9,10 @@ use {Atom, LocalName};
use applicable_declarations::ApplicableDeclarationBlock;
use context::QuirksMode;
use dom::TElement;
use fallible::FallibleVec;
use hash::{HashMap, HashSet};
use hash::map as hash_map;
use hashglobe::FailedAllocationError;
use pdqsort::sort_by;
use precomputed_hash::PrecomputedHash;
use rule_tree::CascadeLevel;
@ -272,18 +274,20 @@ impl SelectorMap<Rule> {
impl<T: SelectorMapEntry> SelectorMap<T> {
/// Inserts into the correct hash, trying id, class, and localname.
pub fn insert(&mut self, entry: T, quirks_mode: QuirksMode) {
pub fn insert(
&mut self,
entry: T,
quirks_mode: QuirksMode
) -> Result<(), FailedAllocationError> {
self.count += 1;
let vector = match find_bucket(entry.selector()) {
Bucket::ID(id) => {
self.id_hash
.entry(id.clone(), quirks_mode)
self.id_hash.try_entry(id.clone(), quirks_mode)?
.or_insert_with(SmallVec::new)
}
Bucket::Class(class) => {
self.class_hash
.entry(class.clone(), quirks_mode)
self.class_hash.try_entry(class.clone(), quirks_mode)?
.or_insert_with(SmallVec::new)
}
Bucket::LocalName { name, lower_name } => {
@ -300,12 +304,11 @@ impl<T: SelectorMapEntry> SelectorMap<T> {
// subsequent selector matching work will filter them out.
if name != lower_name {
self.local_name_hash
.entry(lower_name.clone())
.try_entry(lower_name.clone())?
.or_insert_with(SmallVec::new)
.push(entry.clone());
.try_push(entry.clone())?;
}
self.local_name_hash
.entry(name.clone())
self.local_name_hash.try_entry(name.clone())?
.or_insert_with(SmallVec::new)
}
Bucket::Universal => {
@ -313,7 +316,7 @@ impl<T: SelectorMapEntry> SelectorMap<T> {
}
};
vector.push(entry);
vector.try_push(entry)
}
/// Looks up entries by id, class, local name, and other (in order).
@ -510,6 +513,18 @@ impl<V: 'static> MaybeCaseInsensitiveHashMap<Atom, V> {
self.0.entry(key)
}
/// HashMap::try_entry
pub fn try_entry(
&mut self,
mut key: Atom,
quirks_mode: QuirksMode
) -> Result<hash_map::Entry<Atom, V>, FailedAllocationError> {
if quirks_mode == QuirksMode::Quirks {
key = key.to_ascii_lowercase()
}
self.0.try_entry(key)
}
/// HashMap::iter
pub fn iter(&self) -> hash_map::Iter<Atom, V> {
self.0.iter()