Skip to content

Commit a93dd2a

Browse files
committed
Add a From<TextRange> impl
1 parent d0560c7 commit a93dd2a

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/range.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use {
22
crate::TextSize,
33
std::{
44
cmp, fmt,
5-
ops::{Bound, Index, IndexMut, RangeBounds},
5+
ops::{Bound, Index, IndexMut, Range, RangeBounds},
66
},
77
};
88

@@ -127,20 +127,16 @@ impl TextRange {
127127
}
128128
}
129129

130-
fn ix(size: TextSize) -> usize {
131-
size.into()
132-
}
133-
134130
impl Index<TextRange> for str {
135131
type Output = str;
136132
fn index(&self, index: TextRange) -> &Self::Output {
137-
&self[ix(index.start())..ix(index.end())]
133+
&self[Range::<usize>::from(index)]
138134
}
139135
}
140136

141137
impl IndexMut<TextRange> for str {
142138
fn index_mut(&mut self, index: TextRange) -> &mut Self::Output {
143-
&mut self[ix(index.start())..ix(index.end())]
139+
&mut self[Range::<usize>::from(index)]
144140
}
145141
}
146142

@@ -153,3 +149,12 @@ impl RangeBounds<TextSize> for TextRange {
153149
Bound::Excluded(&self.end)
154150
}
155151
}
152+
153+
impl<T> From<TextRange> for Range<T>
154+
where
155+
T: From<TextSize>,
156+
{
157+
fn from(r: TextRange) -> Self {
158+
r.start().into()..r.end().into()
159+
}
160+
}

0 commit comments

Comments
 (0)