diff options
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.rs | 56 |
1 files changed, 56 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..b89b4f2bf --- /dev/null +++ b/crates/hir_ty/src/diagnostics/match_check/pat_util.rs | |||
@@ -0,0 +1,56 @@ | |||
1 | //! Pattern untilities. | ||
2 | //! | ||
3 | //! Originates from `rustc_hir::pat_util` | ||
4 | |||
5 | use std::iter::{Enumerate, ExactSizeIterator}; | ||
6 | |||
7 | pub(crate) struct EnumerateAndAdjust<I> { | ||
8 | enumerate: Enumerate<I>, | ||
9 | gap_pos: usize, | ||
10 | gap_len: usize, | ||
11 | } | ||
12 | |||
13 | impl<I> Iterator for EnumerateAndAdjust<I> | ||
14 | where | ||
15 | I: Iterator, | ||
16 | { | ||
17 | type Item = (usize, <I as Iterator>::Item); | ||
18 | |||
19 | fn next(&mut self) -> Option<(usize, <I as Iterator>::Item)> { | ||
20 | self.enumerate | ||
21 | .next() | ||
22 | .map(|(i, elem)| (if i < self.gap_pos { i } else { i + self.gap_len }, elem)) | ||
23 | } | ||
24 | |||
25 | fn size_hint(&self) -> (usize, Option<usize>) { | ||
26 | self.enumerate.size_hint() | ||
27 | } | ||
28 | } | ||
29 | |||
30 | pub(crate) trait EnumerateAndAdjustIterator { | ||
31 | fn enumerate_and_adjust( | ||
32 | self, | ||
33 | expected_len: usize, | ||
34 | gap_pos: Option<usize>, | ||
35 | ) -> EnumerateAndAdjust<Self> | ||
36 | where | ||
37 | Self: Sized; | ||
38 | } | ||
39 | |||
40 | impl<T: ExactSizeIterator> EnumerateAndAdjustIterator for T { | ||
41 | fn enumerate_and_adjust( | ||
42 | self, | ||
43 | expected_len: usize, | ||
44 | gap_pos: Option<usize>, | ||
45 | ) -> EnumerateAndAdjust<Self> | ||
46 | where | ||
47 | Self: Sized, | ||
48 | { | ||
49 | let actual_len = self.len(); | ||
50 | EnumerateAndAdjust { | ||
51 | enumerate: self.enumerate(), | ||
52 | gap_pos: gap_pos.unwrap_or(expected_len), | ||
53 | gap_len: expected_len - actual_len, | ||
54 | } | ||
55 | } | ||
56 | } | ||