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::pushmoves the box, and moving one re-asserts unique ownership of its contents, which invalidates every slice already handed out. Reallocation moves them again.Boxcarries a genuinenoaliasguarantee that LLVM exploits, so this is not a technicality. - Storing
&'static strfromBox::leak. Handing out slices is then safe, butDrophas to free them, and reconstructing aBoxfrom 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
impl SourceCache
pub fn new() -> Self
Sourcepub fn add(&self, name: String, text: String) -> (SourceId, &str)
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.
pub fn name(&self, id: SourceId) -> Option<String>
pub fn text(&self, id: SourceId) -> Option<&str>
Trait Implementations§
Source§impl Default for SourceCache
impl Default for SourceCache
Source§fn default() -> SourceCache
fn default() -> SourceCache
Returns the “default value” for a type. Read more
Source§impl Drop for SourceCache
impl Drop for SourceCache
Auto Trait Implementations§
impl !Freeze for SourceCache
impl !RefUnwindSafe for SourceCache
impl !Send for SourceCache
impl !Sync for SourceCache
impl Unpin for SourceCache
impl UnsafeUnpin for SourceCache
impl UnwindSafe for SourceCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more