pub struct TextRange { /* private fields */ }
Expand description
A range in text, represented as a pair of TextSize
.
It is a logic error for start
to be greater than end
.
Implementations
impl TextRange
impl TextRange
pub fn new(start: TextSize, end: TextSize) -> TextRange
pub fn new(start: TextSize, end: TextSize) -> TextRange
Creates a new TextRange
with the given start
and end
(start..end
).
Panics
Panics if end < start
.
Examples
let start = TextSize::from(5);
let end = TextSize::from(10);
let range = TextRange::new(start, end);
assert_eq!(range.start(), start);
assert_eq!(range.end(), end);
assert_eq!(range.len(), end - start);
pub fn at(offset: TextSize, len: TextSize) -> TextRange
pub fn at(offset: TextSize, len: TextSize) -> TextRange
Create a new TextRange
with the given offset
and len
(offset..offset + len
).
Examples
let text = "0123456789";
let offset = TextSize::from(2);
let length = TextSize::from(5);
let range = TextRange::at(offset, length);
assert_eq!(range, TextRange::new(offset, offset + length));
assert_eq!(&text[range], "23456")
impl TextRange
impl TextRange
Identity methods.
impl TextRange
impl TextRange
Manipulation methods.
pub fn contains(self, offset: TextSize) -> bool
pub fn contains(self, offset: TextSize) -> bool
Check if this range contains an offset.
The end index is considered excluded.
Examples
let (start, end): (TextSize, TextSize);
let range = TextRange::new(start, end);
assert!(range.contains(start));
assert!(!range.contains(end));
pub fn contains_inclusive(self, offset: TextSize) -> bool
pub fn contains_inclusive(self, offset: TextSize) -> bool
Check if this range contains an offset.
The end index is considered included.
Examples
let (start, end): (TextSize, TextSize);
let range = TextRange::new(start, end);
assert!(range.contains_inclusive(start));
assert!(range.contains_inclusive(end));
pub fn contains_range(self, other: TextRange) -> bool
pub fn contains_range(self, other: TextRange) -> bool
Check if this range completely contains another range.
Examples
let larger = TextRange::new(0.into(), 20.into());
let smaller = TextRange::new(5.into(), 15.into());
assert!(larger.contains_range(smaller));
assert!(!smaller.contains_range(larger));
// a range always contains itself
assert!(larger.contains_range(larger));
assert!(smaller.contains_range(smaller));
pub fn intersect(self, other: TextRange) -> Option<TextRange>
pub fn intersect(self, other: TextRange) -> Option<TextRange>
The range covered by both ranges, if it exists. If the ranges touch but do not overlap, the output range is empty.
Examples
assert_eq!(
TextRange::intersect(
TextRange::new(0.into(), 10.into()),
TextRange::new(5.into(), 15.into()),
),
Some(TextRange::new(5.into(), 10.into())),
);
pub fn cover(self, other: TextRange) -> TextRange
pub fn cover(self, other: TextRange) -> TextRange
Extends the range to cover other
as well.
Examples
assert_eq!(
TextRange::cover(
TextRange::new(0.into(), 5.into()),
TextRange::new(15.into(), 20.into()),
),
TextRange::new(0.into(), 20.into()),
);
pub fn cover_offset(self, offset: TextSize) -> TextRange
pub fn cover_offset(self, offset: TextSize) -> TextRange
Extends the range to cover other
offsets as well.
Examples
assert_eq!(
TextRange::empty(0.into()).cover_offset(20.into()),
TextRange::new(0.into(), 20.into()),
)
pub fn checked_add(self, offset: TextSize) -> Option<TextRange>
pub fn checked_add(self, offset: TextSize) -> Option<TextRange>
Add an offset to this range.
Note that this is not appropriate for changing where a TextRange
is
within some string; rather, it is for changing the reference anchor
that the TextRange
is measured against.
The unchecked version (Add::add
) will always panic on overflow,
in contrast to primitive integers, which check in debug mode only.
pub fn checked_sub(self, offset: TextSize) -> Option<TextRange>
pub fn checked_sub(self, offset: TextSize) -> Option<TextRange>
Subtract an offset from this range.
Note that this is not appropriate for changing where a TextRange
is
within some string; rather, it is for changing the reference anchor
that the TextRange
is measured against.
The unchecked version (Sub::sub
) will always panic on overflow,
in contrast to primitive integers, which check in debug mode only.
pub fn ordering(self, other: TextRange) -> Ordering
pub fn ordering(self, other: TextRange) -> Ordering
Relative order of the two ranges (overlapping ranges are considered equal).
This is useful when, for example, binary searching an array of disjoint ranges.
Examples
let a = TextRange::new(0.into(), 3.into());
let b = TextRange::new(4.into(), 5.into());
assert_eq!(a.ordering(b), Ordering::Less);
let a = TextRange::new(0.into(), 3.into());
let b = TextRange::new(3.into(), 5.into());
assert_eq!(a.ordering(b), Ordering::Less);
let a = TextRange::new(0.into(), 3.into());
let b = TextRange::new(2.into(), 5.into());
assert_eq!(a.ordering(b), Ordering::Equal);
let a = TextRange::new(0.into(), 3.into());
let b = TextRange::new(2.into(), 2.into());
assert_eq!(a.ordering(b), Ordering::Equal);
let a = TextRange::new(2.into(), 3.into());
let b = TextRange::new(2.into(), 2.into());
assert_eq!(a.ordering(b), Ordering::Greater);
Trait Implementations
impl<'_, T> Add<T> for &'_ TextRange where
TextRange: Add<T>,
<TextRange as Add<T>>::Output == TextRange,
impl<'_, T> Add<T> for &'_ TextRange where
TextRange: Add<T>,
<TextRange as Add<T>>::Output == TextRange,
impl<A> AddAssign<A> for TextRange where
TextRange: Add<A>,
<TextRange as Add<A>>::Output == TextRange,
impl<A> AddAssign<A> for TextRange where
TextRange: Add<A>,
<TextRange as Add<A>>::Output == TextRange,
fn add_assign(&mut self, rhs: A)
fn add_assign(&mut self, rhs: A)
Performs the +=
operation. Read more
impl RangeBounds<TextSize> for TextRange
impl RangeBounds<TextSize> for TextRange
impl<'_, T> Sub<T> for &'_ TextRange where
TextRange: Sub<T>,
<TextRange as Sub<T>>::Output == TextRange,
impl<'_, T> Sub<T> for &'_ TextRange where
TextRange: Sub<T>,
<TextRange as Sub<T>>::Output == TextRange,
impl<S> SubAssign<S> for TextRange where
TextRange: Sub<S>,
<TextRange as Sub<S>>::Output == TextRange,
impl<S> SubAssign<S> for TextRange where
TextRange: Sub<S>,
<TextRange as Sub<S>>::Output == TextRange,
fn sub_assign(&mut self, rhs: S)
fn sub_assign(&mut self, rhs: S)
Performs the -=
operation. Read more
impl Copy for TextRange
impl Eq for TextRange
impl StructuralEq for TextRange
impl StructuralPartialEq for TextRange
Auto Trait Implementations
impl RefUnwindSafe for TextRange
impl Send for TextRange
impl Sync for TextRange
impl Unpin for TextRange
impl UnwindSafe for TextRange
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> Cast for T
impl<T> Cast for T
fn cast<U>(self, interner: <U as HasInterner>::Interner) -> U where
Self: CastTo<U>,
U: HasInterner,
fn cast<U>(self, interner: <U as HasInterner>::Interner) -> U where
Self: CastTo<U>,
U: HasInterner,
Cast a value to type U
using CastTo
.
sourceimpl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to key
and return true
if they are equal.
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> Pointable for T
impl<T> Pointable for T
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more