style: Make the cascade data cache generic

We're going to use it both for UA sheets and for author styles in Shadow
DOM.

Differential Revision: https://phabricator.services.mozilla.com/D107265
This commit is contained in:
Oriol Brufau 2023-05-16 09:47:54 +02:00
parent b38517757f
commit 11153c63fa
2 changed files with 158 additions and 78 deletions

View file

@ -184,7 +184,9 @@ pub struct SheetCollectionFlusher<'a, S>
where
S: StylesheetInDocument + PartialEq + 'static,
{
iter: slice::IterMut<'a, StylesheetSetEntry<S>>,
// TODO: This can be made an iterator again once
// https://github.com/rust-lang/rust/pull/82771 lands on stable.
entries: &'a mut [StylesheetSetEntry<S>],
validity: DataValidity,
dirty: bool,
}
@ -204,32 +206,42 @@ where
pub fn data_validity(&self) -> DataValidity {
self.validity
}
/// Returns an iterator over the remaining list of sheets to consume.
pub fn sheets<'b>(&'b self) -> impl Iterator<Item = &'b S> {
self.entries.iter().map(|entry| &entry.sheet)
}
}
impl<'a, S> Iterator for SheetCollectionFlusher<'a, S>
impl<'a, S> SheetCollectionFlusher<'a, S>
where
S: StylesheetInDocument + PartialEq + 'static,
{
type Item = (&'a S, SheetRebuildKind);
fn next(&mut self) -> Option<Self::Item> {
loop {
let potential_sheet = self.iter.next()?;
/// Iterates over all sheets and values that we have to invalidate.
///
/// TODO(emilio): This would be nicer as an iterator but we can't do that
/// until https://github.com/rust-lang/rust/pull/82771 stabilizes.
///
/// Since we don't have a good use-case for partial iteration, this does the
/// trick for now.
pub fn each(self, mut callback: impl FnMut(&S, SheetRebuildKind) -> bool) {
for potential_sheet in self.entries.iter_mut() {
let committed = mem::replace(&mut potential_sheet.committed, true);
if !committed {
let rebuild_kind = if !committed {
// If the sheet was uncommitted, we need to do a full rebuild
// anyway.
return Some((&potential_sheet.sheet, SheetRebuildKind::Full));
}
let rebuild_kind = match self.validity {
DataValidity::Valid => continue,
DataValidity::CascadeInvalid => SheetRebuildKind::CascadeOnly,
DataValidity::FullyInvalid => SheetRebuildKind::Full,
SheetRebuildKind::Full
} else {
match self.validity {
DataValidity::Valid => continue,
DataValidity::CascadeInvalid => SheetRebuildKind::CascadeOnly,
DataValidity::FullyInvalid => SheetRebuildKind::Full,
}
};
return Some((&potential_sheet.sheet, rebuild_kind));
if !callback(&potential_sheet.sheet, rebuild_kind) {
return;
}
}
}
}
@ -357,7 +369,7 @@ where
let validity = mem::replace(&mut self.data_validity, DataValidity::Valid);
SheetCollectionFlusher {
iter: self.entries.iter_mut(),
entries: &mut self.entries,
dirty,
validity,
}