Struct Fragment

Source
pub struct Fragment {
    pub source: SourceRef,
    pub range: Range<usize>,
}
Available on crate feature source-tracking only.
Expand description

A fragment of source code.

This can be part of (or all of) a Source.

Fields§

§source: SourceRef

The Source that this fragment is in.

§range: Range<usize>

Fragments are represented using byte ranges in the Source referenced by Fragment::source.

This Fragment is considered invalid if this range is out of order or either end of it is not on a char boundary in source according to str::is_char_boundary.

Implementations§

Source§

impl Fragment

Source

pub fn is_valid(&self) -> bool

Check that this Fragment is valid, and references a real existing (though possibly empty) part of the Fragment::source.

Source

pub fn as_str(&self) -> &str

Get the str represented by this Fragment.

§Panics
Source

pub const fn len(&self) -> usize

Get the length (in bytes) of this Fragment. Does not check this Fragment for validity.

Source

pub const fn is_empty(&self) -> bool

Check if this fragment has a Fragment::len == 0. Does not check this Fragment for validity.

Source

pub fn is_empty_at_end_of_source(&self) -> bool

Check if this fragment is empty at the end of it’s source.

Uses debug_assert to check for validity.

Source

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

Return true if this Fragment entirely contains another Fragment and they’re from the same Source by Source::id.

If other is empty, it can still be considered to be contained in this Fragment if its Fragment::range is entirely within self’s Fragment::range (basically whether the location of the empty fragment is in this one).

Source

pub fn offset_from(&self, origin: &Self) -> usize

Get the number of bytes between the beginning of origin and the beginning of self.

§Panics:
Source

pub fn chars(&self) -> Chars<'_>

Get a Chars Iterator over the characters in this Fragment.

Source

pub fn trimmed(self) -> Self

Get a sub-fragment of this fragment (see Fragment::contains) with the whitespace at either end trimmed off. This will return the fragment unchanged if it is empty.

This calls Fragment::trim_start and then Fragment::trim_end internally and should match the behavior of str::trim.

If this returns an empty Fragment it will be at the end of the parent Fragment.

Source

pub fn trim_end(self) -> Self

Get a sub-fragment of this fragment (see Fragment::contains) with the whitespace trimmed off the end. This will return it unchanged if empty.

See str::trim_end for exact behaviors.

Source

pub fn trim_start(self) -> Self

Get a sub-fragment of this fragment (see Fragment::contains) with the whitespace trimmed off the start. This will return it unchanged if empty.

See str::trim_start for exact behaviors.

Source

pub fn split_at(&self, bytes_from_start: usize) -> (Self, Self)

Split this Fragment into two sub-Fragments, the left containing the first bytes_from_start bytes, and the right containing the rest.

§Panics
  • This will panic if the provided bytes_from_start does not land on a unicode character boundary or is larger than the length of this fragment according to str::is_char_boundary.
Source

pub fn split_at_unchecked(&self, bytes_from_start: usize) -> (Self, Self)

This is the same as Fragment::split_at except it does not check that the created fragments are valid or that either can call Fragment::as_str without panicking. Use with caution.

Note that this is not technically unsafe, since all bugs that may emerge from abuse/misuse here are logic bugs (not memory or concurrency bugs).

Source

pub fn advance_by(&mut self, bytes: usize)

Move the start of this Fragment forward by a given number of bytes.

§Panics
  • Panics if the advancing by bytes would create an invalid Fragment.
Source

pub fn advance_by_unchecked(&mut self, bytes: usize)

This is the same as Fragment::advance_by except without the bounds checking. Use carefully or the updated Fragment will be invalid.

Source

pub fn retain(&mut self, bytes: usize)

Retain up to bytes bytes of this Fragment.

§Panics
  • Panics if the updated Fragment would be invalid.
Source

pub fn retain_unchecked(&mut self, bytes: usize)

This is the same as Fragment::retain except without the bounds checking. Use carefully or the updated Fragment will be invalid.

Source

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

Get a Range of line indices (0-indexed, see Source::get_line) that this fragment overlaps.

Source

pub fn starts_on_line(&self) -> usize

Get the line number (not index) that this line starts on.

This re-calculates Fragment::line_indices, which may be expensive on very large files, so use with care.

Source

pub fn starting_col_index(&self) -> usize

Get the number of bytes between the start of the line that this Fragment starts on and the start of this Fragment

Source

pub fn cover(lhs: Fragment, rhs: Fragment) -> Fragment

Compute the “cover” over two Fragments – that is, the Fragment containing both of them and all the source code in between.

§Panics
  • If the Fragments are from different sources.

Trait Implementations§

Source§

impl Clone for Fragment

Source§

fn clone(&self) -> Fragment

Returns a copy 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 Fragment

Source§

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

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

impl Display for Fragment

Source§

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

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

impl PartialEq for Fragment

Source§

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

Fragment equality is based on referencing the same Source using Arc::ptr_eq and having the same Fragment::range.

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 Eq for Fragment

Auto Trait Implementations§

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

Checks if this value is equivalent to the given key. 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> 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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> 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.