From 408e2ac3bb35a54529ea0647a024fb817a27a8c2 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Thu, 30 Mar 2023 11:19:38 -0700 Subject: [PATCH] Add `IndexSlice` to go with `IndexVec` Moves the methods that don't need full `IndexVec`-ness over to `IndexSlice`, and have `IndexVec` deref to `IndexSlice` so everything keeps working. --- compiler/rustc_index/src/vec.rs | 230 ++++++++++++++++++++++---------- 1 file changed, 160 insertions(+), 70 deletions(-) diff --git a/compiler/rustc_index/src/vec.rs b/compiler/rustc_index/src/vec.rs index acf883fe90cec..596849bd45682 100644 --- a/compiler/rustc_index/src/vec.rs +++ b/compiler/rustc_index/src/vec.rs @@ -5,7 +5,7 @@ use std::fmt; use std::fmt::Debug; use std::hash::Hash; use std::marker::PhantomData; -use std::ops::{Index, IndexMut, RangeBounds}; +use std::ops::{Deref, DerefMut, Index, IndexMut, RangeBounds}; use std::slice; use std::vec; @@ -52,15 +52,27 @@ impl Idx for u32 { } #[derive(Clone, PartialEq, Eq, Hash)] +#[repr(transparent)] pub struct IndexVec { pub raw: Vec, _marker: PhantomData, } +#[derive(PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct IndexSlice { + _marker: PhantomData, + pub raw: [T], +} + // Whether `IndexVec` is `Send` depends only on the data, // not the phantom data. unsafe impl Send for IndexVec where T: Send {} +// Whether `IndexSlice` is `Send` depends only on the data, +// not the phantom data. +unsafe impl Send for IndexSlice where T: Send {} + #[cfg(feature = "rustc_serialize")] impl> Encodable for IndexVec { fn encode(&self, s: &mut S) { @@ -122,6 +134,16 @@ impl IndexVec { Self::from_raw(indices.map(func).collect()) } + #[inline] + pub fn as_slice(&self) -> &IndexSlice { + IndexSlice::from_raw(&self.raw) + } + + #[inline] + pub fn as_mut_slice(&mut self) -> &mut IndexSlice { + IndexSlice::from_raw_mut(&mut self.raw) + } + #[inline] pub fn push(&mut self, d: T) -> I { let idx = I::new(self.len()); @@ -135,32 +157,119 @@ impl IndexVec { } #[inline] - pub fn len(&self) -> usize { - self.raw.len() + pub fn into_iter(self) -> vec::IntoIter { + self.raw.into_iter() } - /// Gives the next index that will be assigned when `push` is - /// called. #[inline] - pub fn next_index(&self) -> I { - I::new(self.len()) + pub fn into_iter_enumerated( + self, + ) -> impl DoubleEndedIterator + ExactSizeIterator { + self.raw.into_iter().enumerate().map(|(n, t)| (I::new(n), t)) } #[inline] - pub fn is_empty(&self) -> bool { - self.raw.is_empty() + pub fn drain<'a, R: RangeBounds>( + &'a mut self, + range: R, + ) -> impl Iterator + 'a { + self.raw.drain(range) } #[inline] - pub fn into_iter(self) -> vec::IntoIter { - self.raw.into_iter() + pub fn drain_enumerated<'a, R: RangeBounds>( + &'a mut self, + range: R, + ) -> impl Iterator + 'a { + let begin = match range.start_bound() { + std::ops::Bound::Included(i) => *i, + std::ops::Bound::Excluded(i) => i.checked_add(1).unwrap(), + std::ops::Bound::Unbounded => 0, + }; + self.raw.drain(range).enumerate().map(move |(n, t)| (I::new(begin + n), t)) } #[inline] - pub fn into_iter_enumerated( - self, - ) -> impl DoubleEndedIterator + ExactSizeIterator { - self.raw.into_iter().enumerate().map(|(n, t)| (I::new(n), t)) + pub fn shrink_to_fit(&mut self) { + self.raw.shrink_to_fit() + } + + #[inline] + pub fn truncate(&mut self, a: usize) { + self.raw.truncate(a) + } + + pub fn convert_index_type(self) -> IndexVec { + IndexVec { raw: self.raw, _marker: PhantomData } + } + + /// Grows the index vector so that it contains an entry for + /// `elem`; if that is already true, then has no + /// effect. Otherwise, inserts new values as needed by invoking + /// `fill_value`. + #[inline] + pub fn ensure_contains_elem(&mut self, elem: I, fill_value: impl FnMut() -> T) { + let min_new_len = elem.index() + 1; + if self.len() < min_new_len { + self.raw.resize_with(min_new_len, fill_value); + } + } + + #[inline] + pub fn resize_to_elem(&mut self, elem: I, fill_value: impl FnMut() -> T) { + let min_new_len = elem.index() + 1; + self.raw.resize_with(min_new_len, fill_value); + } +} + +impl Deref for IndexVec { + type Target = IndexSlice; + + #[inline] + fn deref(&self) -> &Self::Target { + self.as_slice() + } +} + +impl DerefMut for IndexVec { + #[inline] + fn deref_mut(&mut self) -> &mut Self::Target { + self.as_mut_slice() + } +} + +impl IndexSlice { + #[inline] + pub fn from_raw(raw: &[T]) -> &Self { + let ptr: *const [T] = raw; + // SAFETY: `IndexSlice` is `repr(transparent)` over a normal slice + unsafe { &*(ptr as *const Self) } + } + + #[inline] + pub fn from_raw_mut(raw: &mut [T]) -> &mut Self { + let ptr: *mut [T] = raw; + // SAFETY: `IndexSlice` is `repr(transparent)` over a normal slice + unsafe { &mut *(ptr as *mut Self) } + } + + #[inline] + pub fn len(&self) -> usize { + self.raw.len() + } + + /// Gives the next index that will be assigned when `push` is called. + /// + /// Manual bounds checks can be done using `idx < slice.next_index()` + /// (as opposed to `idx.index() < slice.len()`). + #[inline] + pub fn next_index(&self) -> I { + I::new(self.len()) + } + + #[inline] + pub fn is_empty(&self) -> bool { + self.raw.is_empty() } #[inline] @@ -194,47 +303,16 @@ impl IndexVec { self.raw.iter_mut().enumerate().map(|(n, t)| (I::new(n), t)) } - #[inline] - pub fn drain<'a, R: RangeBounds>( - &'a mut self, - range: R, - ) -> impl Iterator + 'a { - self.raw.drain(range) - } - - #[inline] - pub fn drain_enumerated<'a, R: RangeBounds>( - &'a mut self, - range: R, - ) -> impl Iterator + 'a { - let begin = match range.start_bound() { - std::ops::Bound::Included(i) => *i, - std::ops::Bound::Excluded(i) => i.checked_add(1).unwrap(), - std::ops::Bound::Unbounded => 0, - }; - self.raw.drain(range).enumerate().map(move |(n, t)| (I::new(begin + n), t)) - } - #[inline] pub fn last_index(&self) -> Option { self.len().checked_sub(1).map(I::new) } - #[inline] - pub fn shrink_to_fit(&mut self) { - self.raw.shrink_to_fit() - } - #[inline] pub fn swap(&mut self, a: I, b: I) { self.raw.swap(a.index(), b.index()) } - #[inline] - 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()) @@ -274,28 +352,6 @@ impl IndexVec { let ptr = self.raw.as_mut_ptr(); unsafe { (&mut *ptr.add(ai), &mut *ptr.add(bi), &mut *ptr.add(ci)) } } - - pub fn convert_index_type(self) -> IndexVec { - IndexVec { raw: self.raw, _marker: PhantomData } - } - - /// Grows the index vector so that it contains an entry for - /// `elem`; if that is already true, then has no - /// effect. Otherwise, inserts new values as needed by invoking - /// `fill_value`. - #[inline] - pub fn ensure_contains_elem(&mut self, elem: I, fill_value: impl FnMut() -> T) { - let min_new_len = elem.index() + 1; - if self.len() < min_new_len { - self.raw.resize_with(min_new_len, fill_value); - } - } - - #[inline] - pub fn resize_to_elem(&mut self, elem: I, fill_value: impl FnMut() -> T) { - let min_new_len = elem.index() + 1; - self.raw.resize_with(min_new_len, fill_value); - } } /// `IndexVec` is often used as a map, so it provides some map-like APIs. @@ -336,7 +392,7 @@ impl IndexVec { } } -impl Index for IndexVec { +impl Index for IndexSlice { type Output = T; #[inline] @@ -345,7 +401,7 @@ impl Index for IndexVec { } } -impl IndexMut for IndexVec { +impl IndexMut for IndexSlice { #[inline] fn index_mut(&mut self, index: I) -> &mut T { &mut self.raw[index.index()] @@ -359,6 +415,20 @@ impl Default for IndexVec { } } +impl Default for &IndexSlice { + #[inline] + fn default() -> Self { + IndexSlice::from_raw(Default::default()) + } +} + +impl Default for &mut IndexSlice { + #[inline] + fn default() -> Self { + IndexSlice::from_raw_mut(Default::default()) + } +} + impl Extend for IndexVec { #[inline] fn extend>(&mut self, iter: J) { @@ -418,5 +488,25 @@ impl<'a, I: Idx, T> IntoIterator for &'a mut IndexVec { } } +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() + } +} + #[cfg(test)] mod tests;