1
Fork 0

Decorative changes to IndexVec

This commit is contained in:
Maybe Waffle 2023-04-19 11:52:43 +00:00
parent 7d23b52376
commit 5d809b1764

View file

@ -27,7 +27,7 @@ pub struct IndexVec<I: Idx, T> {
impl<I: Idx, T> IndexVec<I, T> { impl<I: Idx, T> IndexVec<I, T> {
#[inline] #[inline]
pub const fn new() -> Self { pub const fn new() -> Self {
IndexVec { raw: Vec::new(), _marker: PhantomData } IndexVec::from_raw(Vec::new())
} }
#[inline] #[inline]
@ -37,7 +37,7 @@ impl<I: Idx, T> IndexVec<I, T> {
#[inline] #[inline]
pub fn with_capacity(capacity: usize) -> Self { pub fn with_capacity(capacity: usize) -> Self {
IndexVec { raw: Vec::with_capacity(capacity), _marker: PhantomData } IndexVec::from_raw(Vec::with_capacity(capacity))
} }
/// Creates a new vector with a copy of `elem` for each index in `universe`. /// Creates a new vector with a copy of `elem` for each index in `universe`.
@ -56,7 +56,7 @@ impl<I: Idx, T> IndexVec<I, T> {
where where
T: Clone, T: Clone,
{ {
IndexVec { raw: vec![elem; universe.len()], _marker: PhantomData } IndexVec::from_raw(vec![elem; universe.len()])
} }
#[inline] #[inline]
@ -64,7 +64,7 @@ impl<I: Idx, T> IndexVec<I, T> {
where where
T: Clone, T: Clone,
{ {
IndexVec { raw: vec![elem; n], _marker: PhantomData } IndexVec::from_raw(vec![elem; n])
} }
/// Create an `IndexVec` with `n` elements, where the value of each /// Create an `IndexVec` with `n` elements, where the value of each
@ -72,8 +72,7 @@ impl<I: Idx, T> IndexVec<I, T> {
/// be allocated only once, with a capacity of at least `n`.) /// be allocated only once, with a capacity of at least `n`.)
#[inline] #[inline]
pub fn from_fn_n(func: impl FnMut(I) -> T, n: usize) -> Self { pub fn from_fn_n(func: impl FnMut(I) -> T, n: usize) -> Self {
let indices = (0..n).map(I::new); IndexVec::from_raw((0..n).map(I::new).map(func).collect())
Self::from_raw(indices.map(func).collect())
} }
#[inline] #[inline]
@ -88,7 +87,7 @@ impl<I: Idx, T> IndexVec<I, T> {
#[inline] #[inline]
pub fn push(&mut self, d: T) -> I { pub fn push(&mut self, d: T) -> I {
let idx = I::new(self.len()); let idx = self.next_index();
self.raw.push(d); self.raw.push(d);
idx idx
} }
@ -139,7 +138,7 @@ impl<I: Idx, T> IndexVec<I, T> {
} }
pub fn convert_index_type<Ix: Idx>(self) -> IndexVec<Ix, T> { pub fn convert_index_type<Ix: Idx>(self) -> IndexVec<Ix, T> {
IndexVec { raw: self.raw, _marker: PhantomData } IndexVec::from_raw(self.raw)
} }
/// Grows the index vector so that it contains an entry for /// Grows the index vector so that it contains an entry for
@ -250,7 +249,7 @@ impl<I: Idx, T> FromIterator<T> for IndexVec<I, T> {
where where
J: IntoIterator<Item = T>, J: IntoIterator<Item = T>,
{ {
IndexVec { raw: FromIterator::from_iter(iter), _marker: PhantomData } IndexVec::from_raw(Vec::from_iter(iter))
} }
} }
@ -270,7 +269,7 @@ impl<'a, I: Idx, T> IntoIterator for &'a IndexVec<I, T> {
#[inline] #[inline]
fn into_iter(self) -> slice::Iter<'a, T> { fn into_iter(self) -> slice::Iter<'a, T> {
self.raw.iter() self.iter()
} }
} }
@ -280,14 +279,14 @@ impl<'a, I: Idx, T> IntoIterator for &'a mut IndexVec<I, T> {
#[inline] #[inline]
fn into_iter(self) -> slice::IterMut<'a, T> { fn into_iter(self) -> slice::IterMut<'a, T> {
self.raw.iter_mut() self.iter_mut()
} }
} }
impl<I: Idx, T> Default for IndexVec<I, T> { impl<I: Idx, T> Default for IndexVec<I, T> {
#[inline] #[inline]
fn default() -> Self { fn default() -> Self {
Self::new() IndexVec::new()
} }
} }
@ -308,7 +307,7 @@ impl<S: Encoder, I: Idx, T: Encodable<S>> Encodable<S> for IndexVec<I, T> {
#[cfg(feature = "rustc_serialize")] #[cfg(feature = "rustc_serialize")]
impl<D: Decoder, I: Idx, T: Decodable<D>> Decodable<D> for IndexVec<I, T> { impl<D: Decoder, I: Idx, T: Decodable<D>> Decodable<D> for IndexVec<I, T> {
fn decode(d: &mut D) -> Self { fn decode(d: &mut D) -> Self {
IndexVec { raw: Decodable::decode(d), _marker: PhantomData } IndexVec::from_raw(Vec::<T>::decode(d))
} }
} }