Skip to main content

LineCache

Struct LineCache 

Source
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 be Clone + Send + Sync + 'static.

Implementations§

Source§

impl<T: Clone + Send + Sync + 'static> LineCache<T>

Source

pub fn new() -> Self

Create a new empty cache.

Source

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 check
  • hash - The expected content hash
Source

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 retrieve
  • hash - The expected content hash
Source

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.

Source

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 of line_idx -> (hash, value) to store
Source

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 of line_idx -> (hash, value) to merge
Source

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.

Source

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)
Source

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
Source

pub fn clear(&self)

Clear all cached entries.

Source

pub fn len(&self) -> usize

Get the number of cached lines.

Source

pub fn is_empty(&self) -> bool

Check if the cache is empty.

Source

pub fn cached_lines(&self) -> Vec<usize>

Get all cached line indices.

Trait Implementations§

Source§

impl<T: Debug> Debug for LineCache<T>

Source§

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

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

impl<T: Clone + Send + Sync + 'static> Default for LineCache<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> !Freeze for LineCache<T>

§

impl<T> RefUnwindSafe for LineCache<T>
where T: RefUnwindSafe,

§

impl<T> Send for LineCache<T>
where T: Sync + Send,

§

impl<T> Sync for LineCache<T>
where T: Sync + Send,

§

impl<T> Unpin for LineCache<T>

§

impl<T> UnsafeUnpin for LineCache<T>

§

impl<T> UnwindSafe for LineCache<T>
where T: RefUnwindSafe,

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.