pub struct LineCache<T> { /* private fields */ }Expand description
A generic line-based cache with hash-based validation.
LineCache<T> stores values indexed by line number, where each entry
includes a content hash for invalidation checking. This enables efficient
cache reuse when line content hasn’t changed.
§Thread Safety
LineCache is Send + Sync when T is. The underlying ArcSwap provides
lock-free reads and atomic updates via the RCU pattern.
§Type Parameters
T: The cached value type. Must beClone + Send + Sync + 'static.
Implementations§
Source§impl<T: Clone + Send + Sync + 'static> LineCache<T>
impl<T: Clone + Send + Sync + 'static> LineCache<T>
Sourcepub fn has(&self, line_idx: usize, hash: u64) -> bool
pub fn has(&self, line_idx: usize, hash: u64) -> bool
Check if a line is cached with a matching hash.
Returns true if the line exists in the cache AND the stored hash
matches the provided hash. This is a fast validity check.
§Arguments
line_idx- The line index to checkhash- The expected content hash
Sourcepub fn get_if_valid(&self, line_idx: usize, hash: u64) -> Option<T>
pub fn get_if_valid(&self, line_idx: usize, hash: u64) -> Option<T>
Get cached value only if the hash matches.
Returns Some(value) if the line is cached AND the stored hash
matches. Returns None if not cached or hash mismatch.
§Arguments
line_idx- The line index to retrievehash- The expected content hash
Sourcepub fn get(&self, line_idx: usize) -> Option<(u64, T)>
pub fn get(&self, line_idx: usize) -> Option<(u64, T)>
Get cached value without hash validation.
Returns Some((hash, value)) if the line is cached, regardless of
whether the hash is current. Use this when you need to inspect
the cached hash.
Sourcepub fn store(&self, entries: HashMap<usize, (u64, T)>)
pub fn store(&self, entries: HashMap<usize, (u64, T)>)
Store entries in the cache, replacing all existing content.
This uses the RCU pattern: atomically swaps the entire cache contents. Concurrent readers see either the old or new state, never a partial update.
§Arguments
entries- Map ofline_idx-> (hash, value) to store
Sourcepub fn update(&self, updates: &HashMap<usize, (u64, T)>)
pub fn update(&self, updates: &HashMap<usize, (u64, T)>)
Update specific entries without replacing the entire cache.
Merges the provided entries into the existing cache. Existing entries
not in updates are preserved.
§Arguments
updates- Map ofline_idx-> (hash, value) to merge
Sourcepub fn clone_entries(&self) -> HashMap<usize, (u64, T)>
pub fn clone_entries(&self) -> HashMap<usize, (u64, T)>
Clone current entries for external modification.
Returns a clone of all cached entries. Use this when you need to inspect or transform the cache contents.
Sourcepub fn invalidate_from(&self, from_line: usize)
pub fn invalidate_from(&self, from_line: usize)
Invalidate (remove) entries from a specific line onward.
Removes all entries where line_idx >= from_line. This is useful
when text is inserted or deleted, invalidating subsequent lines.
§Arguments
from_line- First line index to invalidate (inclusive)
Sourcepub fn invalidate_line(&self, line_idx: usize)
pub fn invalidate_line(&self, line_idx: usize)
Invalidate a specific line.
Removes the entry for a single line if it exists.
§Arguments
line_idx- The line index to invalidate
Sourcepub fn cached_lines(&self) -> Vec<usize>
pub fn cached_lines(&self) -> Vec<usize>
Get all cached line indices.