Unnamed repository; edit this file 'description' to name the repository.
implement RangeBounds for TextRange
| -rw-r--r-- | lib/text-size/src/lib.rs | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/lib/text-size/src/lib.rs b/lib/text-size/src/lib.rs index 73e076923f..6d08649751 100644 --- a/lib/text-size/src/lib.rs +++ b/lib/text-size/src/lib.rs @@ -220,14 +220,6 @@ pub struct TextRange { end: TextUnit, } -impl TextRange { - #[inline(always)] - pub fn checked_sub(self, other: TextUnit) -> Option<TextRange> { - let res = TextRange::offset_len(self.start().checked_sub(other)?, self.len()); - Some(res) - } -} - impl fmt::Debug for TextRange { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { <Self as fmt::Display>::fmt(self, f) @@ -307,6 +299,25 @@ impl TextRange { pub fn contains_inclusive(&self, offset: TextUnit) -> bool { self.start() <= offset && offset <= self.end() } + + #[inline(always)] + pub fn checked_sub(self, other: TextUnit) -> Option<TextRange> { + let res = TextRange::offset_len( + self.start().checked_sub(other)?, + self.len() + ); + Some(res) + } +} + +impl ops::RangeBounds<TextUnit> for TextRange { + fn start_bound(&self) -> ops::Bound<&TextUnit> { + ops::Bound::Included(&self.start) + } + + fn end_bound(&self) -> ops::Bound<&TextUnit> { + ops::Bound::Excluded(&self.end) + } } impl ops::Index<TextRange> for str { |