From 261cd1a1c4a5da445157193e2da6160cd3fb948b Mon Sep 17 00:00:00 2001 From: Waleed Khan Date: Sun, 20 Feb 2022 21:14:48 -0800 Subject: [PATCH] build: add shims for nightly feature `map_first_last` --- lib/src/diff.rs | 6 ++-- lib/src/index.rs | 13 +++---- lib/src/lib.rs | 2 +- lib/src/nightly_shims.rs | 61 ++++++++++++++++++++++++++++++++ lib/src/revset_graph_iterator.rs | 3 +- 5 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 lib/src/nightly_shims.rs diff --git a/lib/src/diff.rs b/lib/src/diff.rs index c4d59c4c3..b11b1eb98 100644 --- a/lib/src/diff.rs +++ b/lib/src/diff.rs @@ -19,6 +19,8 @@ use std::ops::Range; use itertools::Itertools; +use crate::nightly_shims::BTreeMapExt; + pub fn find_line_ranges(text: &[u8]) -> Vec> { let mut ranges = vec![]; let mut start = 0; @@ -175,7 +177,7 @@ pub(crate) fn unchanged_ranges( let max_occurrences = 100; let mut left_histogram = Histogram::calculate(left, left_ranges, max_occurrences); - if *left_histogram.count_to_words.first_entry().unwrap().key() > max_occurrences { + if *left_histogram.count_to_words.ext_first_key().unwrap() > max_occurrences { // If there are very many occurrences of all words, then we just give up. return vec![]; } @@ -185,7 +187,7 @@ pub(crate) fn unchanged_ranges( // the LCS. let mut uncommon_shared_words = vec![]; while !left_histogram.count_to_words.is_empty() && uncommon_shared_words.is_empty() { - let left_words = left_histogram.count_to_words.pop_first().unwrap().1; + let left_words = left_histogram.count_to_words.ext_pop_first_value().unwrap(); for left_word in left_words { if right_histogram.word_to_positions.contains_key(left_word) { uncommon_shared_words.push(left_word); diff --git a/lib/src/index.rs b/lib/src/index.rs index 6e322843a..87d6282bb 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -34,6 +34,7 @@ use thiserror::Error; use crate::backend::{ChangeId, CommitId}; use crate::commit::Commit; use crate::file_util::persist_content_addressed_temp_file; +use crate::nightly_shims::BTreeSetExt; #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)] pub struct IndexPosition(u32); @@ -812,25 +813,25 @@ impl<'a> CompositeIndex<'a> { let mut result = BTreeSet::new(); while !(items1.is_empty() || items2.is_empty()) { - let entry1 = items1.last().unwrap(); - let entry2 = items2.last().unwrap(); + let entry1 = items1.ext_last().unwrap(); + let entry2 = items2.ext_last().unwrap(); match entry1.cmp(entry2) { Ordering::Greater => { - let entry1 = items1.pop_last().unwrap(); + let entry1 = items1.ext_pop_last().unwrap(); for parent_entry in entry1.0.parents() { items1.insert(IndexEntryByGeneration(parent_entry)); } } Ordering::Less => { - let entry2 = items2.pop_last().unwrap(); + let entry2 = items2.ext_pop_last().unwrap(); for parent_entry in entry2.0.parents() { items2.insert(IndexEntryByGeneration(parent_entry)); } } Ordering::Equal => { result.insert(entry1.0.pos); - items1.pop_last(); - items2.pop_last(); + items1.ext_pop_last(); + items2.ext_pop_last(); } } } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 950798692..6b1502dda 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -13,7 +13,6 @@ // limitations under the License. #![feature(assert_matches)] -#![feature(map_first_last)] #![deny(unused_must_use)] #[macro_use] @@ -39,6 +38,7 @@ pub mod index_store; pub mod local_backend; pub mod lock; pub mod matchers; +pub mod nightly_shims; pub mod op_heads_store; pub mod op_store; pub mod operation; diff --git a/lib/src/nightly_shims.rs b/lib/src/nightly_shims.rs new file mode 100644 index 000000000..c9d7301ef --- /dev/null +++ b/lib/src/nightly_shims.rs @@ -0,0 +1,61 @@ +use std::collections::{BTreeMap, BTreeSet}; + +pub trait BTreeMapExt { + fn ext_first_key(&self) -> Option<&K>; + fn ext_last_key(&self) -> Option<&K>; + fn ext_pop_first_key(&mut self) -> Option; + fn ext_pop_last_key(&mut self) -> Option; + fn ext_pop_first_value(&mut self) -> Option; + fn ext_pop_last_value(&mut self) -> Option; +} + +impl BTreeMapExt for BTreeMap { + fn ext_first_key(&self) -> Option<&K> { + self.keys().next() + } + + fn ext_last_key(&self) -> Option<&K> { + self.keys().next_back() + } + + fn ext_pop_first_key(&mut self) -> Option { + let key = self.ext_first_key()?; + let key = key.clone(); // ownership hack + Some(self.remove_entry(&key).unwrap().0) + } + + fn ext_pop_last_key(&mut self) -> Option { + let key = self.ext_last_key()?; + let key = key.clone(); // ownership hack + Some(self.remove_entry(&key).unwrap().0) + } + + fn ext_pop_first_value(&mut self) -> Option { + let key = self.ext_first_key()?; + let key = key.clone(); // ownership hack + Some(self.remove_entry(&key).unwrap().1) + } + + fn ext_pop_last_value(&mut self) -> Option { + let key = self.ext_last_key()?; + let key = key.clone(); // ownership hack + Some(self.remove_entry(&key).unwrap().1) + } +} + +pub trait BTreeSetExt { + fn ext_last(&self) -> Option<&K>; + fn ext_pop_last(&mut self) -> Option; +} + +impl BTreeSetExt for BTreeSet { + fn ext_last(&self) -> Option<&K> { + self.iter().next_back() + } + + fn ext_pop_last(&mut self) -> Option { + let key = self.ext_last()?; + let key = key.clone(); // ownership hack + self.take(&key) + } +} diff --git a/lib/src/revset_graph_iterator.rs b/lib/src/revset_graph_iterator.rs index 7ea4c9a69..c1f6920a1 100644 --- a/lib/src/revset_graph_iterator.rs +++ b/lib/src/revset_graph_iterator.rs @@ -16,6 +16,7 @@ use std::cmp::min; use std::collections::{BTreeMap, HashSet}; use crate::index::{IndexEntry, IndexPosition}; +use crate::nightly_shims::BTreeMapExt; use crate::revset::RevsetIterator; #[derive(Debug, PartialEq, Eq, Clone, Hash)] @@ -149,7 +150,7 @@ impl<'revset, 'repo> RevsetGraphIterator<'revset, 'repo> { } fn next_index_entry(&mut self) -> Option> { - if let Some((_, index_entry)) = self.look_ahead.pop_last() { + if let Some(index_entry) = self.look_ahead.ext_pop_last_value() { return Some(index_entry); } self.input_set_iter.next()