|
1 | 1 | use std::fmt::Debug;
|
2 | 2 | use std::hash::Hash;
|
| 3 | +use std::ops; |
| 4 | +use std::slice::SliceIndex; |
3 | 5 |
|
4 | 6 | /// Represents some newtyped `usize` wrapper.
|
5 | 7 | ///
|
@@ -43,3 +45,92 @@ impl Idx for u32 {
|
43 | 45 | self as usize
|
44 | 46 | }
|
45 | 47 | }
|
| 48 | + |
| 49 | +/// Helper trait for indexing operations with a custom index type. |
| 50 | +pub trait IntoSliceIdx<I, T: ?Sized> { |
| 51 | + type Output: SliceIndex<T>; |
| 52 | + fn into_slice_idx(self) -> Self::Output; |
| 53 | +} |
| 54 | + |
| 55 | +impl<I: Idx, T> IntoSliceIdx<I, [T]> for I { |
| 56 | + type Output = usize; |
| 57 | + #[inline] |
| 58 | + fn into_slice_idx(self) -> Self::Output { |
| 59 | + self.index() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl<I, T> IntoSliceIdx<I, [T]> for ops::RangeFull { |
| 64 | + type Output = ops::RangeFull; |
| 65 | + #[inline] |
| 66 | + fn into_slice_idx(self) -> Self::Output { |
| 67 | + self |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::Range<I> { |
| 72 | + type Output = ops::Range<usize>; |
| 73 | + #[inline] |
| 74 | + fn into_slice_idx(self) -> Self::Output { |
| 75 | + ops::Range { start: self.start.index(), end: self.end.index() } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::RangeFrom<I> { |
| 80 | + type Output = ops::RangeFrom<usize>; |
| 81 | + #[inline] |
| 82 | + fn into_slice_idx(self) -> Self::Output { |
| 83 | + ops::RangeFrom { start: self.start.index() } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::RangeTo<I> { |
| 88 | + type Output = ops::RangeTo<usize>; |
| 89 | + #[inline] |
| 90 | + fn into_slice_idx(self) -> Self::Output { |
| 91 | + ..self.end.index() |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::RangeInclusive<I> { |
| 96 | + type Output = ops::RangeInclusive<usize>; |
| 97 | + #[inline] |
| 98 | + fn into_slice_idx(self) -> Self::Output { |
| 99 | + ops::RangeInclusive::new(self.start().index(), self.end().index()) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::RangeToInclusive<I> { |
| 104 | + type Output = ops::RangeToInclusive<usize>; |
| 105 | + #[inline] |
| 106 | + fn into_slice_idx(self) -> Self::Output { |
| 107 | + ..=self.end.index() |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[cfg(feature = "nightly")] |
| 112 | +impl<I: Idx, T> IntoSliceIdx<I, [T]> for core::range::Range<I> { |
| 113 | + type Output = core::range::Range<usize>; |
| 114 | + #[inline] |
| 115 | + fn into_slice_idx(self) -> Self::Output { |
| 116 | + core::range::Range { start: self.start.index(), end: self.end.index() } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +#[cfg(feature = "nightly")] |
| 121 | +impl<I: Idx, T> IntoSliceIdx<I, [T]> for core::range::RangeFrom<I> { |
| 122 | + type Output = core::range::RangeFrom<usize>; |
| 123 | + #[inline] |
| 124 | + fn into_slice_idx(self) -> Self::Output { |
| 125 | + core::range::RangeFrom { start: self.start.index() } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +#[cfg(feature = "nightly")] |
| 130 | +impl<I: Idx, T> IntoSliceIdx<I, [T]> for core::range::RangeInclusive<I> { |
| 131 | + type Output = core::range::RangeInclusive<usize>; |
| 132 | + #[inline] |
| 133 | + fn into_slice_idx(self) -> Self::Output { |
| 134 | + core::range::RangeInclusive { start: self.start.index(), end: self.end.index() } |
| 135 | + } |
| 136 | +} |
0 commit comments