From 63a462f37ca584e1a585a69e30823ce25d4d252f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 25 Apr 2020 00:57:47 +0200 Subject: Switch to TryFrom --- crates/ra_ide_db/src/line_index.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'crates/ra_ide_db/src/line_index.rs') diff --git a/crates/ra_ide_db/src/line_index.rs b/crates/ra_ide_db/src/line_index.rs index 81eebc711..00ba95913 100644 --- a/crates/ra_ide_db/src/line_index.rs +++ b/crates/ra_ide_db/src/line_index.rs @@ -1,8 +1,9 @@ //! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)` //! representation. +use std::iter; + use ra_syntax::{TextRange, TextSize}; use rustc_hash::FxHashMap; -use std::iter; use superslice::Ext; #[derive(Clone, Debug, PartialEq, Eq)] @@ -116,12 +117,11 @@ impl LineIndex { res } - fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextSize { - let mut col: TextSize = col.into(); + fn utf16_to_utf8_col(&self, line: u32, mut col: u32) -> TextSize { if let Some(utf16_chars) = self.utf16_lines.get(&line) { for c in utf16_chars { - if col >= c.start { - col += c.len() - TextSize::from_usize(1); + if col >= u32::from(c.start) { + col += u32::from(c.len()) - 1; } else { // From here on, all utf16 characters come *after* the character we are mapping, // so we don't need to take them into account @@ -130,12 +130,12 @@ impl LineIndex { } } - col + col.into() } } #[cfg(test)] -mod test_line_index { +mod tests { use super::*; #[test] @@ -224,12 +224,12 @@ const C: char = \"メ メ\"; assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15); // UTF-16 to UTF-8 - assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from_usize(15)); + assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from(15)); - assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextSize::from_usize(20)); - assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from_usize(23)); + assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextSize::from(20)); + assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(23)); - assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextSize::from_usize(15)); + assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextSize::from(15)); } #[test] -- cgit v1.2.3