Skip to content

Commit 192b316

Browse files
bors[bot]CAD97
andauthored
Merge #35
35: Re-add Index<TextRange> for String r=matklad a=CAD97 These exist in `text_unit`, and seem to be required to index `String`. (Why doesn't deref coersion kick in here? I don't know.) Co-authored-by: CAD97 <[email protected]>
2 parents 53123e5 + b1b7dc1 commit 192b316

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/range.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,29 @@ impl TextRange {
305305
impl Index<TextRange> for str {
306306
type Output = str;
307307
#[inline]
308-
fn index(&self, index: TextRange) -> &Self::Output {
308+
fn index(&self, index: TextRange) -> &str {
309+
&self[Range::<usize>::from(index)]
310+
}
311+
}
312+
313+
impl Index<TextRange> for String {
314+
type Output = str;
315+
#[inline]
316+
fn index(&self, index: TextRange) -> &str {
309317
&self[Range::<usize>::from(index)]
310318
}
311319
}
312320

313321
impl IndexMut<TextRange> for str {
314322
#[inline]
315-
fn index_mut(&mut self, index: TextRange) -> &mut Self::Output {
323+
fn index_mut(&mut self, index: TextRange) -> &mut str {
324+
&mut self[Range::<usize>::from(index)]
325+
}
326+
}
327+
328+
impl IndexMut<TextRange> for String {
329+
#[inline]
330+
fn index_mut(&mut self, index: TextRange) -> &mut str {
316331
&mut self[Range::<usize>::from(index)]
317332
}
318333
}

tests/indexing.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use text_size::*;
2+
3+
#[test]
4+
fn main() {
5+
let range = TextRange::default();
6+
&""[range];
7+
&String::new()[range];
8+
}

0 commit comments

Comments
 (0)