diff --git a/compiler/rustc_index/src/slice.rs b/compiler/rustc_index/src/slice.rs index cad96ca26bc..8eae079e2f8 100644 --- a/compiler/rustc_index/src/slice.rs +++ b/compiler/rustc_index/src/slice.rs @@ -5,7 +5,7 @@ slice, }; -use crate::{idx::Idx, vec::IndexVec}; +use crate::{Idx, IndexVec}; /// A view into contiguous `T`s, indexed by `I` rather than by `usize`. /// @@ -140,6 +140,17 @@ pub fn pick3_mut(&mut self, a: I, b: I, c: I) -> (&mut T, &mut T, &mut T) { let ptr = self.raw.as_mut_ptr(); unsafe { (&mut *ptr.add(ai), &mut *ptr.add(bi), &mut *ptr.add(ci)) } } + + #[inline] + pub fn binary_search(&self, value: &T) -> Result + where + T: Ord, + { + match self.raw.binary_search(value) { + Ok(i) => Ok(Idx::new(i)), + Err(i) => Err(Idx::new(i)), + } + } } impl IndexSlice { @@ -172,20 +183,6 @@ pub fn invert_bijective_mapping(&self) -> IndexVec { } } -impl IndexSlice { - #[inline] - pub fn binary_search(&self, value: &T) -> Result { - match self.raw.binary_search(value) { - Ok(i) => Ok(Idx::new(i)), - Err(i) => Err(Idx::new(i)), - } - } -} - -// Whether `IndexSlice` is `Send` depends only on the data, -// not the phantom data. -unsafe impl Send for IndexSlice where T: Send {} - impl fmt::Debug for IndexSlice { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&self.raw, fmt) @@ -208,6 +205,26 @@ fn index_mut(&mut self, index: I) -> &mut T { } } +impl<'a, I: Idx, T> IntoIterator for &'a IndexSlice { + type Item = &'a T; + type IntoIter = slice::Iter<'a, T>; + + #[inline] + fn into_iter(self) -> slice::Iter<'a, T> { + self.raw.iter() + } +} + +impl<'a, I: Idx, T> IntoIterator for &'a mut IndexSlice { + type Item = &'a mut T; + type IntoIter = slice::IterMut<'a, T>; + + #[inline] + fn into_iter(self) -> slice::IterMut<'a, T> { + self.raw.iter_mut() + } +} + impl ToOwned for IndexSlice { type Owned = IndexVec; @@ -234,22 +251,6 @@ fn default() -> Self { } } -impl<'a, I: Idx, T> IntoIterator for &'a IndexSlice { - type Item = &'a T; - type IntoIter = slice::Iter<'a, T>; - - #[inline] - fn into_iter(self) -> slice::Iter<'a, T> { - self.raw.iter() - } -} - -impl<'a, I: Idx, T> IntoIterator for &'a mut IndexSlice { - type Item = &'a mut T; - type IntoIter = slice::IterMut<'a, T>; - - #[inline] - fn into_iter(self) -> slice::IterMut<'a, T> { - self.raw.iter_mut() - } -} +// Whether `IndexSlice` is `Send` depends only on the data, +// not the phantom data. +unsafe impl Send for IndexSlice where T: Send {} diff --git a/compiler/rustc_index/src/vec.rs b/compiler/rustc_index/src/vec.rs index 3cd2eb56936..dce5334461e 100644 --- a/compiler/rustc_index/src/vec.rs +++ b/compiler/rustc_index/src/vec.rs @@ -9,8 +9,7 @@ use std::slice; use std::vec; -use crate::idx::Idx; -use crate::slice::IndexSlice; +use crate::{Idx, IndexSlice}; /// An owned contiguous collection of `T`s, indexed by `I` rather than by `usize`. /// @@ -25,30 +24,6 @@ pub struct IndexVec { _marker: PhantomData, } -// Whether `IndexVec` is `Send` depends only on the data, -// not the phantom data. -unsafe impl Send for IndexVec where T: Send {} - -#[cfg(feature = "rustc_serialize")] -impl> Encodable for IndexVec { - fn encode(&self, s: &mut S) { - Encodable::encode(&self.raw, s); - } -} - -#[cfg(feature = "rustc_serialize")] -impl> Decodable for IndexVec { - fn decode(d: &mut D) -> Self { - IndexVec { raw: Decodable::decode(d), _marker: PhantomData } - } -} - -impl fmt::Debug for IndexVec { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(&self.raw, fmt) - } -} - impl IndexVec { #[inline] pub fn new() -> Self { @@ -183,6 +158,14 @@ pub fn ensure_contains_elem(&mut self, elem: I, fill_value: impl FnMut() -> T) - &mut self[elem] } + #[inline] + pub fn resize(&mut self, new_len: usize, value: T) + where + T: Clone, + { + self.raw.resize(new_len, value) + } + #[inline] pub fn resize_to_elem(&mut self, elem: I, fill_value: impl FnMut() -> T) { let min_new_len = elem.index() + 1; @@ -190,6 +173,30 @@ pub fn resize_to_elem(&mut self, elem: I, fill_value: impl FnMut() -> T) { } } +/// `IndexVec` is often used as a map, so it provides some map-like APIs. +impl IndexVec> { + #[inline] + pub fn insert(&mut self, index: I, value: T) -> Option { + self.ensure_contains_elem(index, || None).replace(value) + } + + #[inline] + pub fn get_or_insert_with(&mut self, index: I, value: impl FnOnce() -> T) -> &mut T { + self.ensure_contains_elem(index, || None).get_or_insert_with(value) + } + + #[inline] + pub fn remove(&mut self, index: I) -> Option { + self.get_mut(index)?.take() + } +} + +impl fmt::Debug for IndexVec { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(&self.raw, fmt) + } +} + impl Deref for IndexVec { type Target = IndexSlice; @@ -218,38 +225,6 @@ fn borrow_mut(&mut self) -> &mut IndexSlice { } } -/// `IndexVec` is often used as a map, so it provides some map-like APIs. -impl IndexVec> { - #[inline] - pub fn insert(&mut self, index: I, value: T) -> Option { - self.ensure_contains_elem(index, || None).replace(value) - } - - #[inline] - pub fn get_or_insert_with(&mut self, index: I, value: impl FnOnce() -> T) -> &mut T { - self.ensure_contains_elem(index, || None).get_or_insert_with(value) - } - - #[inline] - pub fn remove(&mut self, index: I) -> Option { - self.get_mut(index)?.take() - } -} - -impl IndexVec { - #[inline] - pub fn resize(&mut self, new_len: usize, value: T) { - self.raw.resize(new_len, value) - } -} - -impl Default for IndexVec { - #[inline] - fn default() -> Self { - Self::new() - } -} - impl Extend for IndexVec { #[inline] fn extend>(&mut self, iter: J) { @@ -279,13 +254,6 @@ fn from_iter(iter: J) -> Self } } -impl From<[T; N]> for IndexVec { - #[inline] - fn from(array: [T; N]) -> Self { - IndexVec::from_raw(array.into()) - } -} - impl IntoIterator for IndexVec { type Item = T; type IntoIter = vec::IntoIter; @@ -316,5 +284,37 @@ fn into_iter(self) -> slice::IterMut<'a, T> { } } +impl Default for IndexVec { + #[inline] + fn default() -> Self { + Self::new() + } +} + +impl From<[T; N]> for IndexVec { + #[inline] + fn from(array: [T; N]) -> Self { + IndexVec::from_raw(array.into()) + } +} + +#[cfg(feature = "rustc_serialize")] +impl> Encodable for IndexVec { + fn encode(&self, s: &mut S) { + Encodable::encode(&self.raw, s); + } +} + +#[cfg(feature = "rustc_serialize")] +impl> Decodable for IndexVec { + fn decode(d: &mut D) -> Self { + IndexVec { raw: Decodable::decode(d), _marker: PhantomData } + } +} + +// Whether `IndexVec` is `Send` depends only on the data, +// not the phantom data. +unsafe impl Send for IndexVec where T: Send {} + #[cfg(test)] mod tests;