style: Move the stylesheet set methods under a macro to reuse it soon.

MozReview-Commit-ID: 50Srw4Mjw18
This commit is contained in:
Emilio Cobos Álvarez 2018-02-08 15:56:26 +01:00
parent a254dc12a4
commit 5e8dd8a2d4
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C

View file

@ -394,28 +394,14 @@ where
invalidations: StylesheetInvalidationSet, invalidations: StylesheetInvalidationSet,
} }
impl<S> DocumentStylesheetSet<S> /// This macro defines methods common to DocumentStylesheetSet and
where /// AuthorStylesheetSet.
S: StylesheetInDocument + PartialEq + 'static, ///
{ /// We could simplify the setup moving invalidations to SheetCollection, but
/// Create a new empty StylesheetSet. /// that would imply not sharing invalidations across origins of the same
pub fn new() -> Self { /// documents, which is slightly annoying.
Self { macro_rules! sheet_set_methods {
collections: Default::default(), ($set_name:expr) => {
invalidations: StylesheetInvalidationSet::new(),
}
}
/// Returns the number of stylesheets in the set.
pub fn len(&self) -> usize {
self.collections.iter_origins().fold(0, |s, (item, _)| s + item.len())
}
/// Returns the `index`th stylesheet in the set for the given origin.
pub fn get(&self, origin: Origin, index: usize) -> Option<&S> {
self.collections.borrow_for_origin(&origin).get(index)
}
fn collect_invalidations_for( fn collect_invalidations_for(
&mut self, &mut self,
device: Option<&Device>, device: Option<&Device>,
@ -436,10 +422,10 @@ where
sheet: S, sheet: S,
guard: &SharedRwLockReadGuard guard: &SharedRwLockReadGuard
) { ) {
debug!("DocumentStylesheetSet::append_stylesheet"); debug!(concat!($set_name, "::append_stylesheet"));
self.collect_invalidations_for(device, &sheet, guard); self.collect_invalidations_for(device, &sheet, guard);
let origin = sheet.contents(guard).origin; let collection = self.collection_for(&sheet, guard);
self.collections.borrow_mut_for_origin(&origin).append(sheet); collection.append(sheet);
} }
/// Prepend a new stylesheet to the current set. /// Prepend a new stylesheet to the current set.
@ -449,11 +435,11 @@ where
sheet: S, sheet: S,
guard: &SharedRwLockReadGuard guard: &SharedRwLockReadGuard
) { ) {
debug!("DocumentStylesheetSet::prepend_stylesheet"); debug!(concat!($set_name, "::prepend_stylesheet"));
self.collect_invalidations_for(device, &sheet, guard); self.collect_invalidations_for(device, &sheet, guard);
let origin = sheet.contents(guard).origin; let collection = self.collection_for(&sheet, guard);
self.collections.borrow_mut_for_origin(&origin).prepend(sheet) collection.prepend(sheet);
} }
/// Insert a given stylesheet before another stylesheet in the document. /// Insert a given stylesheet before another stylesheet in the document.
@ -464,13 +450,11 @@ where
before_sheet: S, before_sheet: S,
guard: &SharedRwLockReadGuard, guard: &SharedRwLockReadGuard,
) { ) {
debug!("DocumentStylesheetSet::insert_stylesheet_before"); debug!(concat!($set_name, "::insert_stylesheet_before"));
self.collect_invalidations_for(device, &sheet, guard); self.collect_invalidations_for(device, &sheet, guard);
let origin = sheet.contents(guard).origin; let collection = self.collection_for(&sheet, guard);
self.collections collection.insert_before(sheet, &before_sheet);
.borrow_mut_for_origin(&origin)
.insert_before(sheet, &before_sheet)
} }
/// Remove a given stylesheet from the set. /// Remove a given stylesheet from the set.
@ -480,11 +464,46 @@ where
sheet: S, sheet: S,
guard: &SharedRwLockReadGuard, guard: &SharedRwLockReadGuard,
) { ) {
debug!("StylesheetSet::remove_stylesheet"); debug!(concat!($set_name, "::remove_stylesheet"));
self.collect_invalidations_for(device, &sheet, guard); self.collect_invalidations_for(device, &sheet, guard);
let collection = self.collection_for(&sheet, guard);
collection.remove(&sheet)
}
}
}
impl<S> DocumentStylesheetSet<S>
where
S: StylesheetInDocument + PartialEq + 'static,
{
/// Create a new empty StylesheetSet.
pub fn new() -> Self {
Self {
collections: Default::default(),
invalidations: StylesheetInvalidationSet::new(),
}
}
fn collection_for(
&mut self,
sheet: &S,
guard: &SharedRwLockReadGuard,
) -> &mut SheetCollection<S> {
let origin = sheet.contents(guard).origin; let origin = sheet.contents(guard).origin;
self.collections.borrow_mut_for_origin(&origin).remove(&sheet) self.collections.borrow_mut_for_origin(&origin)
}
sheet_set_methods!("DocumentStylesheetSet");
/// Returns the number of stylesheets in the set.
pub fn len(&self) -> usize {
self.collections.iter_origins().fold(0, |s, (item, _)| s + item.len())
}
/// Returns the `index`th stylesheet in the set for the given origin.
pub fn get(&self, origin: Origin, index: usize) -> Option<&S> {
self.collections.borrow_for_origin(&origin).get(index)
} }
/// Returns whether the given set has changed from the last flush. /// Returns whether the given set has changed from the last flush.