Skip to content

Commit 37e1d1c

Browse files
bors[bot]matklad
andauthored
Merge #5503
5503: Replace superslice with API on path to stabilization r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 8a49f93 + 4f7a3fb commit 37e1d1c

File tree

5 files changed

+37
-13
lines changed

5 files changed

+37
-13
lines changed

Cargo.lock

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_ide_db/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ log = "0.4.8"
1616
rayon = "1.3.0"
1717
fst = { version = "0.4", default-features = false }
1818
rustc-hash = "1.1.0"
19-
superslice = "1.0.0"
2019
once_cell = "1.3.1"
2120
either = "1.5.3"
2221

22+
stdx = { path = "../stdx" }
23+
2324
ra_syntax = { path = "../ra_syntax" }
2425
ra_text_edit = { path = "../ra_text_edit" }
2526
ra_db = { path = "../ra_db" }

crates/ra_ide_db/src/line_index.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::iter;
44

55
use ra_syntax::{TextRange, TextSize};
66
use rustc_hash::FxHashMap;
7-
use superslice::Ext;
7+
use stdx::partition_point;
88

99
#[derive(Clone, Debug, PartialEq, Eq)]
1010
pub struct LineIndex {
@@ -89,7 +89,7 @@ impl LineIndex {
8989
}
9090

9191
pub fn line_col(&self, offset: TextSize) -> LineCol {
92-
let line = self.newlines.upper_bound(&offset) - 1;
92+
let line = partition_point(&self.newlines, |&it| it <= offset) - 1;
9393
let line_start_offset = self.newlines[line];
9494
let col = offset - line_start_offset;
9595

@@ -103,8 +103,8 @@ impl LineIndex {
103103
}
104104

105105
pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ {
106-
let lo = self.newlines.lower_bound(&range.start());
107-
let hi = self.newlines.upper_bound(&range.end());
106+
let lo = partition_point(&self.newlines, |&it| it < range.start());
107+
let hi = partition_point(&self.newlines, |&it| it <= range.end());
108108
let all = iter::once(range.start())
109109
.chain(self.newlines[lo..hi].iter().copied())
110110
.chain(iter::once(range.end()));

crates/stdx/src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,36 @@ impl<'a> Iterator for LinesWithEnds<'a> {
158158
}
159159
}
160160

161+
// https://github.com/rust-lang/rust/issues/73831
162+
pub fn partition_point<T, P>(slice: &[T], mut pred: P) -> usize
163+
where
164+
P: FnMut(&T) -> bool,
165+
{
166+
let mut left = 0;
167+
let mut right = slice.len();
168+
169+
while left != right {
170+
let mid = left + (right - left) / 2;
171+
// SAFETY:
172+
// When left < right, left <= mid < right.
173+
// Therefore left always increases and right always decreases,
174+
// and either of them is selected.
175+
// In both cases left <= right is satisfied.
176+
// Therefore if left < right in a step,
177+
// left <= right is satisfied in the next step.
178+
// Therefore as long as left != right, 0 <= left < right <= len is satisfied
179+
// and if this case 0 <= mid < len is satisfied too.
180+
let value = unsafe { slice.get_unchecked(mid) };
181+
if pred(value) {
182+
left = mid + 1;
183+
} else {
184+
right = mid;
185+
}
186+
}
187+
188+
left
189+
}
190+
161191
#[cfg(test)]
162192
mod tests {
163193
use super::*;

xtask/tests/tidy.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ fn rust_files_are_tidy() {
5353
fn check_licenses() {
5454
let expected = "
5555
0BSD OR MIT OR Apache-2.0
56-
Apache-2.0
5756
Apache-2.0 OR BSL-1.0
5857
Apache-2.0 OR MIT
5958
Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT

0 commit comments

Comments
 (0)