diff options
| author | James Miller <james@aatch.net> | 2017-02-08 22:19:22 +1300 |
|---|---|---|
| committer | Eduard-Mihai Burtescu <edy.burt@gmail.com> | 2017-03-10 02:55:39 +0200 |
| commit | d708a4072a9fa35be3cb6a1e74557925507ba606 (patch) | |
| tree | 70c0bb012cf2547b7156f16a5b6ca09c7f7aa5c1 | |
| parent | ec87925325c005922977ebf9b10dc9e98d8a6d40 (diff) | |
| download | rust-d708a4072a9fa35be3cb6a1e74557925507ba606.tar.gz rust-d708a4072a9fa35be3cb6a1e74557925507ba606.zip | |
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.
| -rw-r--r-- | src/librustc/ty/structural_impls.rs | 11 | ||||
| -rw-r--r-- | src/librustc_data_structures/indexed_vec.rs | 23 | ||||
| -rw-r--r-- | src/librustc_data_structures/lib.rs | 1 |
3 files changed, 35 insertions, 0 deletions
diff --git a/src/librustc/ty/structural_impls.rs b/src/librustc/ty/structural_impls.rs index 48f6fcd11b8..49824e8a738 100644 --- a/src/librustc/ty/structural_impls.rs +++ b/src/librustc/ty/structural_impls.rs @@ -12,6 +12,7 @@ use infer::type_variable; use ty::{self, Lift, Ty, TyCtxt}; use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor}; use rustc_data_structures::accumulate_vec::AccumulateVec; +use rustc_data_structures::indexed_vec::{IndexVec, Idx}; use std::rc::Rc; use syntax::abi; @@ -834,3 +835,13 @@ impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::error::ExpectedFoun self.expected.visit_with(visitor) || self.found.visit_with(visitor) } } + +impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> { + fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self { + self.iter().map(|x| x.fold_with(folder)).collect() + } + + fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool { + self.iter().any(|t| t.visit_with(visitor)) + } +} diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index 00cea9cbdf6..3f478d7c165 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use std::collections::range::RangeArgument; use std::fmt::Debug; use std::iter::{self, FromIterator}; use std::slice; @@ -146,6 +147,18 @@ impl<I: Idx, T> IndexVec<I, T> { } #[inline] + pub fn drain<'a, R: RangeArgument<usize>>( + &'a mut self, range: R) -> impl Iterator<Item=T> + 'a { + self.raw.drain(range) + } + + #[inline] + pub fn drain_enumerated<'a, R: RangeArgument<usize>>( + &'a mut self, range: R) -> impl Iterator<Item=(I, T)> + 'a { + self.raw.drain(range).enumerate().map(IntoIdx { _marker: PhantomData }) + } + + #[inline] pub fn last(&self) -> Option<I> { self.len().checked_sub(1).map(I::new) } @@ -164,6 +177,16 @@ impl<I: Idx, T> IndexVec<I, T> { pub fn truncate(&mut self, a: usize) { self.raw.truncate(a) } + + #[inline] + pub fn get(&self, index: I) -> Option<&T> { + self.raw.get(index.index()) + } + + #[inline] + pub fn get_mut(&mut self, index: I) -> Option<&mut T> { + self.raw.get_mut(index.index()) + } } impl<I: Idx, T> Index<I> for IndexVec<I, T> { diff --git a/src/librustc_data_structures/lib.rs b/src/librustc_data_structures/lib.rs index 3dce4398f3b..f278325ebec 100644 --- a/src/librustc_data_structures/lib.rs +++ b/src/librustc_data_structures/lib.rs @@ -38,6 +38,7 @@ #![feature(associated_consts)] #![feature(unsize)] #![feature(i128_type)] +#![feature(conservative_impl_trait)] #![cfg_attr(unix, feature(libc))] #![cfg_attr(test, feature(test))] |
