pub struct Span { /* private fields */ }Expand description
A compressed span.
Whereas SpanData is 12 bytes, which is a bit too big to stick everywhere, Span
is a form that only takes up 8 bytes, with less space for the length and
context. The vast majority (99.9%+) of SpanData instances will fit within
those 8 bytes; any SpanData whose fields don’t fit into a Span are
stored in a separate interner table, and the Span will index into that
table. Interning is rare enough that the cost is low, but common enough
that the code is exercised regularly.
An earlier version of this code used only 4 bytes for Span, but that was
slower because only 80–90% of spans could be stored inline (even less in
very large crates) and so the interner was used a lot more.
Inline (compressed) format:
span.base_or_index == span_data.lospan.len_or_tag == len == span_data.hi - span_data.lo(must be<= MAX_LEN)span.ctxt == span_data.ctxt(must be<= MAX_CTXT)
Interned format:
span.base_or_index == index(indexes into the interner table)span.len_or_tag == LEN_TAG(high bit set, all other bits are zero)span.ctxt == 0
The inline form uses 0 for the tag value (rather than 1) so that we don’t need to mask out the tag bit when getting the length, and so that the dummy span can be all zeroes.
Notes about the choice of field sizes:
baseis 32 bits in bothSpanandSpanData, which means thatbasevalues never cause interning. The number of bits needed forbasedepends on the crate size. 32 bits allows up to 4 GiB of code in a crate.lenis 15 bits inSpan(a u16, minus 1 bit for the tag) and 32 bits inSpanData, which means that largelenvalues will cause interning. The number of bits needed forlendoes not depend on the crate size. The most common numbers of bits forlenare from 0 to 7, with a peak usually at 3 or 4, and then it drops off quickly from 8 onwards. 15 bits is enough for 99.99%+ of cases, but larger values (sometimes 20+ bits) might occur dozens of times in a typical crate.ctxtis 16 bits inSpanand 32 bits inSpanData, which means that largectxtvalues will cause interning. The number of bits needed forctxtvalues depend partly on the crate size and partly on the form of the code. No crates inrustc-perfneed more than 15 bits forctxt, but larger crates might need more than 16 bits.
In order to reliably use parented spans in incremental compilation,
the dependency to the parent definition’s span. This is performed
using the callback SPAN_TRACK to access the query engine.
Implementations§
Source§impl Span
impl Span
pub fn lo(self) -> BytePos
pub fn with_lo(self, lo: BytePos) -> Span
pub fn hi(self) -> BytePos
pub fn with_hi(self, hi: BytePos) -> Span
Sourcepub fn shrink_to_lo(self) -> Span
pub fn shrink_to_lo(self) -> Span
Returns a new span representing an empty span at the beginning of this span.
Sourcepub fn shrink_to_hi(self) -> Span
pub fn shrink_to_hi(self) -> Span
Returns a new span representing an empty span at the end of this span.
Sourcepub fn substitute_dummy(self, other: Span) -> Span
pub fn substitute_dummy(self, other: Span) -> Span
Returns self if self is not the dummy span, and other otherwise.
Sourcepub fn source_equal(self, other: Span) -> bool
pub fn source_equal(self, other: Span) -> bool
Returns true if the spans are equal with regards to the source text.
Use this instead of == when either span could be generated code,
and you only care that they point to the same bytes of source text.
Sourcepub fn trim_start(self, other: Span) -> Option<Span>
pub fn trim_start(self, other: Span) -> Option<Span>
Returns Some(span), where the start is trimmed by the end of other.
Sourcepub fn to(self, end: Span) -> Span
pub fn to(self, end: Span) -> Span
Returns a Span that would enclose both self and end.
____ ___
self lorem ipsum end
^^^^^^^^^^^^^^^^^^^^Sourcepub fn between(self, end: Span) -> Span
pub fn between(self, end: Span) -> Span
Returns a Span between the end of self to the beginning of end.
____ ___
self lorem ipsum end
^^^^^^^^^^^^^Sourcepub fn until(self, end: Span) -> Span
pub fn until(self, end: Span) -> Span
Returns a Span from the beginning of self until the beginning of end.
____ ___
self lorem ipsum end
^^^^^^^^^^^^^^^^^pub fn from_inner(self, inner: InnerSpan) -> Span
Trait Implementations§
Source§impl Ord for Span
impl Ord for Span
Source§impl PartialOrd for Span
impl PartialOrd for Span
impl Copy for Span
impl Eq for Span
impl StructuralPartialEq for Span
Auto Trait Implementations§
impl Freeze for Span
impl RefUnwindSafe for Span
impl Send for Span
impl Sync for Span
impl Unpin for Span
impl UnwindSafe for Span
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more