use std::{
cell::{Cell, OnceCell, RefCell},
sync::{Arc, Weak},
};
#[cfg(test)]
use crate::layout::layout_result::SubtreeHolds;
use crate::{
core::layout_dirty::LayoutWatch,
layout::{Constraints, layout_result::LayoutResult},
};
pub struct LayoutCache {
inner: RefCell<CachedLayouts>,
validated: Cell<Option<ValidatedLayout>>,
watch: OnceCell<Arc<LayoutWatch>>,
pub(crate) tracking_generation: Cell<u64>,
pub(crate) inspected_generation: Cell<u64>,
pub(crate) guards_generation: Cell<u64>,
override_clear_generation: Cell<u64>,
subtree: Cell<u16>,
local_dirty: Cell<bool>,
descendant_dirty: Cell<bool>,
prepared: Cell<Option<Box<PreparedServe>>>,
ticked: Cell<Option<Box<TickedClean>>>,
descendants_match_front: Cell<bool>,
}
#[derive(Clone, Copy, Default)]
pub(crate) struct LayoutSubtree {
pub(crate) text_input: bool,
pub(crate) select: bool,
pub(crate) timeline: bool,
pub(crate) scroll: bool,
pub(crate) canvas: bool,
pub(crate) resource_images: bool,
pub(crate) resource_svgs: bool,
pub(crate) loading_images: bool,
pub(crate) refs: bool,
pub(crate) layers: bool,
pub(crate) droppable: bool,
pub(crate) animations: bool,
pub(crate) slots: bool,
}
impl LayoutSubtree {
fn flags(&self) -> [bool; 13] {
[
self.text_input,
self.select,
self.timeline,
self.scroll,
self.canvas,
self.resource_images,
self.resource_svgs,
self.loading_images,
self.refs,
self.layers,
self.droppable,
self.animations,
self.slots,
]
}
fn pack(&self) -> u16 {
self
.flags()
.iter()
.enumerate()
.fold(0, |bits, (at, on)| bits | (u16::from(*on) << at))
}
fn unpack(bits: u16) -> Self {
let on = |at: u16| bits & (1 << at) != 0;
Self {
text_input: on(0),
select: on(1),
timeline: on(2),
scroll: on(3),
canvas: on(4),
resource_images: on(5),
resource_svgs: on(6),
loading_images: on(7),
refs: on(8),
layers: on(9),
droppable: on(10),
animations: on(11),
slots: on(12),
}
}
pub(crate) fn indexed(&self) -> bool {
self.refs || self.layers || self.text_input || self.select
}
}
struct ValidatedLayout {
generation: u64,
result: Weak<LayoutResult>,
}
struct CachedLayout {
constraints: Constraints,
result: Arc<LayoutResult>,
}
struct TwoLayouts {
slots: [Option<CachedLayout>; 2],
front: usize,
}
#[derive(Default)]
enum CachedLayouts {
#[default]
Empty,
One(CachedLayout),
Two(Box<TwoLayouts>),
}
impl CachedLayouts {
fn front(&self) -> Option<&CachedLayout> {
match self {
Self::Empty => None,
Self::One(cached) => Some(cached),
Self::Two(two) => two.slots[two.front].as_ref(),
}
}
fn front_index(&self) -> usize {
match self {
Self::Two(two) => two.front,
_ => 0,
}
}
fn set_front(&mut self, index: usize) {
if let Self::Two(two) = self {
two.front = index;
}
}
fn entries(&self) -> [Option<&CachedLayout>; 2] {
match self {
Self::Empty => [None, None],
Self::One(cached) => [Some(cached), None],
Self::Two(two) => [two.slots[0].as_ref(), two.slots[1].as_ref()],
}
}
fn entry_mut(&mut self, index: usize) -> Option<&mut CachedLayout> {
match self {
Self::Empty => None,
Self::One(cached) => Some(cached),
Self::Two(two) => two.slots[index].as_mut(),
}
}
fn find(&self, constraints: Constraints) -> Option<usize> {
self
.entries()
.iter()
.position(|slot| slot.is_some_and(|cached| cached.constraints == constraints))
}
fn clear(&mut self) {
match self {
Self::Two(two) => two.slots = [None, None],
_ => *self = Self::Empty,
}
}
fn store(&mut self, constraints: Constraints, result: Arc<LayoutResult>) {
let index = self.find(constraints);
let entry = CachedLayout { constraints, result };
match self {
Self::Empty => *self = Self::One(entry),
Self::One(cached) if index.is_some() => *cached = entry,
Self::One(_) => {
let Self::One(first) = std::mem::take(self) else {
unreachable!("matched just above");
};
*self = Self::Two(Box::new(TwoLayouts {
slots: [Some(first), Some(entry)],
front: 1,
}));
}
Self::Two(two) => {
let index = index.unwrap_or(1 - two.front);
two.slots[index] = Some(entry);
two.front = index;
}
}
}
}
impl LayoutCache {
pub fn new() -> Self {
Self {
inner: RefCell::new(CachedLayouts::Empty),
validated: Cell::new(None),
watch: OnceCell::new(),
tracking_generation: Cell::new(0),
inspected_generation: Cell::new(0),
guards_generation: Cell::new(0),
override_clear_generation: Cell::new(0),
subtree: Cell::new(0),
local_dirty: Cell::new(false),
descendant_dirty: Cell::new(false),
descendants_match_front: Cell::new(true),
prepared: Cell::new(None),
ticked: Cell::new(None),
}
}
pub(crate) fn subtree(&self) -> LayoutSubtree {
LayoutSubtree::unpack(self.subtree.get())
}
pub(crate) fn set_subtree(&self, subtree: LayoutSubtree) {
self.subtree.set(subtree.pack());
}
pub(crate) fn take_ticked(&self) -> Option<Box<TickedClean>> {
self.ticked.take()
}
pub(crate) fn set_ticked(&self, ticked: Option<Box<TickedClean>>) {
self.ticked.set(ticked);
}
pub(crate) fn ticked_clean(&self) -> bool {
let ticked = self.ticked.take();
let clean = ticked.as_ref().is_some_and(|ticked| ticked.is_current(self.watch()));
self.ticked.set(ticked);
clean
}
pub(crate) fn take_prepared(&self) -> Option<Box<PreparedServe>> {
self.prepared.take()
}
pub(crate) fn set_prepared(&self, prepared: Option<Box<PreparedServe>>) {
self.prepared.set(prepared);
}
pub(crate) fn prepared_from(&self, entry: &Arc<LayoutResult>, text_writes: u64) -> bool {
let prepared = self.prepared.take();
let held = prepared.as_ref().is_some_and(|prepared| {
prepared.text_writes == text_writes
&& (prepared.served == Arc::as_ptr(entry) as usize || Arc::ptr_eq(&prepared.result, entry))
});
self.prepared.set(prepared);
held
}
pub(crate) fn prepared_now(&self, result: &Arc<LayoutResult>, text_writes: u64) {
if !(result.holds.scroll || result.holds.text) {
self.prepared.set(None);
return;
}
self.prepared.set(Some(Box::new(PreparedServe {
served: Arc::as_ptr(result) as usize,
text_writes,
result: result.clone(),
})));
}
#[inline]
pub(crate) fn watch(&self) -> &Arc<LayoutWatch> {
match self.watch.get() {
Some(watch) => watch,
None => self.make_watch(),
}
}
#[cold]
#[inline(never)]
fn make_watch(&self) -> &Arc<LayoutWatch> {
self.watch.get_or_init(LayoutWatch::new)
}
#[cfg(test)]
pub(crate) fn has_watch(&self) -> bool {
self.watch.get().is_some()
}
pub(crate) fn mark_watch(&self) {
if let Some(watch) = self.watch.get() {
watch.mark();
}
}
pub(crate) fn adopt_watch_from(&self, old: &Self) {
if self.watch.get().is_none()
&& let Some(watch) = old.watch.get()
{
watch.detach_parent();
let _ = self.watch.set(watch.clone());
}
}
#[cfg(test)]
pub fn get(&self, constraints: Constraints) -> Option<LayoutResult> {
self.get_shared(constraints).map(|result| result.as_ref().clone())
}
pub(crate) fn get_shared(&self, constraints: Constraints) -> Option<Arc<LayoutResult>> {
if self.is_dirty() {
return None;
}
let mut entries = self.inner.borrow_mut();
let index = entries.find(constraints)?;
if index != entries.front_index() {
entries.set_front(index);
self.descendants_match_front.set(false);
}
Some(entries.front()?.result.clone())
}
pub(crate) fn matches_validated(&self, result: &Arc<LayoutResult>) -> bool {
let validated = self.validated.take();
let matches = validated.as_ref().is_some_and(|validated| {
validated.generation == self.watch().generation() && std::ptr::eq(validated.result.as_ptr(), Arc::as_ptr(result))
});
self.validated.set(validated);
matches
}
pub(crate) fn remember_validation(&self, result: &Arc<LayoutResult>) {
self.validated.set(Some(ValidatedLayout {
generation: self.watch().generation(),
result: Arc::downgrade(result),
}));
}
pub(crate) fn record_validation(&self, constraints: Constraints, generation: u64) {
let mut entries = self.inner.borrow_mut();
if let Some(index) = entries.find(constraints) {
let result = &mut entries.entry_mut(index).unwrap().result;
if result.generation != generation {
Arc::make_mut(result).generation = generation;
}
}
}
pub(crate) fn contains(&self, constraints: Constraints) -> bool {
if self.is_dirty() {
return false;
}
self.inner.borrow().find(constraints).is_some()
}
pub(crate) fn record_reuse(&self, constraints: Constraints, result: &Arc<LayoutResult>) {
let mut entries = self.inner.borrow_mut();
match entries.find(constraints) {
Some(index) if index == entries.front_index() => return,
Some(index) => entries.set_front(index),
None => entries.store(constraints, result.clone()),
}
self.descendants_match_front.set(false);
}
pub(crate) fn get_repairable(&self, constraints: Constraints) -> Option<Arc<LayoutResult>> {
if !self.descendants_match_front.get() {
return None;
}
let entries = self.inner.borrow();
let front = entries.front()?;
(front.constraints == constraints).then(|| front.result.clone())
}
pub(crate) fn constraints(&self) -> Option<Constraints> {
self.inner.borrow().front().map(|cached| cached.constraints)
}
pub(crate) fn has_cached_result(&self) -> bool {
self.inner.borrow().front().is_some()
}
pub(crate) fn cached_entry(&self) -> Option<(Constraints, crate::layout::Size)> {
self
.inner
.borrow()
.front()
.map(|cached| (cached.constraints, cached.result.size))
}
pub(crate) fn preserve_from(&self, old: &Self) {
self.mark_watch();
self.override_clear_generation.set(old.override_clear_generation.get());
*self.inner.borrow_mut() = old.inner.take();
self.descendants_match_front.set(old.descendants_match_front.get());
self.local_dirty.set(self.local_dirty.get() || old.local_dirty.get());
self
.descendant_dirty
.set(self.descendant_dirty.get() || old.descendant_dirty.get());
}
#[cfg(test)]
pub fn store(&self, constraints: Constraints, result: LayoutResult) {
self.store_shared(constraints, Arc::new(result));
}
pub(crate) fn store_shared(&self, constraints: Constraints, result: Arc<LayoutResult>) {
let mut borrow = self.inner.borrow_mut();
if self.is_dirty() {
borrow.clear();
}
borrow.store(constraints, result);
self.descendants_match_front.set(true);
self.clear_dirty();
}
pub fn invalidate(&self) {
self.mark_watch();
self.validated.take();
*self.inner.borrow_mut() = CachedLayouts::Empty;
self.descendants_match_front.set(true);
self.clear_dirty();
}
pub(crate) fn mark_placed_by_parent(&self) {
self.clear_dirty();
}
pub(crate) fn mark_local_dirty(&self) {
self.mark_watch();
self.local_dirty.set(true);
}
pub(crate) fn mark_descendant_dirty(&self) {
if let Some(watch) = self.watch.get() {
watch.mark_descendant();
}
self.descendant_dirty.set(true);
}
pub(crate) fn needs_dirty_scan(&self) -> bool {
self.inspected_generation.get() != self.watch().generation()
}
pub(crate) fn record_dirty_scan(&self, generation: u64, local: bool, descendant: bool) {
self.local_dirty.set(self.local_dirty.get() || local);
self.descendant_dirty.set(self.descendant_dirty.get() || descendant);
self.inspected_generation.set(generation);
}
pub(crate) fn take_override_cleared(&self, generation: u64) -> bool {
self.override_clear_generation.replace(generation) != generation
}
pub(crate) fn is_local_dirty(&self) -> bool {
self.local_dirty.get()
}
pub(crate) fn is_descendant_dirty(&self) -> bool {
self.descendant_dirty.get()
}
pub(crate) fn is_dirty(&self) -> bool {
self.is_local_dirty() || self.is_descendant_dirty()
}
fn clear_dirty(&self) {
self.local_dirty.set(false);
self.descendant_dirty.set(false);
}
pub(crate) fn estimated_memory_bytes(&self) -> usize {
let borrow = self.inner.borrow();
let cached_bytes = borrow
.entries()
.into_iter()
.flatten()
.map(|cached| cached.result.estimated_memory_bytes() + std::mem::size_of::<[usize; 2]>())
.sum::<usize>();
let slots_bytes = if matches!(*borrow, CachedLayouts::Two(_)) {
std::mem::size_of::<TwoLayouts>()
} else {
0
};
std::mem::size_of::<Self>()
+ std::mem::size_of::<crate::core::layout_dirty::LayoutWatch>()
+ slots_bytes
+ cached_bytes
}
}
pub(crate) fn world_changes() -> u64 {
crate::app::theme::theme_mutations()
}
pub(crate) struct TickedClean {
pub(crate) generation: u64,
pub(crate) epoch: u64,
pub(crate) ticks: u64,
pub(crate) world: u64,
pub(crate) ids: Option<Arc<TickedIds>>,
}
impl TickedClean {
pub(crate) fn is_current(&self, watch: &crate::core::layout_dirty::LayoutWatch) -> bool {
self.generation == watch.generation() && self.ticks == watch.ticks() && self.world == world_changes()
}
}
pub(crate) struct TickedIds {
pub(crate) own: Option<crate::core::NodeId>,
pub(crate) children: Vec<Arc<TickedIds>>,
}
impl TickedIds {
pub(crate) fn each(&self, each: &mut impl FnMut(crate::core::NodeId)) {
if let Some(own) = self.own {
each(own);
}
for child in &self.children {
child.each(each);
}
}
}
pub(crate) struct PreparedServe {
pub(crate) served: usize,
pub(crate) text_writes: u64,
pub(crate) result: Arc<LayoutResult>,
}
impl Default for LayoutCache {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::layout::Size;
#[test]
fn dirty_scan_does_not_consume_a_later_notification() {
let cache = LayoutCache::new();
let generation = cache.watch().generation();
cache.watch().mark();
cache.record_dirty_scan(generation, false, false);
assert!(cache.needs_dirty_scan());
}
#[test]
fn a_watch_made_after_marks_is_newer_than_them() {
let cache = LayoutCache::new();
cache.invalidate();
cache.mark_local_dirty();
cache.mark_descendant_dirty();
assert!(!cache.has_watch());
let after_marks = LayoutWatch::new().generation();
assert!(cache.watch().generation() > after_marks);
assert!(cache.needs_dirty_scan());
assert!(cache.is_local_dirty() && cache.is_descendant_dirty());
}
#[test]
fn adopting_a_watch_keeps_one_already_made() {
let parent = LayoutWatch::new();
let old = LayoutCache::new();
assert!(old.watch().set_parent(&parent));
let made = LayoutCache::new();
let own = made.watch().clone();
made.adopt_watch_from(&old);
assert!(Arc::ptr_eq(made.watch(), &own));
let new = LayoutCache::new();
new.adopt_watch_from(&old);
assert!(Arc::ptr_eq(new.watch(), old.watch()));
let before = parent.generation();
new.mark_watch();
assert_eq!(parent.generation(), before, "the old parent hears no more");
}
fn constraints(max_width: f32) -> Constraints {
Constraints {
min_width: 0.0,
min_height: 0.0,
max_width,
max_height: f32::MAX,
}
}
fn result(width: f32) -> LayoutResult {
LayoutResult {
size: Size::new(width, 10.0),
children: vec![],
text_layout: None,
dropped: false,
holds: SubtreeHolds::UNKNOWN,
generation: 0,
sticky_shifts: None,
reach: crate::layout::layout_result::Reach::UNKNOWN,
}
}
#[test]
fn a_validation_holds_for_its_result_until_the_generation_moves() {
let cache = LayoutCache::new();
let validated = Arc::new(result(40.0));
let other = Arc::new(result(40.0));
assert!(!cache.matches_validated(&validated), "nothing validated yet");
cache.remember_validation(&validated);
assert!(cache.matches_validated(&validated));
assert!(cache.matches_validated(&validated), "asking keeps the record");
assert!(!cache.matches_validated(&other), "another snapshot was not validated");
cache.watch().mark();
assert!(!cache.matches_validated(&validated), "a later change voids it");
cache.remember_validation(&validated);
assert!(cache.matches_validated(&validated));
cache.remember_validation(&other);
assert!(!cache.matches_validated(&validated), "the record holds one snapshot");
assert!(cache.matches_validated(&other));
cache.invalidate();
assert!(!cache.matches_validated(&other));
}
#[test]
fn store_on_a_dirty_cache_drops_stale_sibling_entries() {
let cache = LayoutCache::new();
cache.store(constraints(100.0), result(40.0));
cache.store(constraints(90.0), result(38.0));
assert!(cache.get(constraints(100.0)).is_some());
assert!(cache.get(constraints(90.0)).is_some());
cache.mark_descendant_dirty();
cache.store(constraints(90.0), result(45.0));
assert_eq!(
cache.get(constraints(90.0)).map(|cached| cached.size.width),
Some(45.0),
"the freshly stored entry is served"
);
assert!(
cache.get(constraints(100.0)).is_none(),
"the pre-invalidation sibling entry must not be served as fresh"
);
}
#[test]
fn clean_stores_keep_both_entries() {
let cache = LayoutCache::new();
cache.store(constraints(100.0), result(40.0));
cache.store(constraints(90.0), result(38.0));
assert_eq!(
cache.get(constraints(100.0)).map(|cached| cached.size.width),
Some(40.0)
);
assert_eq!(cache.get(constraints(90.0)).map(|cached| cached.size.width), Some(38.0));
}
#[test]
fn serving_an_entry_makes_its_constraints_the_front_ones() {
let cache = LayoutCache::new();
cache.store(constraints(1440.0), result(1440.0));
cache.store(constraints(960.0), result(960.0));
assert!(cache.get(constraints(1440.0)).is_some());
assert_eq!(cache.constraints().map(|front| front.max_width), Some(1440.0));
assert_eq!(cache.cached_entry().map(|(_, size)| size.width), Some(1440.0));
}
#[test]
fn a_repair_needs_the_front_entry_its_descendants_were_laid_out_for() {
let cache = LayoutCache::new();
cache.store(constraints(1440.0), result(1440.0));
cache.store(constraints(960.0), result(960.0));
assert!(cache.get_repairable(constraints(960.0)).is_some());
assert!(
cache.get_repairable(constraints(1440.0)).is_none(),
"an entry behind the front one is not repairable"
);
assert!(cache.get(constraints(1440.0)).is_some());
assert!(
cache.get_repairable(constraints(1440.0)).is_none(),
"a served older entry skipped its descendants"
);
assert!(
cache.get(constraints(1440.0)).is_some(),
"the served entry is still a clean hit"
);
cache.store(constraints(1440.0), result(1440.0));
assert!(cache.get_repairable(constraints(1440.0)).is_some());
}
#[test]
fn preserving_a_cache_keeps_whether_its_descendants_match_the_front_entry() {
let old = LayoutCache::new();
old.store(constraints(1440.0), result(1440.0));
old.store(constraints(960.0), result(960.0));
assert!(old.get(constraints(1440.0)).is_some());
let new = LayoutCache::new();
new.preserve_from(&old);
assert!(new.get_repairable(constraints(1440.0)).is_none());
}
#[test]
fn preserving_a_cache_takes_the_entries_instead_of_cloning_them() {
let old = LayoutCache::new();
old.store(constraints(1440.0), result(1440.0));
old.store(constraints(960.0), result(960.0));
let older = old.get_shared(constraints(1440.0)).unwrap();
let front = old.get_shared(constraints(960.0)).unwrap();
let new = LayoutCache::new();
new.preserve_from(&old);
assert!(!old.has_cached_result(), "the old cache keeps no entry");
assert!(old.get_shared(constraints(960.0)).is_none());
assert_eq!(
Arc::strong_count(&front),
2,
"only the new cache holds the result besides this test"
);
assert_eq!(new.constraints().map(|front| front.max_width), Some(960.0));
assert!(Arc::ptr_eq(&new.get_shared(constraints(960.0)).unwrap(), &front));
assert!(Arc::ptr_eq(&new.get_shared(constraints(1440.0)).unwrap(), &older));
}
#[test]
fn preserving_a_cache_carries_its_dirty_flags_with_the_entries() {
let old = LayoutCache::new();
old.store(constraints(960.0), result(960.0));
old.mark_local_dirty();
old.mark_descendant_dirty();
let new = LayoutCache::new();
new.preserve_from(&old);
assert!(new.is_local_dirty());
assert!(new.is_descendant_dirty());
assert!(new.has_cached_result());
assert!(
new.get_shared(constraints(960.0)).is_none(),
"a dirty cache serves nothing"
);
}
#[test]
fn a_reused_result_becomes_the_front_entry() {
let cache = LayoutCache::new();
cache.store(constraints(292.0), result(292.0));
cache.store(constraints(300.0), result(300.0));
cache.record_reuse(constraints(292.0), &result(292.0).into());
assert_eq!(cache.constraints().map(|front| front.max_width), Some(292.0));
assert!(cache.get_repairable(constraints(292.0)).is_none());
cache.store(constraints(300.0), result(300.0));
cache.store(constraints(310.0), result(310.0));
cache.record_reuse(constraints(292.0), &result(292.0).into());
assert_eq!(
cache.cached_entry().map(|(front, size)| (front.max_width, size.width)),
Some((292.0, 292.0))
);
assert!(cache.get(constraints(310.0)).is_some(), "the newer entry stays");
}
#[test]
fn only_a_second_set_of_constraints_boxes_the_entries() {
let cache = LayoutCache::new();
assert!(matches!(*cache.inner.borrow(), CachedLayouts::Empty));
cache.store(constraints(100.0), result(40.0));
assert!(matches!(*cache.inner.borrow(), CachedLayouts::One(_)));
cache.store(constraints(100.0), result(41.0));
assert!(
matches!(*cache.inner.borrow(), CachedLayouts::One(_)),
"the same constraints replace the entry"
);
assert_eq!(
cache.get(constraints(100.0)).map(|cached| cached.size.width),
Some(41.0)
);
cache.record_reuse(constraints(100.0), &result(41.0).into());
assert!(
matches!(*cache.inner.borrow(), CachedLayouts::One(_)),
"the only entry is the front one already"
);
cache.store(constraints(90.0), result(38.0));
assert!(matches!(*cache.inner.borrow(), CachedLayouts::Two(_)));
assert_eq!(
cache.get(constraints(100.0)).map(|cached| cached.size.width),
Some(41.0)
);
assert_eq!(cache.get(constraints(90.0)).map(|cached| cached.size.width), Some(38.0));
cache.invalidate();
assert!(matches!(*cache.inner.borrow(), CachedLayouts::Empty));
}
#[test]
fn a_third_set_of_constraints_replaces_the_entry_behind_the_front_one() {
let cache = LayoutCache::new();
cache.store(constraints(100.0), result(100.0));
cache.store(constraints(90.0), result(90.0));
cache.store(constraints(80.0), result(80.0));
assert!(
cache.get(constraints(100.0)).is_none(),
"the inline entry was behind the front one"
);
assert!(cache.get(constraints(90.0)).is_some());
let cache = LayoutCache::new();
cache.store(constraints(100.0), result(100.0));
cache.store(constraints(90.0), result(90.0));
assert!(cache.get(constraints(100.0)).is_some());
cache.store(constraints(80.0), result(80.0));
assert!(
cache.get(constraints(90.0)).is_none(),
"the served inline entry was the front one"
);
assert!(cache.get(constraints(100.0)).is_some());
}
#[test]
fn an_inline_entry_is_cleared_and_preserved_like_a_boxed_one() {
let cache = LayoutCache::new();
cache.store(constraints(100.0), result(100.0));
cache.mark_local_dirty();
assert!(cache.get(constraints(100.0)).is_none(), "a dirty cache serves nothing");
cache.store(constraints(90.0), result(90.0));
assert!(
matches!(*cache.inner.borrow(), CachedLayouts::One(_)),
"the stale entry is gone"
);
assert!(cache.get(constraints(100.0)).is_none());
assert!(cache.get_repairable(constraints(90.0)).is_some());
let new = LayoutCache::new();
new.preserve_from(&cache);
assert!(!cache.has_cached_result());
assert_eq!(
new.cached_entry().map(|(front, size)| (front.max_width, size.width)),
Some((90.0, 90.0))
);
}
}
#[cfg(test)]
mod subtree_tests {
use super::*;
#[test]
fn every_flag_of_a_subtree_is_kept_as_it_was_set() {
let cache = LayoutCache::default();
for at in 0..13 {
let mut flags = [false; 13];
flags[at] = true;
let subtree = LayoutSubtree::unpack(1 << at);
assert_eq!(subtree.flags(), flags, "flag {at}");
cache.set_subtree(subtree);
assert_eq!(cache.subtree().flags(), flags, "flag {at} kept");
}
cache.set_subtree(LayoutSubtree::unpack(0x1fff));
assert_eq!(cache.subtree().flags(), [true; 13]);
}
}