Skip to content

Commit d708a40

Browse files
Aatcheddyb
authored andcommitted
Add extra methods to IndexVec and implement TypeFoldable for it
Adds `get`/`get_mut` accessors and `drain`/`drain_enumerated` iterators to IndexVec. Implements TypeFoldable for IndexVec.
1 parent ec87925 commit d708a40

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/librustc/ty/structural_impls.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use infer::type_variable;
1212
use ty::{self, Lift, Ty, TyCtxt};
1313
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
1414
use rustc_data_structures::accumulate_vec::AccumulateVec;
15+
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
1516

1617
use std::rc::Rc;
1718
use syntax::abi;
@@ -834,3 +835,13 @@ impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::error::ExpectedFoun
834835
self.expected.visit_with(visitor) || self.found.visit_with(visitor)
835836
}
836837
}
838+
839+
impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
840+
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
841+
self.iter().map(|x| x.fold_with(folder)).collect()
842+
}
843+
844+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
845+
self.iter().any(|t| t.visit_with(visitor))
846+
}
847+
}

src/librustc_data_structures/indexed_vec.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use std::collections::range::RangeArgument;
1112
use std::fmt::Debug;
1213
use std::iter::{self, FromIterator};
1314
use std::slice;
@@ -145,6 +146,18 @@ impl<I: Idx, T> IndexVec<I, T> {
145146
self.raw.iter_mut().enumerate().map(IntoIdx { _marker: PhantomData })
146147
}
147148

149+
#[inline]
150+
pub fn drain<'a, R: RangeArgument<usize>>(
151+
&'a mut self, range: R) -> impl Iterator<Item=T> + 'a {
152+
self.raw.drain(range)
153+
}
154+
155+
#[inline]
156+
pub fn drain_enumerated<'a, R: RangeArgument<usize>>(
157+
&'a mut self, range: R) -> impl Iterator<Item=(I, T)> + 'a {
158+
self.raw.drain(range).enumerate().map(IntoIdx { _marker: PhantomData })
159+
}
160+
148161
#[inline]
149162
pub fn last(&self) -> Option<I> {
150163
self.len().checked_sub(1).map(I::new)
@@ -164,6 +177,16 @@ impl<I: Idx, T> IndexVec<I, T> {
164177
pub fn truncate(&mut self, a: usize) {
165178
self.raw.truncate(a)
166179
}
180+
181+
#[inline]
182+
pub fn get(&self, index: I) -> Option<&T> {
183+
self.raw.get(index.index())
184+
}
185+
186+
#[inline]
187+
pub fn get_mut(&mut self, index: I) -> Option<&mut T> {
188+
self.raw.get_mut(index.index())
189+
}
167190
}
168191

169192
impl<I: Idx, T> Index<I> for IndexVec<I, T> {

src/librustc_data_structures/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#![feature(associated_consts)]
3939
#![feature(unsize)]
4040
#![feature(i128_type)]
41+
#![feature(conservative_impl_trait)]
4142

4243
#![cfg_attr(unix, feature(libc))]
4344
#![cfg_attr(test, feature(test))]

0 commit comments

Comments
 (0)