pub struct Fragment {
pub source: SourceRef,
pub range: Range<usize>,
}
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
impl Fragment
Sourcepub fn is_valid(&self) -> bool
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.
Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Get the str represented by this Fragment.
§Panics
- This will panic in the unlikely event that Fragment::range is out of bounds or lands between char boundaries for Fragment::source.
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Check if this fragment has a Fragment::len
== 0
.
Does not check this Fragment for validity.
Sourcepub fn contains(&self, other: &Self) -> bool
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).
Sourcepub fn offset_from(&self, origin: &Self) -> usize
pub fn offset_from(&self, origin: &Self) -> usize
Get the number of bytes between the beginning of origin
and the beginning of self
.
§Panics:
- Panics if
self
is not a Fragment withinorigin
according toFragment::contains
.
Sourcepub fn trimmed(self) -> Self
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.
Sourcepub fn trim_end(self) -> Self
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.
Sourcepub fn trim_start(self) -> Self
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.
Sourcepub fn split_at(&self, bytes_from_start: usize) -> (Self, Self)
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.
Sourcepub fn split_at_unchecked(&self, bytes_from_start: usize) -> (Self, Self)
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.
Sourcepub fn advance_by(&mut self, bytes: usize)
pub fn advance_by(&mut self, bytes: usize)
Sourcepub fn advance_by_unchecked(&mut self, bytes: usize)
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 created Fragments will be invalid.
Sourcepub fn retain_unchecked(&mut self, bytes: usize)
pub fn retain_unchecked(&mut self, bytes: usize)
This is the same as Fragment::retain except without the bounds checking. Use carefully or the created Fragments will be invalid.
Sourcepub fn line_indices(&self) -> Range<usize>
pub fn line_indices(&self) -> Range<usize>
Get a Range of line indices (0-indexed, see Source::get_line) that this fragment overlaps.
Sourcepub fn starts_on_line(&self) -> usize
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.
Sourcepub fn starting_col_index(&self) -> usize
pub fn starting_col_index(&self) -> usize
Trait Implementations§
Source§impl PartialEq for Fragment
impl PartialEq for Fragment
Source§fn eq(&self, other: &Self) -> bool
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.
impl Eq for Fragment
Auto Trait Implementations§
impl Freeze for Fragment
impl RefUnwindSafe for Fragment
impl Send for Fragment
impl Sync for Fragment
impl Unpin for Fragment
impl UnwindSafe for Fragment
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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