aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/diagnostics/match_check/pat_util.rs
diff options
context:
space:
mode:
authorDawer <[email protected]>2021-05-11 13:18:16 +0100
committerDawer <[email protected]>2021-05-31 20:23:09 +0100
commite84efc4a4656e54a4f08b99592d5d98ac5726449 (patch)
tree7f21f05a7eb6cbef32ca9c081e19f9f0c2392567 /crates/hir_ty/src/diagnostics/match_check/pat_util.rs
parent894b4c64ffdb280a38c1ea2e9be145ca308965fd (diff)
Replace the old match checking algorithm
Diffstat (limited to 'crates/hir_ty/src/diagnostics/match_check/pat_util.rs')
-rw-r--r--crates/hir_ty/src/diagnostics/match_check/pat_util.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/crates/hir_ty/src/diagnostics/match_check/pat_util.rs b/crates/hir_ty/src/diagnostics/match_check/pat_util.rs
new file mode 100644
index 000000000..eb0b07a52
--- /dev/null
+++ b/crates/hir_ty/src/diagnostics/match_check/pat_util.rs
@@ -0,0 +1,52 @@
1use std::iter::{Enumerate, ExactSizeIterator};
2
3pub(crate) struct EnumerateAndAdjust<I> {
4 enumerate: Enumerate<I>,
5 gap_pos: usize,
6 gap_len: usize,
7}
8
9impl<I> Iterator for EnumerateAndAdjust<I>
10where
11 I: Iterator,
12{
13 type Item = (usize, <I as Iterator>::Item);
14
15 fn next(&mut self) -> Option<(usize, <I as Iterator>::Item)> {
16 self.enumerate
17 .next()
18 .map(|(i, elem)| (if i < self.gap_pos { i } else { i + self.gap_len }, elem))
19 }
20
21 fn size_hint(&self) -> (usize, Option<usize>) {
22 self.enumerate.size_hint()
23 }
24}
25
26pub(crate) trait EnumerateAndAdjustIterator {
27 fn enumerate_and_adjust(
28 self,
29 expected_len: usize,
30 gap_pos: Option<usize>,
31 ) -> EnumerateAndAdjust<Self>
32 where
33 Self: Sized;
34}
35
36impl<T: ExactSizeIterator> EnumerateAndAdjustIterator for T {
37 fn enumerate_and_adjust(
38 self,
39 expected_len: usize,
40 gap_pos: Option<usize>,
41 ) -> EnumerateAndAdjust<Self>
42 where
43 Self: Sized,
44 {
45 let actual_len = self.len();
46 EnumerateAndAdjust {
47 enumerate: self.enumerate(),
48 gap_pos: gap_pos.unwrap_or(expected_len),
49 gap_len: expected_len - actual_len,
50 }
51 }
52}