Unnamed repository; edit this file 'description' to name the repository.
-rw-r--r--lib/text-size/src/range.rs19
-rw-r--r--lib/text-size/tests/indexing.rs8
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];
+}