#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};
use core::ops::Range;
use crate::{AttrsOwned, HashMap, ShapeGlyph};
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct ShapeRunKey {
pub text: String,
pub default_attrs: AttrsOwned,
pub attrs_spans: Vec<(Range<usize>, AttrsOwned)>,
}
#[derive(Clone, Default)]
pub struct ShapeRunCache {
age: u64,
cache: HashMap<ShapeRunKey, (u64, Vec<ShapeGlyph>)>,
}
impl ShapeRunCache {
pub fn get(&mut self, key: &ShapeRunKey) -> Option<&Vec<ShapeGlyph>> {
self.cache.get_mut(key).map(|(age, glyphs)| {
*age = self.age;
&*glyphs
})
}
pub fn insert(&mut self, key: ShapeRunKey, glyphs: Vec<ShapeGlyph>) {
self.cache.insert(key, (self.age, glyphs));
}
pub fn trim(&mut self, keep_ages: u64) {
self.cache
.retain(|_key, (age, _glyphs)| *age + keep_ages >= self.age);
self.age += 1;
}
}
impl core::fmt::Debug for ShapeRunCache {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("ShapeRunCache").finish()
}
}