Skip to main content

Span

Struct Span 

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

A shared source snapshot and optional byte range within it.

Implementations§

Source§

impl Span

Source

pub fn new(source: Arc<Source>, range: Option<Range<Offset>>) -> Self

Builds a span in source with an optional byte range.

Source

pub fn ranged(source: Arc<Source>, range: Range<Offset>) -> Self

Builds a span covering range in source.

Source

pub fn unlocated(source: Arc<Source>) -> Self

Builds a span whose source is known but whose byte range is not.

Source

pub fn for_text(text: impl Into<Arc<str>>) -> Self

Creates a span covering one complete synthetic source buffer.

Examples found in repository?
examples/parse_emit_walkthrough.rs (line 17)
13    fn parse(input: &mut ParseStream) -> Result<Self, TokenParseError> {
14        let Some(token) = input.next_token() else {
15            return Err(TokenParseError::UnexpectedEnd {
16                expected: "identifier",
17                span: Span::for_text(""),
18            });
19        };
20
21        let span = token.span();
22        match token {
23            TokenTree::Ident(identifier) => Ok(Self(identifier)),
24            token => Err(TokenParseError::UnexpectedToken {
25                expected: "identifier",
26                found: token.spelling().unwrap_or("group or trivia").to_owned(),
27                span,
28            }),
29        }
30    }
Source

pub fn file(&self) -> File

Returns the source file identity.

Source

pub fn source(&self) -> &Arc<Source> ⓘ

Borrows the shared source snapshot.

Source

pub fn source_id(&self) -> File

Returns the source identity.

Source

pub fn range(&self) -> Option<Range<Offset>>

Returns a copy of the indexed byte range.

Source

pub fn with_source(self, source: Arc<Source>) -> Self

Changes the source snapshot while preserving this span’s range.

Source

pub fn with_text(self, text: impl Into<Arc<str>>) -> Self

Ensures that this span resolves to exactly text.

Parsed syntax retains its original source allocation. Programmatically constructed syntax receives a compact synthetic source allocation rather than storing a second copy of its spelling in every token.

Source

pub fn text(&self) -> Option<&str>

Returns the exact source text covered by this span.

Source

pub fn at(&self, offset: Offset) -> Self

Creates a zero-width span at offset with this span’s source identity.

Source

pub fn subspan(&self, start: Offset, end: Offset) -> Self

Returns a subspan at byte offsets relative to this span’s opening.

The returned span shares the same source identity. Invalid or unavailable ranges become unlocated spans in the same file.

Source

pub fn start(&self) -> Option<Offset>

The boundary this span opens at, or None when it is unlocated.

Source

pub fn end(&self) -> Option<Offset>

The boundary this span closes at, past its last byte.

Source

pub fn bounds(&self) -> Option<(Offset, Offset)>

The boundaries this span lies between.

Source

pub fn line_column_range(&self) -> Option<LineColumnRange>

Returns this span as one-based line and character-column positions.

Returns None for an unlocated span, a range outside its source snapshot, or a boundary inside a multi-byte UTF-8 character.

Source

pub fn len(&self) -> Option<usize>

How many bytes this span covers, or None when it is unlocated.

Source

pub fn is_empty(&self) -> bool

Reports whether this span covers no bytes, as the virtual marker tokens do. Unlocated syntax covers no bytes either.

Source

pub fn shares_source(&self, other: &Self) -> bool

Reports whether both spans identify the same source site.

Source

pub fn contains(&self, offset: Offset) -> bool

Returns whether offset lies inside this span, excluding its closing boundary.

Boundaries sit between bytes, so the closing one belongs to whatever follows: in a+b the boundary after a is contained by +, not by a. This is the predicate to use when exactly one span must own a position. A zero-width span contains nothing, having no interior.

Source

pub fn touches(&self, offset: Offset) -> bool

Returns whether offset lies inside this span or on either boundary.

Adjacent spans both touch the boundary between them, so this answers “which syntax is the cursor at” rather than “which syntax owns this position” — a cursor just past an identifier still touches it, and a zero-width marker is touched at its own position.

Source

pub fn shifted(&self, edit: &TextEdit) -> Self

Returns this span’s position in the buffer produced by edit.

Both boundaries move under the same rule, so nothing here decides which side of an insertion a boundary belongs to. Because that rule is monotone the pair can never invert, and a zero-width span needs no special case: its two equal boundaries stay equal.

Text inserted at a span’s closing boundary therefore extends the span, which is what an unclosed group wants — it keeps reaching whatever now follows it. A span wholly removed by a deletion collapses to a zero-width span at the deletion point rather than losing its location, because it still records where the syntax used to be.

Source

pub fn resized(&self, len: usize) -> Self

Returns this span with the same opening boundary and source identity but a new byte length.

Source§

impl Span

Source

pub fn join(&self, other: Self) -> Option<Self>

The smallest span covering both, or None when they belong to different files.

Source

pub fn meet(&self, other: Self) -> Option<Self>

The overlap of both, or None when the files differ or the ranges do not overlap.

Source

pub fn join_all<I, S>(spans: I) -> Option<Self>
where I: IntoIterator<Item = S>, S: Into<Option<Self>>,

Joins every span in an iterator, returning None for an empty iterator or when any two spans belong to different files.

Trait Implementations§

Source§

impl Clone for Span

Source§

fn clone(&self) -> Span

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Span

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Span

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Span

Source§

impl Hash for Span

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Span

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Spanned for Span

Source§

fn span(&self) -> Span

Returns the smallest span covering this value. Read more
Source§

fn try_span(&self) -> Option<Span>

Returns the smallest span covering this value, when its constituents share one source snapshot.
Source§

fn expect_span(&self) -> Span

Returns this value’s span when one source snapshot covers it. Read more

Auto Trait Implementations§

§

impl Freeze for Span

§

impl RefUnwindSafe for Span

§

impl Send for Span

§

impl Sync for Span

§

impl Unpin for Span

§

impl UnsafeUnpin for Span

§

impl UnwindSafe for Span

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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.