internal: use API stabilized in 1.52

This commit is contained in:
Aleksey Kladov 2021-05-04 14:10:49 +03:00 committed by Kirill Bulatov
parent 3b4d5df840
commit 6a16ec52aa
7 changed files with 16 additions and 90 deletions

View file

@ -381,7 +381,7 @@ impl<'a> CompletionContext<'a> {
let def = self.sema.to_def(&it); let def = self.sema.to_def(&it);
(def.map(|def| def.ret_type(self.db)), None) (def.map(|def| def.ret_type(self.db)), None)
}, },
ast::Stmt(it) => (None, None), ast::Stmt(_) => (None, None),
_ => { _ => {
match node.parent() { match node.parent() {
Some(n) => { Some(n) => {

View file

@ -3,7 +3,6 @@
use std::iter; use std::iter;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use stdx::partition_point;
use syntax::{TextRange, TextSize}; use syntax::{TextRange, TextSize};
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
@ -97,7 +96,7 @@ impl LineIndex {
} }
pub fn line_col(&self, offset: TextSize) -> LineCol { pub fn line_col(&self, offset: TextSize) -> LineCol {
let line = partition_point(&self.newlines, |&it| it <= offset) - 1; let line = self.newlines.partition_point(|&it| it <= offset) - 1;
let line_start_offset = self.newlines[line]; let line_start_offset = self.newlines[line];
let col = offset - line_start_offset; let col = offset - line_start_offset;
LineCol { line: line as u32, col: col.into() } LineCol { line: line as u32, col: col.into() }
@ -118,8 +117,8 @@ impl LineIndex {
} }
pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ { pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ {
let lo = partition_point(&self.newlines, |&it| it < range.start()); let lo = self.newlines.partition_point(|&it| it < range.start());
let hi = partition_point(&self.newlines, |&it| it <= range.end()); let hi = self.newlines.partition_point(|&it| it <= range.end());
let all = iter::once(range.start()) let all = iter::once(range.start())
.chain(self.newlines[lo..hi].iter().copied()) .chain(self.newlines[lo..hi].iter().copied())
.chain(iter::once(range.end())); .chain(iter::once(range.end()));

View file

@ -4,7 +4,6 @@
use std::str::FromStr; use std::str::FromStr;
use cfg::CfgOptions; use cfg::CfgOptions;
use stdx::split_once;
#[derive(Clone, Eq, PartialEq, Debug)] #[derive(Clone, Eq, PartialEq, Debug)]
pub enum CfgFlag { pub enum CfgFlag {
@ -15,7 +14,7 @@ pub enum CfgFlag {
impl FromStr for CfgFlag { impl FromStr for CfgFlag {
type Err = String; type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let res = match split_once(s, '=') { let res = match s.split_once('=') {
Some((key, value)) => { Some((key, value)) => {
if !(value.starts_with('"') && value.ends_with('"')) { if !(value.starts_with('"') && value.ends_with('"')) {
return Err(format!("Invalid cfg ({:?}), value should be in quotes", s)); return Err(format!("Invalid cfg ({:?}), value should be in quotes", s));

View file

@ -65,20 +65,6 @@ pub fn replace(buf: &mut String, from: char, to: &str) {
*buf = buf.replace(from, to) *buf = buf.replace(from, to)
} }
// https://github.com/rust-lang/rust/issues/74773
pub fn split_once(haystack: &str, delim: char) -> Option<(&str, &str)> {
let mut split = haystack.splitn(2, delim);
let prefix = split.next()?;
let suffix = split.next()?;
Some((prefix, suffix))
}
pub fn rsplit_once(haystack: &str, delim: char) -> Option<(&str, &str)> {
let mut split = haystack.rsplitn(2, delim);
let suffix = split.next()?;
let prefix = split.next()?;
Some((prefix, suffix))
}
pub fn trim_indent(mut text: &str) -> String { pub fn trim_indent(mut text: &str) -> String {
if text.starts_with('\n') { if text.starts_with('\n') {
text = &text[1..]; text = &text[1..];
@ -89,7 +75,7 @@ pub fn trim_indent(mut text: &str) -> String {
.map(|it| it.len() - it.trim_start().len()) .map(|it| it.len() - it.trim_start().len())
.min() .min()
.unwrap_or(0); .unwrap_or(0);
lines_with_ends(text) text.split_inclusive('\n')
.map( .map(
|line| { |line| {
if line.len() <= indent { if line.len() <= indent {
@ -102,70 +88,12 @@ pub fn trim_indent(mut text: &str) -> String {
.collect() .collect()
} }
pub fn lines_with_ends(text: &str) -> LinesWithEnds {
LinesWithEnds { text }
}
pub struct LinesWithEnds<'a> {
text: &'a str,
}
impl<'a> Iterator for LinesWithEnds<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<&'a str> {
if self.text.is_empty() {
return None;
}
let idx = self.text.find('\n').map_or(self.text.len(), |it| it + 1);
let (res, next) = self.text.split_at(idx);
self.text = next;
Some(res)
}
}
/// Returns `idx` such that:
///
/// ```text
/// ∀ x in slice[..idx]: pred(x)
/// && ∀ x in slice[idx..]: !pred(x)
/// ```
///
/// https://github.com/rust-lang/rust/issues/73831
pub fn partition_point<T, P>(slice: &[T], mut pred: P) -> usize
where
P: FnMut(&T) -> bool,
{
let mut left = 0;
let mut right = slice.len();
while left != right {
let mid = left + (right - left) / 2;
// SAFETY:
// When left < right, left <= mid < right.
// Therefore left always increases and right always decreases,
// and either of them is selected.
// In both cases left <= right is satisfied.
// Therefore if left < right in a step,
// left <= right is satisfied in the next step.
// Therefore as long as left != right, 0 <= left < right <= len is satisfied
// and if this case 0 <= mid < len is satisfied too.
let value = unsafe { slice.get_unchecked(mid) };
if pred(value) {
left = mid + 1;
} else {
right = mid;
}
}
left
}
pub fn equal_range_by<T, F>(slice: &[T], mut key: F) -> ops::Range<usize> pub fn equal_range_by<T, F>(slice: &[T], mut key: F) -> ops::Range<usize>
where where
F: FnMut(&T) -> Ordering, F: FnMut(&T) -> Ordering,
{ {
let start = partition_point(slice, |it| key(it) == Ordering::Less); let start = slice.partition_point(|it| key(it) == Ordering::Less);
let len = partition_point(&slice[start..], |it| key(it) == Ordering::Equal); let len = slice[start..].partition_point(|it| key(it) == Ordering::Equal);
start..start + len start..start + len
} }

View file

@ -62,7 +62,7 @@
//! ``` //! ```
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use stdx::{lines_with_ends, split_once, trim_indent}; use stdx::trim_indent;
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub struct Fixture { pub struct Fixture {
@ -93,7 +93,7 @@ impl Fixture {
let default = if ra_fixture.contains("//-") { None } else { Some("//- /main.rs") }; let default = if ra_fixture.contains("//-") { None } else { Some("//- /main.rs") };
for (ix, line) in default.into_iter().chain(lines_with_ends(&fixture)).enumerate() { for (ix, line) in default.into_iter().chain(fixture.split_inclusive('\n')).enumerate() {
if line.contains("//-") { if line.contains("//-") {
assert!( assert!(
line.starts_with("//-"), line.starts_with("//-"),
@ -133,14 +133,14 @@ impl Fixture {
let mut env = FxHashMap::default(); let mut env = FxHashMap::default();
let mut introduce_new_source_root = false; let mut introduce_new_source_root = false;
for component in components[1..].iter() { for component in components[1..].iter() {
let (key, value) = split_once(component, ':').unwrap(); let (key, value) = component.split_once(':').unwrap();
match key { match key {
"crate" => krate = Some(value.to_string()), "crate" => krate = Some(value.to_string()),
"deps" => deps = value.split(',').map(|it| it.to_string()).collect(), "deps" => deps = value.split(',').map(|it| it.to_string()).collect(),
"edition" => edition = Some(value.to_string()), "edition" => edition = Some(value.to_string()),
"cfg" => { "cfg" => {
for entry in value.split(',') { for entry in value.split(',') {
match split_once(entry, '=') { match entry.split_once('=') {
Some((k, v)) => cfg_key_values.push((k.to_string(), v.to_string())), Some((k, v)) => cfg_key_values.push((k.to_string(), v.to_string())),
None => cfg_atoms.push(entry.to_string()), None => cfg_atoms.push(entry.to_string()),
} }
@ -148,7 +148,7 @@ impl Fixture {
} }
"env" => { "env" => {
for key in value.split(',') { for key in value.split(',') {
if let Some((k, v)) = split_once(key, '=') { if let Some((k, v)) = key.split_once('=') {
env.insert(k.into(), v.into()); env.insert(k.into(), v.into());
} }
} }

View file

@ -17,7 +17,7 @@ use std::{
}; };
use profile::StopWatch; use profile::StopWatch;
use stdx::{is_ci, lines_with_ends}; use stdx::is_ci;
use text_size::{TextRange, TextSize}; use text_size::{TextRange, TextSize};
pub use dissimilar::diff as __diff; pub use dissimilar::diff as __diff;
@ -181,7 +181,7 @@ pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> {
let mut prev_line_start: Option<TextSize> = None; let mut prev_line_start: Option<TextSize> = None;
let mut line_start: TextSize = 0.into(); let mut line_start: TextSize = 0.into();
let mut prev_line_annotations: Vec<(TextSize, usize)> = Vec::new(); let mut prev_line_annotations: Vec<(TextSize, usize)> = Vec::new();
for line in lines_with_ends(text) { for line in text.split_inclusive('\n') {
let mut this_line_annotations = Vec::new(); let mut this_line_annotations = Vec::new();
if let Some(idx) = line.find("//") { if let Some(idx) = line.find("//") {
let annotation_offset = TextSize::of(&line[..idx + "//".len()]); let annotation_offset = TextSize::of(&line[..idx + "//".len()]);

View file

@ -8,7 +8,7 @@ use xshell::{cmd, pushd};
use crate::flags; use crate::flags;
// Latest stable, feel free to send a PR if this lags behind. // Latest stable, feel free to send a PR if this lags behind.
const REQUIRED_RUST_VERSION: u32 = 51; const REQUIRED_RUST_VERSION: u32 = 52;
impl flags::Install { impl flags::Install {
pub(crate) fn run(self) -> Result<()> { pub(crate) fn run(self) -> Result<()> {