Skip to content

Commit 99ebfe2

Browse files
committed
move index code around
1 parent e496fbe commit 99ebfe2

File tree

2 files changed

+100
-99
lines changed

2 files changed

+100
-99
lines changed

compiler/rustc_index/src/slice.rs

+35-34
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
slice,
66
};
77

8-
use crate::{idx::Idx, vec::IndexVec};
8+
use crate::{Idx, IndexVec};
99

1010
/// A view into contiguous `T`s, indexed by `I` rather than by `usize`.
1111
///
@@ -140,6 +140,17 @@ impl<I: Idx, T> IndexSlice<I, T> {
140140
let ptr = self.raw.as_mut_ptr();
141141
unsafe { (&mut *ptr.add(ai), &mut *ptr.add(bi), &mut *ptr.add(ci)) }
142142
}
143+
144+
#[inline]
145+
pub fn binary_search(&self, value: &T) -> Result<I, I>
146+
where
147+
T: Ord,
148+
{
149+
match self.raw.binary_search(value) {
150+
Ok(i) => Ok(Idx::new(i)),
151+
Err(i) => Err(Idx::new(i)),
152+
}
153+
}
143154
}
144155

145156
impl<I: Idx, J: Idx> IndexSlice<I, J> {
@@ -172,20 +183,6 @@ impl<I: Idx, J: Idx> IndexSlice<I, J> {
172183
}
173184
}
174185

175-
impl<I: Idx, T: Ord> IndexSlice<I, T> {
176-
#[inline]
177-
pub fn binary_search(&self, value: &T) -> Result<I, I> {
178-
match self.raw.binary_search(value) {
179-
Ok(i) => Ok(Idx::new(i)),
180-
Err(i) => Err(Idx::new(i)),
181-
}
182-
}
183-
}
184-
185-
// Whether `IndexSlice` is `Send` depends only on the data,
186-
// not the phantom data.
187-
unsafe impl<I: Idx, T> Send for IndexSlice<I, T> where T: Send {}
188-
189186
impl<I: Idx, T: fmt::Debug> fmt::Debug for IndexSlice<I, T> {
190187
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
191188
fmt::Debug::fmt(&self.raw, fmt)
@@ -208,6 +205,26 @@ impl<I: Idx, T> IndexMut<I> for IndexSlice<I, T> {
208205
}
209206
}
210207

208+
impl<'a, I: Idx, T> IntoIterator for &'a IndexSlice<I, T> {
209+
type Item = &'a T;
210+
type IntoIter = slice::Iter<'a, T>;
211+
212+
#[inline]
213+
fn into_iter(self) -> slice::Iter<'a, T> {
214+
self.raw.iter()
215+
}
216+
}
217+
218+
impl<'a, I: Idx, T> IntoIterator for &'a mut IndexSlice<I, T> {
219+
type Item = &'a mut T;
220+
type IntoIter = slice::IterMut<'a, T>;
221+
222+
#[inline]
223+
fn into_iter(self) -> slice::IterMut<'a, T> {
224+
self.raw.iter_mut()
225+
}
226+
}
227+
211228
impl<I: Idx, T: Clone> ToOwned for IndexSlice<I, T> {
212229
type Owned = IndexVec<I, T>;
213230

@@ -234,22 +251,6 @@ impl<I: Idx, T> Default for &mut IndexSlice<I, T> {
234251
}
235252
}
236253

237-
impl<'a, I: Idx, T> IntoIterator for &'a IndexSlice<I, T> {
238-
type Item = &'a T;
239-
type IntoIter = slice::Iter<'a, T>;
240-
241-
#[inline]
242-
fn into_iter(self) -> slice::Iter<'a, T> {
243-
self.raw.iter()
244-
}
245-
}
246-
247-
impl<'a, I: Idx, T> IntoIterator for &'a mut IndexSlice<I, T> {
248-
type Item = &'a mut T;
249-
type IntoIter = slice::IterMut<'a, T>;
250-
251-
#[inline]
252-
fn into_iter(self) -> slice::IterMut<'a, T> {
253-
self.raw.iter_mut()
254-
}
255-
}
254+
// Whether `IndexSlice` is `Send` depends only on the data,
255+
// not the phantom data.
256+
unsafe impl<I: Idx, T> Send for IndexSlice<I, T> where T: Send {}

compiler/rustc_index/src/vec.rs

+65-65
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use std::ops::{Deref, DerefMut, RangeBounds};
99
use std::slice;
1010
use std::vec;
1111

12-
use crate::idx::Idx;
13-
use crate::slice::IndexSlice;
12+
use crate::{Idx, IndexSlice};
1413

1514
/// An owned contiguous collection of `T`s, indexed by `I` rather than by `usize`.
1615
///
@@ -25,30 +24,6 @@ pub struct IndexVec<I: Idx, T> {
2524
_marker: PhantomData<fn(&I)>,
2625
}
2726

28-
// Whether `IndexVec` is `Send` depends only on the data,
29-
// not the phantom data.
30-
unsafe impl<I: Idx, T> Send for IndexVec<I, T> where T: Send {}
31-
32-
#[cfg(feature = "rustc_serialize")]
33-
impl<S: Encoder, I: Idx, T: Encodable<S>> Encodable<S> for IndexVec<I, T> {
34-
fn encode(&self, s: &mut S) {
35-
Encodable::encode(&self.raw, s);
36-
}
37-
}
38-
39-
#[cfg(feature = "rustc_serialize")]
40-
impl<D: Decoder, I: Idx, T: Decodable<D>> Decodable<D> for IndexVec<I, T> {
41-
fn decode(d: &mut D) -> Self {
42-
IndexVec { raw: Decodable::decode(d), _marker: PhantomData }
43-
}
44-
}
45-
46-
impl<I: Idx, T: fmt::Debug> fmt::Debug for IndexVec<I, T> {
47-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
48-
fmt::Debug::fmt(&self.raw, fmt)
49-
}
50-
}
51-
5227
impl<I: Idx, T> IndexVec<I, T> {
5328
#[inline]
5429
pub fn new() -> Self {
@@ -183,13 +158,45 @@ impl<I: Idx, T> IndexVec<I, T> {
183158
&mut self[elem]
184159
}
185160

161+
#[inline]
162+
pub fn resize(&mut self, new_len: usize, value: T)
163+
where
164+
T: Clone,
165+
{
166+
self.raw.resize(new_len, value)
167+
}
168+
186169
#[inline]
187170
pub fn resize_to_elem(&mut self, elem: I, fill_value: impl FnMut() -> T) {
188171
let min_new_len = elem.index() + 1;
189172
self.raw.resize_with(min_new_len, fill_value);
190173
}
191174
}
192175

176+
/// `IndexVec` is often used as a map, so it provides some map-like APIs.
177+
impl<I: Idx, T> IndexVec<I, Option<T>> {
178+
#[inline]
179+
pub fn insert(&mut self, index: I, value: T) -> Option<T> {
180+
self.ensure_contains_elem(index, || None).replace(value)
181+
}
182+
183+
#[inline]
184+
pub fn get_or_insert_with(&mut self, index: I, value: impl FnOnce() -> T) -> &mut T {
185+
self.ensure_contains_elem(index, || None).get_or_insert_with(value)
186+
}
187+
188+
#[inline]
189+
pub fn remove(&mut self, index: I) -> Option<T> {
190+
self.get_mut(index)?.take()
191+
}
192+
}
193+
194+
impl<I: Idx, T: fmt::Debug> fmt::Debug for IndexVec<I, T> {
195+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
196+
fmt::Debug::fmt(&self.raw, fmt)
197+
}
198+
}
199+
193200
impl<I: Idx, T> Deref for IndexVec<I, T> {
194201
type Target = IndexSlice<I, T>;
195202

@@ -218,38 +225,6 @@ impl<I: Idx, T> BorrowMut<IndexSlice<I, T>> for IndexVec<I, T> {
218225
}
219226
}
220227

221-
/// `IndexVec` is often used as a map, so it provides some map-like APIs.
222-
impl<I: Idx, T> IndexVec<I, Option<T>> {
223-
#[inline]
224-
pub fn insert(&mut self, index: I, value: T) -> Option<T> {
225-
self.ensure_contains_elem(index, || None).replace(value)
226-
}
227-
228-
#[inline]
229-
pub fn get_or_insert_with(&mut self, index: I, value: impl FnOnce() -> T) -> &mut T {
230-
self.ensure_contains_elem(index, || None).get_or_insert_with(value)
231-
}
232-
233-
#[inline]
234-
pub fn remove(&mut self, index: I) -> Option<T> {
235-
self.get_mut(index)?.take()
236-
}
237-
}
238-
239-
impl<I: Idx, T: Clone> IndexVec<I, T> {
240-
#[inline]
241-
pub fn resize(&mut self, new_len: usize, value: T) {
242-
self.raw.resize(new_len, value)
243-
}
244-
}
245-
246-
impl<I: Idx, T> Default for IndexVec<I, T> {
247-
#[inline]
248-
fn default() -> Self {
249-
Self::new()
250-
}
251-
}
252-
253228
impl<I: Idx, T> Extend<T> for IndexVec<I, T> {
254229
#[inline]
255230
fn extend<J: IntoIterator<Item = T>>(&mut self, iter: J) {
@@ -279,13 +254,6 @@ impl<I: Idx, T> FromIterator<T> for IndexVec<I, T> {
279254
}
280255
}
281256

282-
impl<I: Idx, T, const N: usize> From<[T; N]> for IndexVec<I, T> {
283-
#[inline]
284-
fn from(array: [T; N]) -> Self {
285-
IndexVec::from_raw(array.into())
286-
}
287-
}
288-
289257
impl<I: Idx, T> IntoIterator for IndexVec<I, T> {
290258
type Item = T;
291259
type IntoIter = vec::IntoIter<T>;
@@ -316,5 +284,37 @@ impl<'a, I: Idx, T> IntoIterator for &'a mut IndexVec<I, T> {
316284
}
317285
}
318286

287+
impl<I: Idx, T> Default for IndexVec<I, T> {
288+
#[inline]
289+
fn default() -> Self {
290+
Self::new()
291+
}
292+
}
293+
294+
impl<I: Idx, T, const N: usize> From<[T; N]> for IndexVec<I, T> {
295+
#[inline]
296+
fn from(array: [T; N]) -> Self {
297+
IndexVec::from_raw(array.into())
298+
}
299+
}
300+
301+
#[cfg(feature = "rustc_serialize")]
302+
impl<S: Encoder, I: Idx, T: Encodable<S>> Encodable<S> for IndexVec<I, T> {
303+
fn encode(&self, s: &mut S) {
304+
Encodable::encode(&self.raw, s);
305+
}
306+
}
307+
308+
#[cfg(feature = "rustc_serialize")]
309+
impl<D: Decoder, I: Idx, T: Decodable<D>> Decodable<D> for IndexVec<I, T> {
310+
fn decode(d: &mut D) -> Self {
311+
IndexVec { raw: Decodable::decode(d), _marker: PhantomData }
312+
}
313+
}
314+
315+
// Whether `IndexVec` is `Send` depends only on the data,
316+
// not the phantom data.
317+
unsafe impl<I: Idx, T> Send for IndexVec<I, T> where T: Send {}
318+
319319
#[cfg(test)]
320320
mod tests;

0 commit comments

Comments
 (0)