Unnamed repository; edit this file 'description' to name the repository.
Merge rust-analyzer/text-size#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]>
| -rw-r--r-- | lib/text-size/src/range.rs | 19 | ||||
| -rw-r--r-- | lib/text-size/tests/indexing.rs | 8 |
2 files changed, 25 insertions, 2 deletions
diff --git a/lib/text-size/src/range.rs b/lib/text-size/src/range.rs index 2ed608aa53..11fe1e702d 100644 --- a/lib/text-size/src/range.rs +++ b/lib/text-size/src/range.rs @@ -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)] } } diff --git a/lib/text-size/tests/indexing.rs b/lib/text-size/tests/indexing.rs new file mode 100644 index 0000000000..ebbed7700d --- /dev/null +++ b/lib/text-size/tests/indexing.rs @@ -0,0 +1,8 @@ +use text_size::*; + +#[test] +fn main() { + let range = TextRange::default(); + &""[range]; + &String::new()[range]; +} |