Skip to main content

Span

Struct Span 

Source
pub struct Span {
    pub start: Position,
    pub end: Position,
    pub start_offset: usize,
    pub end_offset: usize,
}
Expand description

A span in source text, covering a range between two positions.

Spans are used to indicate the start and end positions of parsed elements. They include both line/column positions and byte offsets for flexible use.

§Byte Offset Semantics

Byte offsets use exclusive end semantics (like Rust ranges):

  • start_offset..end_offset covers the half-open range [start, end)
  • Use source[start_offset..end_offset] to slice the spanned text

§Position Semantics

Line and column positions are 1-indexed (the first line is line 1, the first column is column 1). The end position points to the last character in the span (inclusive for display purposes).

§Synthetic Spans

Spans created by Span::synthetic() have line: 0 to distinguish them from real source positions. Use is_synthetic() to check.

Fields§

§start: Position

The start position (line and column, 1-indexed).

§end: Position

The end position (line and column, 1-indexed).

§start_offset: usize

The start byte offset in the source (0-indexed, inclusive).

§end_offset: usize

The end byte offset in the source (0-indexed, exclusive).

Implementations§

Source§

impl Span

Source

pub const fn synthetic() -> Self

Create a synthetic span for values without source positions.

Uses line 0 to distinguish from real spans (which are 1-indexed). This is useful when converting Value to Expr for deserialization. Check with is_synthetic().

Source

pub const fn is_synthetic(&self) -> bool

Returns true if this is a synthetic span (no source position).

Synthetic spans are created by Span::synthetic() for values converted from Value to AST without original source positions. They use line 0 to distinguish from real spans (which are 1-indexed).

Source

pub fn slice<'a>(&self, source: &'a str) -> &'a str

Slice the given source text using this span’s byte offsets.

§Panics

Panics if the byte offsets are out of bounds for the source.

Source

pub fn between(start: &Span, end: &Span) -> Self

Create a span covering from the start of start to the end of end.

This is useful for creating spans that cover a range of tokens, such as the span of a tuple from ( to ).

Trait Implementations§

Source§

impl Clone for Span

Source§

fn clone(&self) -> Span

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Span

Source§

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

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

impl Default for Span

Source§

fn default() -> Span

Returns the “default value” for a type. Read more
Source§

impl Display for Span

Source§

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

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

impl PartialEq for Span

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Span

Source§

impl Eq for Span

Source§

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> 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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 = Infallible

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.