pub struct SourceSpan { /* private fields */ }Expand description
Represents a range of bytes in a specific source file
A SourceSpan is a combination of SourceId and a range
of byte indices in the corresponding file. With one, you may
obtain a variety of useful information about the source to which
it maps using the CodeMap from which it was created:
- Can be used to get a
strof the original file content containing just the specified range. - Can be used to get file/line/column at which the span starts
- Can be used to get the [SourceFile] from which it is derived
A SourceSpan has a canonical “default” value, which is represented
by SourceSpan::UNKNOWN. It can be treated like a regular span, however
when a request is made for content or location information corresponding
to it, those APIs will return None or Err. This is useful when
constructing syntax trees and the like without sources, such as in
testing scenarios.
Implementations§
Source§impl SourceSpan
impl SourceSpan
Sourcepub fn new(start: SourceIndex, end: SourceIndex) -> Self
pub fn new(start: SourceIndex, end: SourceIndex) -> Self
Creates a new span from start to end
This function will panic if the indices are in different source files
Sourcepub fn is_unknown(self) -> bool
pub fn is_unknown(self) -> bool
Returns true if this span represents an “unknown” source span
Sourcepub fn start(&self) -> SourceIndex
pub fn start(&self) -> SourceIndex
Returns the starting SourceIndex of this span
Sourcepub fn start_index(&self) -> ByteIndex
pub fn start_index(&self) -> ByteIndex
Returns the starting ByteIndex of this span in its [SourceFile]
Sourcepub fn shrink_front(self, offset: ByteOffset) -> Self
pub fn shrink_front(self, offset: ByteOffset) -> Self
Shrinks this span by truncating offset bytes from the start of its range
Sourcepub fn end(&self) -> SourceIndex
pub fn end(&self) -> SourceIndex
Returns the ending source index of this span
Sourcepub fn end_index(&self) -> ByteIndex
pub fn end_index(&self) -> ByteIndex
Returns the ending byte index of this span in its SourceFile
Sourcepub fn merge(self, other: SourceSpan) -> Option<SourceSpan>
pub fn merge(self, other: SourceSpan) -> Option<SourceSpan>
Creates a new span that covers both this span and other, forming a new contiguous span
Returns None if either span is invalid or from a different source file.
The order of the spans is not important.
Trait Implementations§
Source§impl Clone for SourceSpan
impl Clone for SourceSpan
Source§fn clone(&self) -> SourceSpan
fn clone(&self) -> SourceSpan
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more