Skip to main content

Span

Struct Span 

Source
pub struct Span<'src> {
    source: &'src str,
    byte_index: usize,
    byte_length: usize,
}
Expand description

A struct that spans over a &str, written as Span<'src>.

byte_index and byte_length both refer to the bytes of the &str, not the utf8 characters of the &str.


We shall name this Span struct Spanley. Spanley is our child now.

Spanley knows where it is with byte_index and Spanley knows when to stop with byte_length.

If source dies, Spanley dies with it. Do not let source die, for Spanley’s sake.

Fields§

§source: &'src str§byte_index: usize§byte_length: usize

Implementations§

Source§

impl<'src> Span<'src>

Source

pub fn new( source: &'src str, char_index: usize, char_length: usize, ) -> Result<Self, SpanError>

Birth a new Spanley. The future is now.

This method takes in a char_index and a char_length over the utf8 characters of the &str whilst the Span stores a byte_index and a byte_length internally over the bytes of the &str.

§Error Handling

This method will return SpanError::Empty if char_length is 0. Empty spans are considered purely invalid.

This method will return SpanError::OutOfBounds if char_index is out of bounds for source or if char_index + char_length would not result in a value above usize::MAX and the last valid indice, at char_index + char_length - 1usize, is out of bounds for source.

This method will return SpanError::Overflow if char_index + char_length would be above usize::MAX, or if byte_index, byte_length or both added together would be above usize::MAX.

Source

pub fn new_encompassing(lhs: &Self, rhs: &Self) -> Result<Self, SpanError>

Birth a Spanley which encompasses two other Spanleys. You see, when two Spanleys love each other very much…

In more technical terms, it will construct a span with a start at the minimum start index of both spans, and end at the maximum end index of both spans.

The constructed Span will therefore also contain anything in the gap between the two input Spans, if there is one.

§Example

Assuming there are two Spans, one contains the word span and the other contains the word another_span from the following string:

"Here is a span and yet another_span, yippe!"
           ^^^^         ^^^^^^^^^^^^

Then the constructed Span would contain the following:

"Here is a span and yet another_span, yippe!"
           ^^^^^^^^^^^^^^^^^^^^^^^^^
§Error Handling

The two operands here are expected to share the same source, if that is not so, this method will return SpanError::MismatchedSource

Source

pub fn overlaps(lhs: &Self, rhs: &Self) -> bool

Checks for overlaps between two Spans.

This method does not validate that both operands share the same source like Span::new_encompassing does, and only compares indices to check for an overlap.

Friendly reminder that Span::end is exclusive.

Source

pub fn get_start_location(&self) -> SpanLocation

Available on crate feature location only.

Constructs a SpanLocation using SpanLocation::from_index_unchecked at Span::start.

This is done without the bounds check in SpanLocation because the Span bounds were already validated at construction.

Source

pub fn get_end_location(&self) -> SpanLocation

Available on crate feature location only.

Constructs a SpanLocation using SpanLocation::from_index_unchecked at Span::end.

This is done without the bounds check in SpanLocation because the Span bounds were already validated at construction.

Source

pub fn start(&self) -> usize

Returns byte_index, which corresponds to the start of the Span.

Source

pub fn end(&self) -> usize

Returns the end index of the Span. The end index is the very next index after the latest contained index and is therefore exclusive.

Calculated by adding together byte_index and byte_length.

Source

pub fn len(&self) -> usize

Returns byte_length.

This is a length in bytes and not in utf8 characters and thus may not be what a human considers the length of the span.

Source

pub fn is_empty(&self) -> bool

Returns true when byte_length is 0.

Source

pub fn source(&self) -> &str

Returns the Span’s internal private source field.

Source

pub fn content(&self) -> &str

Returns a string slice over the contents of the Span.

Source

pub fn get_context(&self, extra_lines: u8) -> Self

This method is meant for logging purposes, it will construct a new Span containing all the lines the current Span spans over, in addition to a varying amount of lines of extra margin at the start and end.

extra_lines is a u8 here because… realistically, we will never be working with a &str that has more than usize::MAX lines. For this reason, the maximum possible amount of extra_lines is of a little below usize::MAX / 2. Additionally, I do not believe you will ever need more than 255 lines of extra context, you freaks.

§Examples

Assuming the current Span starts at the word span on line 3 and ends with the word span on line 4 in the following text:

1 | Here's a lines at the very very start!
2 | Here's a line right before the lines with the span.
3 | Here's the line where the span starts.
                              ^^^^^^^^^^^^
4 | Here's the line where the span ends.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 | Here's a line right after the lines with the span.
6 | Here is the final line.

The resulting Span with a provided extra_lines of 1 will contain the following:

1 | Here's a lines at the very very start!
2 | Here's a line right before the lines with the span.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 | Here's the line where the span starts.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 | Here's the line where the span ends.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 | Here's a line right after the lines with the span.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6 | Here is the final line.

And with extra_lines set to 0:

1 | Here's a lines at the very very start!
2 | Here's a line right before the lines with the span.
3 | Here's the line where the span starts.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 | Here's the line where the span ends.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 | Here's a line right after the lines with the span.
6 | Here is the final line.

Trait Implementations§

Source§

impl<'src> Clone for Span<'src>

Source§

fn clone(&self) -> Span<'src>

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 Display for Span<'_>

Source§

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

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

impl<'src> PartialEq for Span<'src>

Source§

fn eq(&self, other: &Span<'src>) -> 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<'src> Copy for Span<'src>

Source§

impl<'src> Eq for Span<'src>

Source§

impl<'src> StructuralPartialEq for Span<'src>

Auto Trait Implementations§

§

impl<'src> Freeze for Span<'src>

§

impl<'src> RefUnwindSafe for Span<'src>

§

impl<'src> Send for Span<'src>

§

impl<'src> Sync for Span<'src>

§

impl<'src> Unpin for Span<'src>

§

impl<'src> UnsafeUnpin for Span<'src>

§

impl<'src> UnwindSafe for Span<'src>

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

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 32 bytes