1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
use std::fmt::Debug;
use std::hash::Hash;
use std::ops;
use std::slice::SliceIndex;
/// Represents some newtyped `usize` wrapper.
///
/// Purpose: avoid mixing indexes for different bitvector domains.
pub trait Idx: Copy + 'static + Eq + PartialEq + Debug + Hash {
fn new(idx: usize) -> Self;
fn index(self) -> usize;
#[inline]
fn increment_by(&mut self, amount: usize) {
*self = self.plus(amount);
}
#[inline]
#[must_use = "Use `increment_by` if you wanted to update the index in-place"]
fn plus(self, amount: usize) -> Self {
Self::new(self.index() + amount)
}
}
impl Idx for usize {
#[inline]
fn new(idx: usize) -> Self {
idx
}
#[inline]
fn index(self) -> usize {
self
}
}
impl Idx for u32 {
#[inline]
fn new(idx: usize) -> Self {
assert!(idx <= u32::MAX as usize);
idx as u32
}
#[inline]
fn index(self) -> usize {
self as usize
}
}
/// Helper trait for indexing operations with a custom index type.
pub trait IntoSliceIdx<I, T: ?Sized> {
type Output: SliceIndex<T>;
fn into_slice_idx(self) -> Self::Output;
}
impl<I: Idx, T> IntoSliceIdx<I, [T]> for I {
type Output = usize;
#[inline]
fn into_slice_idx(self) -> Self::Output {
self.index()
}
}
impl<I, T> IntoSliceIdx<I, [T]> for ops::RangeFull {
type Output = ops::RangeFull;
#[inline]
fn into_slice_idx(self) -> Self::Output {
self
}
}
impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::Range<I> {
type Output = ops::Range<usize>;
#[inline]
fn into_slice_idx(self) -> Self::Output {
ops::Range { start: self.start.index(), end: self.end.index() }
}
}
impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::RangeFrom<I> {
type Output = ops::RangeFrom<usize>;
#[inline]
fn into_slice_idx(self) -> Self::Output {
ops::RangeFrom { start: self.start.index() }
}
}
impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::RangeTo<I> {
type Output = ops::RangeTo<usize>;
#[inline]
fn into_slice_idx(self) -> Self::Output {
..self.end.index()
}
}
impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::RangeInclusive<I> {
type Output = ops::RangeInclusive<usize>;
#[inline]
fn into_slice_idx(self) -> Self::Output {
ops::RangeInclusive::new(self.start().index(), self.end().index())
}
}
impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::RangeToInclusive<I> {
type Output = ops::RangeToInclusive<usize>;
#[inline]
fn into_slice_idx(self) -> Self::Output {
..=self.end.index()
}
}
#[cfg(feature = "nightly")]
impl<I: Idx, T> IntoSliceIdx<I, [T]> for core::range::Range<I> {
type Output = core::range::Range<usize>;
#[inline]
fn into_slice_idx(self) -> Self::Output {
core::range::Range { start: self.start.index(), end: self.end.index() }
}
}
#[cfg(feature = "nightly")]
impl<I: Idx, T> IntoSliceIdx<I, [T]> for core::range::RangeFrom<I> {
type Output = core::range::RangeFrom<usize>;
#[inline]
fn into_slice_idx(self) -> Self::Output {
core::range::RangeFrom { start: self.start.index() }
}
}
#[cfg(feature = "nightly")]
impl<I: Idx, T> IntoSliceIdx<I, [T]> for core::range::RangeInclusive<I> {
type Output = core::range::RangeInclusive<usize>;
#[inline]
#[cfg(bootstrap)]
fn into_slice_idx(self) -> Self::Output {
core::range::RangeInclusive { start: self.start.index(), end: self.end.index() }
}
#[inline]
#[cfg(not(bootstrap))]
fn into_slice_idx(self) -> Self::Output {
core::range::RangeInclusive { start: self.start.index(), last: self.last.index() }
}
}
#[cfg(all(feature = "nightly", not(bootstrap)))]
impl<I: Idx, T> IntoSliceIdx<I, [T]> for core::range::RangeToInclusive<I> {
type Output = core::range::RangeToInclusive<usize>;
#[inline]
fn into_slice_idx(self) -> Self::Output {
core::range::RangeToInclusive { last: self.last.index() }
}
}
|