Skip to main content

SourceCache

Struct SourceCache 

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

Every source text, kept alive as long as the cache is, so the zero-copy pipeline can hand out &str slices into it.

The texts are held as raw pointers from Box::into_raw, which is load-bearing and was arrived at the hard way — Miri rejected the two obvious alternatives:

  • Storing Box<str> and taking a reference before pushing. Vec::push moves the box, and moving one re-asserts unique ownership of its contents, which invalidates every slice already handed out. Reallocation moves them again. Box carries a genuine noalias guarantee that LLVM exploits, so this is not a technicality.
  • Storing &'static str from Box::leak. Handing out slices is then safe, but Drop has to free them, and reconstructing a Box from a shared reference deallocates through provenance that never granted write access.

A raw pointer has no aliasing guarantee to violate: moving it inside the Vec retags nothing, shared references derived from it stay valid, and Drop frees through the same provenance it was created with. Checked under both Stacked Borrows and Tree Borrows; see references_survive_later_insertions_and_reallocation.

Implementations§

Source§

impl SourceCache

Source

pub fn new() -> Self

Source

pub fn add(&self, name: String, text: String) -> (SourceId, &str)

Registers a source and returns a slice with the cache’s lifetime.

The slice is returned bounded by &self, so no caller can outlive the arena that owns it.

Source

pub fn name(&self, id: SourceId) -> Option<String>

Source

pub fn text(&self, id: SourceId) -> Option<&str>

Trait Implementations§

Source§

impl Default for SourceCache

Source§

fn default() -> SourceCache

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

impl Drop for SourceCache

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. 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> Same for T

Source§

type Output = T

Should always be Self
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.