Span

Struct Span 

Source
pub struct Span<T> {
    pub start: usize,
    pub end: usize,
    /* private fields */
}
Expand description

A window in a [T] sequence.

Note that the range covered by a Span is end-exclusive, meaning that the end index is not included in the range covered by the Span. If you’re familiar with the Rust range syntax, you could say the span covers the equivalent of start..end, not start..=end.

For a Span to be correct, its end index must be greater than or equal to its start index. Creating or using a Span which does not follow this rule may lead to unexpected behavior or panics.

Although specific to harper.js, this page may clear up any questions you have.

Fields§

§start: usize

The start index of the span.

§end: usize

The end index of the span.

Note that Span represents an exclusive range. This means that a Span::new(0, 5) will cover the values 0, 1, 2, 3, 4; it will not cover the 5.

Implementations§

Source§

impl<T> Span<T>

Source

pub const EMPTY: Self

An empty Span.

Source

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

Creates a new Span with the provided start and end indices.

§Panics

This will panic if start is greater than end.

Source

pub fn new_with_len(start: usize, len: usize) -> Self

Creates a new Span from the provided start position and length.

Source

pub fn len(&self) -> usize

The length of the Span.

Source

pub fn is_empty(&self) -> bool

Checks whether the Span is empty.

A Span is considered empty if it has a length of 0.

Source

pub fn contains(&self, idx: usize) -> bool

Checks whether idx is within the range of the span.

Source

pub fn overlaps_with(&self, other: Self) -> bool

Checks whether this span’s range overlaps with other.

Source

pub fn try_get_content<'a>(&self, source: &'a [T]) -> Option<&'a [T]>

Get the associated content. Will return None if the span is non-empty and any aspect is invalid.

Source

pub fn expand_to_include(&mut self, target: usize)

Expand the span by either modifying Self::start or Self::end to include the target index.

Does nothing if the span already includes the target.

Source

pub fn get_content<'a>(&self, source: &'a [T]) -> &'a [T]

Get the associated content. Will panic if any aspect is invalid.

Source

pub fn set_len(&mut self, length: usize)

Set the span’s length.

Source

pub fn with_len(&self, length: usize) -> Self

Returns a copy of this Span with a new length.

Source

pub fn push_by(&mut self, by: usize)

Add an amount to both Self::start and Self::end

Source

pub fn pull_by(&mut self, by: usize)

Subtract an amount from both Self::start and Self::end

Source

pub fn pushed_by(&self, by: usize) -> Self

Add an amount to a copy of both Self::start and Self::end

Source

pub fn pulled_by(&self, by: usize) -> Option<Self>

Subtract an amount to a copy of both Self::start and Self::end

Source§

impl<T: Display + Debug> Span<T>

Additional functions for types that implement std::fmt::Debug and Display.

Source

pub fn get_content_string(&self, source: &[T]) -> String

Gets the content of this Span<T> as a String.

Source§

impl Span<Token>

Functionality specific to Token spans.

Source

pub fn to_char_span(&self, source_document_tokens: &[Token]) -> Span<char>

Converts the Span<Token> into a Span<char>.

This requires knowing the character spans of the tokens covered by this Span<Token>. Because of this, a reference to the source token sequence used to create this span is required.

Trait Implementations§

Source§

impl<T> Clone for Span<T>

Source§

fn clone(&self) -> Self

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<T: Debug> Debug for Span<T>

Source§

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

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

impl<T: Default> Default for Span<T>

Source§

fn default() -> Span<T>

Returns the “default value” for a type. Read more
Source§

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

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> From<Range<usize>> for Span<T>

Source§

fn from(value: Range<usize>) -> Self

Reinterprets the provided std::ops::Range as a Span.

Source§

impl<T> From<Span<T>> for Range<usize>

Source§

fn from(value: Span<T>) -> Self

Converts the Span to an std::ops::Range.

Source§

impl FromIterator<Span<char>> for Mask

Source§

fn from_iter<T: IntoIterator<Item = Span<char>>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<T> IntoIterator for Span<T>

Source§

fn into_iter(self) -> Self::IntoIter

Converts the Span into an iterator that yields the indices covered by its range.

Note that Span is half-open, meaning that the value Self::end will not be yielded by this iterator: it will stop at the index immediately preceding Self::end.

Source§

type Item = usize

The type of the elements being iterated over.
Source§

type IntoIter = Range<usize>

Which kind of iterator are we turning this into?
Source§

impl<T: PartialEq> PartialEq for Span<T>

Source§

fn eq(&self, other: &Span<T>) -> 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<T> Serialize for Span<T>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T> Copy for Span<T>

Source§

impl<T: Eq> Eq for Span<T>

Source§

impl<T> StructuralPartialEq for Span<T>

Auto Trait Implementations§

§

impl<T> Freeze for Span<T>

§

impl<T> RefUnwindSafe for Span<T>
where T: RefUnwindSafe,

§

impl<T> Send for Span<T>
where T: Send,

§

impl<T> Sync for Span<T>
where T: Sync,

§

impl<T> Unpin for Span<T>
where T: Unpin,

§

impl<T> UnwindSafe for Span<T>
where T: UnwindSafe,

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> 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<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> LSend for T
where T: ?Sized,