From 5ef8391b680be9ab20f3a1457c68d9dd5122a16e Mon Sep 17 00:00:00 2001 From: CAD97 Date: Wed, 25 Mar 2020 21:41:48 -0400 Subject: [PATCH 1/2] Re-add Index for String --- src/range.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/range.rs b/src/range.rs index 2ed608a..11fe1e7 100644 --- a/src/range.rs +++ b/src/range.rs @@ -305,14 +305,29 @@ impl TextRange { impl Index for str { type Output = str; #[inline] - fn index(&self, index: TextRange) -> &Self::Output { + fn index(&self, index: TextRange) -> &str { + &self[Range::::from(index)] + } +} + +impl Index for String { + type Output = str; + #[inline] + fn index(&self, index: TextRange) -> &str { &self[Range::::from(index)] } } impl IndexMut 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::::from(index)] + } +} + +impl IndexMut for String { + #[inline] + fn index_mut(&mut self, index: TextRange) -> &mut str { &mut self[Range::::from(index)] } } From b1b7dc101aabf13edfcb97cd756bc659334cd79e Mon Sep 17 00:00:00 2001 From: CAD97 Date: Thu, 26 Mar 2020 17:03:04 -0400 Subject: [PATCH 2/2] Add test for indexing --- tests/indexing.rs | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 tests/indexing.rs diff --git a/tests/indexing.rs b/tests/indexing.rs new file mode 100644 index 0000000..ebbed77 --- /dev/null +++ b/tests/indexing.rs @@ -0,0 +1,8 @@ +use text_size::*; + +#[test] +fn main() { + let range = TextRange::default(); + &""[range]; + &String::new()[range]; +}