Skip to content

Commit

Permalink
Merge #35
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
bors[bot] and CAD97 authored Mar 26, 2020
2 parents 53123e5 + b1b7dc1 commit 192b316
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,29 @@ impl TextRange {
impl Index<TextRange> for str {
type Output = str;
#[inline]
fn index(&self, index: TextRange) -> &Self::Output {
fn index(&self, index: TextRange) -> &str {
&self[Range::<usize>::from(index)]
}
}

impl Index<TextRange> for String {
type Output = str;
#[inline]
fn index(&self, index: TextRange) -> &str {
&self[Range::<usize>::from(index)]
}
}

impl IndexMut<TextRange> for str {
#[inline]
fn index_mut(&mut self, index: TextRange) -> &mut Self::Output {
fn index_mut(&mut self, index: TextRange) -> &mut str {
&mut self[Range::<usize>::from(index)]
}
}

impl IndexMut<TextRange> for String {
#[inline]
fn index_mut(&mut self, index: TextRange) -> &mut str {
&mut self[Range::<usize>::from(index)]
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/indexing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use text_size::*;

#[test]
fn main() {
let range = TextRange::default();
&""[range];
&String::new()[range];
}

0 comments on commit 192b316

Please sign in to comment.