Struct Span

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

Represents a specific range of text within a Source.

Internally Span is extremely lightweight and is essentially just a reference to a Source and a range of bytes within that source, so it can be cheaply cloned and passed around without issue. The underlying Source mechanism is stored within an Rc so that it can be shared between multiple Spans without needing to be cloned. This cheap sharing, combined with the lack of any sort of tokenization in Quoth allows us to provide direct access to the original, unmodified source text for any given Span.

Spans can be created directly using Span::new, or by using the Spanned trait to access the underlying Span of a type.

use quoth::*;
use std::rc::Rc;

let span = Span::new(Rc::new(Source::from_str("Hello, world!")), 0..5);

Spans can be joined together using the Span::join method, which will return a new Span that encompasses both of the original spans. This can be useful for combining spans that were generated from different parts of the same source.

use quoth::*;
use std::rc::Rc;

let source = Rc::new(Source::from_str("Hello, world!"));
let span1 = Span::new(source.clone(), 0..5);
let span2 = Span::new(source.clone(), 7..12);
let encompassing_span = span1.join(&span2).unwrap();
assert_eq!(encompassing_span.source_text(), "Hello, world");

Implementations§

Source§

impl Span

Source

pub fn blank() -> Span

Returns a blank Span with no source and a zero-length range.

Note that blank Spans are special in that they can be joined with a Span from any Source without error, and will always return the other Span as the result.

Source

pub fn new(source: Rc<Source>, byte_range: Range<usize>) -> Self

Creates a new Span from a Source and a byte range.

Source

pub fn source(&self) -> &Source

Returns the Source that this Span is associated with.

Source

pub fn source_text(&self) -> IndexedSlice<'_>

Returns the text of the Source that this Span is associated with.

Source

pub fn source_path(&self) -> Option<&Path>

Returns the path of the Source that this Span is associated with, if it has one.

Source

pub fn byte_range(&self) -> &Range<usize>

Returns the byte range of this Span, representing the start and end of the span within the Source.

Note that because of UTF-8, the start and end of the range may not correspond with the start and end of a character in the source text.

Source

pub fn start(&self) -> LineCol

Returns the line and column of the start of this Span within the Source.

Source

pub fn end(&self) -> LineCol

Returns the line and column of the end of this Span within the Source.

Source

pub fn source_lines( &self, ) -> impl Iterator<Item = (IndexedSlice<'_>, Range<usize>)> + '_

Returns an iterator over the lines of the Source that this Span is associated with,

Source

pub fn join(&self, other: &Span) -> Result<Span, SpanJoinError>

Joins this Span with another Span, returning a new Span that encompasses both.

If the two spans do not come from the same Source, this method will return an error unless one or more of the spans is Span::blank().

Source

pub fn is_blank(&self) -> bool

Returns whether this Span is blank, i.e. has a zero-length range.

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 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: &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 Spanned for Span

Source§

fn span(&self) -> Span

Returns the underlying Span of self. Read more
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<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, 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.