Skip to main content

Span

Struct Span 

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

A half-open byte range start..end into a single source.

A Span is two packed BytePos offsets — eight bytes, Copy — that a lexer attaches to every token and a parser threads through every node. The range is half-open: start is included, end is not, so the length is exactly end - start and adjacent spans (a.end == b.start) do not overlap.

§Invariant

start <= end always holds. Span::new enforces it by ordering its two arguments, so a span can never be constructed inverted, and every method may rely on it. An empty span (start == end) is legal and marks a zero-width point — the position of an insertion, or a token with no text.

Spans order lexicographically by start then end, so a slice of spans sorts into source order.

§Examples

use span_lang::Span;

let s = Span::new(4, 10);
assert_eq!(s.len(), 6);
assert!(!s.is_empty());

// Arguments are ordered, so an inverted call still yields a valid span.
assert_eq!(Span::new(10, 4), Span::new(4, 10));

Implementations§

Source§

impl Span

Source

pub const fn new(start: u32, end: u32) -> Self

Constructs a span covering start..end.

If start > end the two are swapped, so the result always upholds the start <= end invariant. This makes construction total — it never panics, whatever offsets a caller supplies — which matters when the offsets come from arithmetic on untrusted input.

§Examples
use span_lang::Span;

let s = Span::new(2, 7);
assert_eq!(s.start().to_u32(), 2);
assert_eq!(s.end().to_u32(), 7);

// Ordering is normalised.
assert_eq!(Span::new(7, 2), s);
Source

pub const fn empty(at: u32) -> Self

Constructs an empty, zero-width span at at.

Equivalent to Span::new(at, at). Use it to mark a point — for instance, the caret position for an “expected token here” diagnostic.

§Examples
use span_lang::Span;

let point = Span::empty(5);
assert!(point.is_empty());
assert_eq!(point.len(), 0);
Source

pub const fn start(self) -> BytePos

Returns the start position (inclusive).

§Examples
use span_lang::{BytePos, Span};

assert_eq!(Span::new(3, 8).start(), BytePos::new(3));
Source

pub const fn end(self) -> BytePos

Returns the end position (exclusive).

§Examples
use span_lang::{BytePos, Span};

assert_eq!(Span::new(3, 8).end(), BytePos::new(8));
Source

pub const fn len(self) -> u32

Returns the length of the span in bytes.

Always end - start, which the invariant guarantees is non-negative.

§Examples
use span_lang::Span;

assert_eq!(Span::new(4, 10).len(), 6);
assert_eq!(Span::empty(4).len(), 0);
Source

pub const fn is_empty(self) -> bool

Returns true if the span is zero-width (start == end).

§Examples
use span_lang::Span;

assert!(Span::empty(9).is_empty());
assert!(!Span::new(9, 12).is_empty());
Source

pub const fn contains(self, pos: BytePos) -> bool

Returns true if pos falls within the span (start <= pos < end).

Membership is half-open to match the range: the end position is not contained, and an empty span contains no position at all.

§Examples
use span_lang::{BytePos, Span};

let s = Span::new(4, 8);
assert!(s.contains(BytePos::new(4)));  // start is included
assert!(s.contains(BytePos::new(7)));
assert!(!s.contains(BytePos::new(8))); // end is excluded
assert!(!Span::empty(4).contains(BytePos::new(4)));
Source

pub const fn merge(self, other: Self) -> Self

Returns the smallest span that covers both self and other.

The result spans min(starts)..max(ends). merge is commutative (a.merge(b) == b.merge(a)) and associative (a.merge(b).merge(c) == a.merge(b.merge(c))), so the order spans are combined in never changes the result — useful when folding a node’s span over its children.

§Examples
use span_lang::Span;

let a = Span::new(4, 10);
let b = Span::new(8, 14);
assert_eq!(a.merge(b), Span::new(4, 14));

// Disjoint spans merge to the range that encloses both.
assert_eq!(Span::new(0, 2).merge(Span::new(20, 24)), Span::new(0, 24));

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 Copy for Span

Source§

impl Debug for Span

Source§

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

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

impl<'de> Deserialize<'de> for Span

Available on crate feature serde only.
Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialises { start, end } and routes it through Span::new, so a span read from an untrusted source upholds the start <= end invariant exactly as a constructed one does — an inverted pair on the wire is normalised, never accepted as-is.

Source§

impl Display for Span

Source§

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

Formats as start..end.

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 Ord for Span

Source§

fn cmp(&self, other: &Span) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. 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 (const: unstable) · 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 PartialOrd for Span

Source§

fn partial_cmp(&self, other: &Span) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Span

Available on crate feature serde only.
Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
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 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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.