pub struct Span { /* private fields */ }Expand description
A half-open byte range [start, end) within one file.
Internally stored as start plus len, so the only constructor
Span::new can reject inversion at construction time and the invariant is
preserved by construction thereafter. An empty span (len == 0) is valid and
marks a position rather than a range.
Implementations§
Source§impl Span
impl Span
Sourcepub fn new(start: impl Into<BytePos>, end: impl Into<BytePos>) -> Span
pub fn new(start: impl Into<BytePos>, end: impl Into<BytePos>) -> Span
The only public constructor. end must be >= start.
Panics (debug only) if end < start. In release builds an inverted range
collapses to an empty span at start, so the type invariant is never
violated even if a caller bypasses the debug assertion.
Sourcepub fn at(pos: impl Into<BytePos>) -> Span
pub fn at(pos: impl Into<BytePos>) -> Span
An empty span anchored at a single offset. Useful for “at this position”.
pub const fn start(self) -> BytePos
pub const fn end(self) -> BytePos
pub const fn len(self) -> u32
pub const fn is_empty(self) -> bool
Sourcepub const fn shifted(self, delta: u32) -> Span
pub const fn shifted(self, delta: u32) -> Span
The same span, moved delta bytes later in the file.
The length is preserved, so the invariant holds by construction. Used to rebase spans produced against a fragment onto the file that contains it: the input-parser’s template scanner works in offsets relative to a backtick template’s interior, and the HIR bridge rebases the tree by the token’s start.
Sourcepub fn cover(self, other: Span) -> Span
pub fn cover(self, other: Span) -> Span
The smallest span covering both self and other. If the two spans are
in different files the caller must use FileSpan::union instead.
An empty span acts as a neutral element: unioning with one returns the other span unchanged.