about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAriel Davis <ariel.z.davis@icloud.com>2023-05-04 16:40:41 -0700
committerAriel Davis <ariel.z.davis@icloud.com>2023-05-06 00:49:23 -0700
commitda5c63c8f90b7cf66d94626352141dc3f0585b6e (patch)
tree24aa500efc782fcbf19223a0f3ea2bd62ff9ed37
parentd9c88460e448efab4f784187f8bcf95c5a945673 (diff)
downloadrust-da5c63c8f90b7cf66d94626352141dc3f0585b6e.tar.gz
rust-da5c63c8f90b7cf66d94626352141dc3f0585b6e.zip
Use boxed slice
As well as doing the shrink_to_fit, we also don't have to keep track of
the capacity anymore.
-rw-r--r--lib/line-index/src/lib.rs18
1 files changed, 8 insertions, 10 deletions
diff --git a/lib/line-index/src/lib.rs b/lib/line-index/src/lib.rs
index 1a4753cbd67..e31f3006e21 100644
--- a/lib/line-index/src/lib.rs
+++ b/lib/line-index/src/lib.rs
@@ -13,9 +13,9 @@ pub use text_size::{TextRange, TextSize};
 #[derive(Clone, Debug, PartialEq, Eq)]
 pub struct LineIndex {
     /// Offset the beginning of each line, zero-based.
-    newlines: Vec<TextSize>,
+    newlines: Box<[TextSize]>,
     /// List of non-ASCII characters on each line.
-    line_wide_chars: IntMap<u32, Vec<WideChar>>,
+    line_wide_chars: IntMap<u32, Box<[WideChar]>>,
 }
 
 /// Line/Column information in native, utf8 format.
@@ -97,7 +97,8 @@ impl LineIndex {
 
                 // Save any utf-16 characters seen in the previous line
                 if !wide_chars.is_empty() {
-                    line_wide_chars.insert(line, std::mem::take(&mut wide_chars));
+                    line_wide_chars
+                        .insert(line, std::mem::take(&mut wide_chars).into_boxed_slice());
                 }
 
                 // Prepare for processing the next line
@@ -115,13 +116,10 @@ impl LineIndex {
 
         // Save any utf-16 characters seen in the last line
         if !wide_chars.is_empty() {
-            line_wide_chars.insert(line, wide_chars);
+            line_wide_chars.insert(line, wide_chars.into_boxed_slice());
         }
 
-        newlines.shrink_to_fit();
-        line_wide_chars.shrink_to_fit();
-
-        LineIndex { newlines, line_wide_chars }
+        LineIndex { newlines: newlines.into_boxed_slice(), line_wide_chars }
     }
 
     /// Transforms the `TextSize` into a `LineCol`.
@@ -168,7 +166,7 @@ impl LineIndex {
     fn utf8_to_wide_col(&self, enc: WideEncoding, line: u32, col: TextSize) -> usize {
         let mut res: usize = col.into();
         if let Some(wide_chars) = self.line_wide_chars.get(&line) {
-            for c in wide_chars {
+            for c in wide_chars.iter() {
                 if c.end <= col {
                     res -= usize::from(c.len()) - c.wide_len(enc);
                 } else {
@@ -183,7 +181,7 @@ impl LineIndex {
 
     fn wide_to_utf8_col(&self, enc: WideEncoding, line: u32, mut col: u32) -> TextSize {
         if let Some(wide_chars) = self.line_wide_chars.get(&line) {
-            for c in wide_chars {
+            for c in wide_chars.iter() {
                 if col > u32::from(c.start) {
                     col += u32::from(c.len()) - c.wide_len(enc) as u32;
                 } else {