Cache

Struct Cache 

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

A cache of diagnostic sources.

Cache manages multiple source files and their associated data, allowing for efficient multi-source diagnostics. It can be reused across multiple render operations.

§Source Lifetime Management

The cache automatically handles different source types:

  • Borrowed sources (&str): Content must remain valid until rendering completes
  • Owned sources (String, Vec<u8>, etc.): Content is stored in the cache’s internal memory managed by the C library

§Single Source Convenience

For simple single-source diagnostics, you can pass sources directly to rendering methods without creating an explicit Cache. See Report::render_to_string() for examples.

§Example

use musubi::{Cache, Report, Level};

let cache = Cache::new()
    .with_source(("let x = 42;", "main.rs"))        // Source 0
    .with_source(("fn foo() {}", "lib.rs"));        // Source 1

let mut report = Report::new()
    .with_title(Level::Error, "Multiple files")
    .with_label((0..3, 0))   // Label in main.rs
    .with_message("here")
    .with_label((3..6, 1))   // Label in lib.rs
    .with_message("and here");

report.render_to_stdout(&cache)?;

Implementations§

Source§

impl Cache

Source

pub fn new() -> Self

Create a new empty cache.

Source

pub fn with_source<S: AddToCache>(self, content: S) -> Self

Add a source to the cache.

Accepts both borrowed (&str) and owned (String) content. For other byte buffers like Vec<u8>, use OwnedSource. Borrowed content must remain valid until rendering completes. Owned content is stored in the cache’s internal memory.

§Example
let cache = Cache::new()
    .with_source("let x = 42;")                    // &str - borrowed
    .with_source(("fn main() {}".to_string(), "main.rs"))  // String - owned
    .with_source((OwnedSource::new(vec![b'a', b'b', b'c']), "data.bin"));  // Vec<u8>

Trait Implementations§

Source§

impl Default for Cache

Source§

fn default() -> Cache

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

impl From<&Cache> for RawCache

Source§

fn from(cache: &Cache) -> RawCache

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for Cache

§

impl RefUnwindSafe for Cache

§

impl !Send for Cache

§

impl !Sync for Cache

§

impl Unpin for Cache

§

impl UnwindSafe for Cache

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.