Skip to main content

CoherenceCache

Struct CoherenceCache 

Source
pub struct CoherenceCache { /* private fields */ }
Expand description

Stores previous layout allocations for temporal coherence.

When terminal size changes, the layout solver re-computes positions from scratch. Without coherence, rounding tie-breaks can cause widgets to “jump” between frames even when the total size change is small.

The CoherenceCache feeds previous allocations to round_layout_stable so that tie-breaking favours keeping elements where they were.

§Usage

use ftui_layout::{CoherenceCache, CoherenceId, round_layout_stable, Constraint, Direction};

let mut coherence = CoherenceCache::new(64);

let id = CoherenceId::new(&constraints, Direction::Horizontal);
let prev = coherence.get(&id);
let alloc = round_layout_stable(&targets, total, prev);
coherence.store(id, alloc.clone());

§Eviction

Entries are evicted on a least-recently-stored basis when the cache reaches capacity. The cache does not grow unboundedly.

Implementations§

Source§

impl CoherenceCache

Source

pub fn new(max_entries: usize) -> Self

Create a new coherence cache with the specified capacity.

Source

pub fn get(&self, id: &CoherenceId) -> Option<Vec<u16>>

Retrieve the previous allocation for a layout, if available.

Returns Some(allocation) suitable for passing directly to round_layout_stable.

Source

pub fn store(&mut self, id: CoherenceId, allocation: Vec<u16>)

Store a layout allocation for future coherence lookups.

If the cache is at capacity, evicts the oldest entry.

Source

pub fn clear(&mut self)

Clear all stored allocations.

Source

pub fn len(&self) -> usize

Number of entries in the cache.

Source

pub fn is_empty(&self) -> bool

Returns true if the cache is empty.

Source

pub fn displacement(&self, id: &CoherenceId, new_alloc: &[u16]) -> (u64, u32)

Compute displacement between a new allocation and the previously stored one.

Returns (sum_displacement, max_displacement) in cells. If no previous allocation exists for the ID, returns (0, 0).

Trait Implementations§

Source§

impl Debug for CoherenceCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CoherenceCache

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.