Span

Struct Span 

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

References a range of bytes in the original source file.

Can be missing, in which case doesn’t correspond to any actual portion of the source file. This is the Default value.

For the purposes of comparisons, all spans are considered equal to each other. This means that calling PartialEq::eq just returns true, calling Hash::hash does nothing and calling Ord::cmp always returns Ordering::Equal. This means that adding a Span field to a type can never impact the derived implementations of these traits.

Implementations§

Source§

impl Span

Source

pub const MISSING: Span

Indicates that the span information is missing, and doesn’t correspond to a known portion of the source file.

Source

pub fn new(start: Location, end: Location) -> Span

Create a span with the specified start and end locations.

If either location is missing, this will return Span::MISSING.

§Panics

If the start location comes after the end location, this function will panic. If either location is missing, this function is guaranteed to succeed.

Source

pub fn from_byte_range<T: PrimUInt>(range: Range<T>) -> Self

Create a span from the specified range of byte indexes.

§Panics

This will panic if the start location comes after the end location, just like Span::new. This will also panic if either offset overflows a Location, just like Location::from_byte.

Source

pub fn eq(self) -> OrderedSpan

Return a wrapper type that implements by-value equality rather than having all values be equal.

Source

pub fn ord(self) -> OrderedSpan

Return a wrapper type that implements by-value ordering rather than having all values be equal.

Source

pub const fn is_missing(&self) -> bool

Check if the span is Self::MISSING.

Source

pub fn byte_range(&self) -> Result<Range<u64>, MissingLocationError>

Return the range of bytes in the original file, or a MissingLocationError if missing.

Source

pub fn byte_len(&self) -> Result<u64, MissingLocationError>

Returns the length of the span in bytes, or a MissingLocationError if missing.

Source

pub fn slice_byte_indexes<R>(self, range: R) -> Self
where R: RangeBounds<u64> + Debug,

Slice the bytes indices this span, returning a subset of its indexes.

If the original span is missing, the result will be too.

§Panics

Referencing an index that exceeds the byte length of this span will trigger a panic. This will also panic If the start of the range exceeds the end of the range,

§Examples
use qbe_parser::ast::Span;
let x = Span::from_byte_range::<u32>(5..20);
assert_eq!(
    x.slice_byte_indexes(2..).byte_range(),
    Ok(7..20)
);
assert_eq!(
    x.slice_byte_indexes(3..5).byte_range(),
    Ok(8..10)
);
assert_eq!(
    x.slice_byte_indexes(3..=5).byte_range(),
    Ok(8..11)
);

The following code will panic due to referencing an out-of-bounds indexes.

use qbe_parser::ast::Span;
let x = Span::from_byte_range::<u32>(5..20);
assert_eq!(x.byte_len(), Ok(15));
x.slice_byte_indexes(100..); // panics
Source

pub fn start(&self) -> Location

The starting location of the span (inclusive), or Location::MISSING if missing.

Source

pub fn end(&self) -> Location

The ending location of the span (exclusive), or Location::MISSING if missing.

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() -> Self

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 From<ByteLocation> for Span

Source§

fn from(location: ByteLocation) -> Self

Converts to this type from the input type.
Source§

impl From<Location> for Span

Source§

fn from(value: Location) -> Self

Converts to this type from the input type.
Source§

impl From<OrderedSpan> for Span

Source§

fn from(value: OrderedSpan) -> Self

Converts to this type from the input type.
Source§

impl From<SimpleSpan> for Span

Source§

fn from(span: SimpleSpan) -> Self

Converts to this type from the input type.
Source§

impl From<Span> for OrderedSpan

Source§

fn from(span: Span) -> Self

Converts to this type from the input type.
Source§

impl Hash for Span

Hashes the span.

This is guaranteed to do nothing, so adding it to a struct will not affect the result of derive(Hash).

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: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

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

Restrict a value to a certain interval. Read more
Source§

impl PartialEq<Span> for OrderedSpan

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

Unconditionally returns true, so that adding a Span to a struct will not affect derive(Eq)

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 PartialOrd<Span> for OrderedSpan

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 · 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 · 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 · 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 · 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 PartialOrd for Span

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · 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 · 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 · 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 · 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 Span for Span

Source§

type Context = ()

Extra context used in a span. Read more
Source§

type Offset = ByteLocation

A type representing a span’s start or end offset from the start of the input. Read more
Source§

fn new(_context: Self::Context, range: Range<Self::Offset>) -> Self

Create a new span given a context and an offset range.
Source§

fn context(&self) -> Self::Context

Return the span’s context.
Source§

fn start(&self) -> Self::Offset

Return the start offset of the span.
Source§

fn end(&self) -> Self::Offset

Return the end offset of the span.
Source§

fn to_end(&self) -> Self
where Self: Sized,

Turn this span into a zero-width span that starts and ends at the end of the original. Read more
Source§

impl Copy for Span

Source§

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

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
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<'src, T> IntoMaybe<'src, T> for T
where T: 'src,

Source§

type Proj<U: 'src> = U

Source§

fn map_maybe<R>( self, _f: impl FnOnce(&'src T) -> &'src R, g: impl FnOnce(T) -> R, ) -> <T as IntoMaybe<'src, T>>::Proj<R>
where R: 'src,

Source§

impl<'p, T> Seq<'p, T> for T
where T: Clone,

Source§

type Item<'a> = &'a T where T: 'a

The item yielded by the iterator.
Source§

type Iter<'a> = Once<&'a T> where T: 'a

An iterator over the items within this container, by reference.
Source§

fn seq_iter(&self) -> <T as Seq<'p, T>>::Iter<'_>

Iterate over the elements of the container.
Source§

fn contains(&self, val: &T) -> bool
where T: PartialEq,

Check whether an item is contained within this sequence.
Source§

fn to_maybe_ref<'b>(item: <T as Seq<'p, T>>::Item<'b>) -> Maybe<T, &'p T>
where 'p: 'b,

Convert an item of the sequence into a MaybeRef.
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> ToSmolStr for T
where T: Display + ?Sized,

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.
Source§

impl<T> OrderedSeq<'_, T> for T
where T: Clone,