|
| 1 | +//! Contains extensions related to arrays. |
| 2 | +
|
| 3 | +use std::convert::TryInto; |
| 4 | + |
| 5 | +/// Extension trait for arrays. |
| 6 | +pub trait ArrayExt { |
| 7 | + /// The item type the array is storing. |
| 8 | + type Item; |
| 9 | + |
| 10 | + /// Just like the slicing operation, this returns an array `LEN` items long at position |
| 11 | + /// `OFFSET`. |
| 12 | + /// |
| 13 | + /// The correctness of this operation is compile-time checked. |
| 14 | + /// |
| 15 | + /// Note that unlike slicing where the second number is the end index, here the second number |
| 16 | + /// is array length! |
| 17 | + fn sub_array<const OFFSET: usize, const LEN: usize>(&self) -> &[Self::Item; LEN]; |
| 18 | + |
| 19 | + /// Returns an item at given statically-known index. |
| 20 | + /// |
| 21 | + /// This is just like normal indexing except the check happens at compile time. |
| 22 | + fn get_static<const INDEX: usize>(&self) -> &Self::Item { &self.sub_array::<INDEX, 1>()[0] } |
| 23 | + |
| 24 | + /// Returns the first item in an array. |
| 25 | + /// |
| 26 | + /// Fails to compile if the array is empty. |
| 27 | + /// |
| 28 | + /// Note that this method's name intentionally shadows the `std`'s `first` method which |
| 29 | + /// returns `Option`. The rationale is that given the known length of the array, we always know |
| 30 | + /// that this will not return `None` so trying to keep the `std` method around is pointless. |
| 31 | + /// Importing the trait will also cause compile failures - that's also intentional to expose |
| 32 | + /// the places where useless checks are made. |
| 33 | + fn first(&self) -> &Self::Item { self.get_static::<0>() } |
| 34 | + |
| 35 | + /// Splits the array into two, non-overlapping smaller arrays covering the entire range. |
| 36 | + /// |
| 37 | + /// This is almost equivalent to just calling [`sub_array`](Self::sub_array) twice, except it also |
| 38 | + /// checks that the arrays don't overlap and that they cover the full range. This is very useful |
| 39 | + /// for demonstrating correctness, especially when chained. Using this technique even revealed |
| 40 | + /// a bug in the past. ([#4195](https://github.com/rust-bitcoin/rust-bitcoin/issues/4195)) |
| 41 | + fn split_array<const LEFT: usize, const RIGHT: usize>( |
| 42 | + &self, |
| 43 | + ) -> (&[Self::Item; LEFT], &[Self::Item; RIGHT]); |
| 44 | + |
| 45 | + /// Splits the array into the first element and the remaining, one element shorter, array. |
| 46 | + /// |
| 47 | + /// Fails to compile if the array is empty. |
| 48 | + /// |
| 49 | + /// Note that this method's name intentionally shadows the `std`'s `split_first` method which |
| 50 | + /// returns `Option`. The rationale is that given the known length of the array, we always know |
| 51 | + /// that this will not return `None` so trying to keep the `std` method around is pointless. |
| 52 | + /// Importing the trait will also cause compile failures - that's also intentional to expose |
| 53 | + /// the places where useless checks are made. |
| 54 | + fn split_first<const RIGHT: usize>(&self) -> (&Self::Item, &[Self::Item; RIGHT]) { |
| 55 | + let (first, remaining) = self.split_array::<1, RIGHT>(); |
| 56 | + (&first[0], remaining) |
| 57 | + } |
| 58 | + |
| 59 | + /// Splits the array into the last element and the remaining, one element shorter, array. |
| 60 | + /// |
| 61 | + /// Fails to compile if the array is empty. |
| 62 | + /// |
| 63 | + /// Note that this method's name intentionally shadows the `std`'s `split_last` method which |
| 64 | + /// returns `Option`. The rationale is that given the known length of the array, we always know |
| 65 | + /// that this will not return `None` so trying to keep the `std` method around is pointless. |
| 66 | + /// Importing the trait will also cause compile failures - that's also intentional to expose |
| 67 | + /// the places where useless checks are made. |
| 68 | + /// |
| 69 | + /// The returned tuple is also reversed just as `std` for consistency and simpler diffs when |
| 70 | + /// migrating. |
| 71 | + fn split_last<const LEFT: usize>(&self) -> (&Self::Item, &[Self::Item; LEFT]) { |
| 72 | + let (remaining, last) = self.split_array::<LEFT, 1>(); |
| 73 | + (&last[0], remaining) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl<T, const N: usize> ArrayExt for [T; N] { |
| 78 | + type Item = T; |
| 79 | + |
| 80 | + fn sub_array<const OFFSET: usize, const LEN: usize>(&self) -> &[Self::Item; LEN] { |
| 81 | + #[allow(clippy::let_unit_value)] |
| 82 | + let () = Hack::<N, OFFSET, LEN>::IS_VALID_RANGE; |
| 83 | + |
| 84 | + self[OFFSET..(OFFSET + LEN)].try_into().expect("this is also compiler-checked above") |
| 85 | + } |
| 86 | + |
| 87 | + fn split_array<const LEFT: usize, const RIGHT: usize>( |
| 88 | + &self, |
| 89 | + ) -> (&[Self::Item; LEFT], &[Self::Item; RIGHT]) { |
| 90 | + #[allow(clippy::let_unit_value)] |
| 91 | + let () = Hack2::<N, LEFT, RIGHT>::IS_FULL_RANGE; |
| 92 | + |
| 93 | + (self.sub_array::<0, LEFT>(), self.sub_array::<LEFT, RIGHT>()) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +struct Hack<const N: usize, const OFFSET: usize, const LEN: usize>; |
| 98 | + |
| 99 | +impl<const N: usize, const OFFSET: usize, const LEN: usize> Hack<N, OFFSET, LEN> { |
| 100 | + const IS_VALID_RANGE: () = [()][(OFFSET + LEN > N) as usize]; |
| 101 | +} |
| 102 | + |
| 103 | +struct Hack2<const N: usize, const LEFT: usize, const RIGHT: usize>; |
| 104 | + |
| 105 | +impl<const N: usize, const LEFT: usize, const RIGHT: usize> Hack2<N, LEFT, RIGHT> { |
| 106 | + const IS_FULL_RANGE: () = [()][(LEFT + RIGHT != N) as usize]; |
| 107 | +} |
0 commit comments