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
impl Span
Sourcepub const MISSING: Span
pub const MISSING: Span
Indicates that the span information is missing, and doesn’t correspond to a known portion of the source file.
Sourcepub fn new(start: Location, end: Location) -> Span
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.
Sourcepub fn from_byte_range<T: PrimUInt>(range: Range<T>) -> Self
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.
Sourcepub fn eq(self) -> OrderedSpan
pub fn eq(self) -> OrderedSpan
Return a wrapper type that implements by-value equality rather than having all values be equal.
Sourcepub fn ord(self) -> OrderedSpan
pub fn ord(self) -> OrderedSpan
Return a wrapper type that implements by-value ordering rather than having all values be equal.
Sourcepub const fn is_missing(&self) -> bool
pub const fn is_missing(&self) -> bool
Check if the span is Self::MISSING.
Sourcepub fn byte_range(&self) -> Result<Range<u64>, MissingLocationError>
pub fn byte_range(&self) -> Result<Range<u64>, MissingLocationError>
Return the range of bytes in the original file,
or a MissingLocationError if missing.
Sourcepub fn byte_len(&self) -> Result<u64, MissingLocationError>
pub fn byte_len(&self) -> Result<u64, MissingLocationError>
Returns the length of the span in bytes,
or a MissingLocationError if missing.
Sourcepub fn slice_byte_indexes<R>(self, range: R) -> Self
pub fn slice_byte_indexes<R>(self, range: R) -> Self
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..); // panicsSourcepub fn start(&self) -> Location
pub fn start(&self) -> Location
The starting location of the span (inclusive),
or Location::MISSING if missing.
Sourcepub fn end(&self) -> Location
pub fn end(&self) -> Location
The ending location of the span (exclusive),
or Location::MISSING if missing.
Trait Implementations§
Source§impl From<ByteLocation> for Span
impl From<ByteLocation> for Span
Source§fn from(location: ByteLocation) -> Self
fn from(location: ByteLocation) -> Self
Source§impl From<OrderedSpan> for Span
impl From<OrderedSpan> for Span
Source§fn from(value: OrderedSpan) -> Self
fn from(value: OrderedSpan) -> Self
Source§impl From<SimpleSpan> for Span
impl From<SimpleSpan> for Span
Source§fn from(span: SimpleSpan) -> Self
fn from(span: SimpleSpan) -> Self
Source§impl From<Span> for OrderedSpan
impl From<Span> for OrderedSpan
Source§impl Hash for Span
Hashes the span.
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§impl Ord for Span
impl Ord for Span
Source§impl PartialEq<Span> for OrderedSpan
impl PartialEq<Span> for OrderedSpan
Source§impl PartialEq for Span
Unconditionally returns true,
so that adding a Span to a struct will not affect derive(Eq)
impl PartialEq for Span
Unconditionally returns true,
so that adding a Span to a struct will not affect derive(Eq)
Source§impl PartialOrd<Span> for OrderedSpan
impl PartialOrd<Span> for OrderedSpan
Source§impl PartialOrd for Span
impl PartialOrd for Span
Source§impl Span for Span
impl Span for Span
impl Copy for Span
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.