From 9208c08a771199e49aa1bff0e80354124eddbe5f Mon Sep 17 00:00:00 2001 From: Alan Egerton Date: Wed, 8 Jun 2022 12:08:00 +0100 Subject: [PATCH] Use liballoc's specialised in-place vec collection liballoc already specialises in-place vector collection, so manually reimplementing it in `IdFunctor::try_map_id` was superfluous. --- compiler/rustc_data_structures/src/functor.rs | 36 ++----------------- 1 file changed, 2 insertions(+), 34 deletions(-) diff --git a/compiler/rustc_data_structures/src/functor.rs b/compiler/rustc_data_structures/src/functor.rs index a3d3f988344..84cb417dd89 100644 --- a/compiler/rustc_data_structures/src/functor.rs +++ b/compiler/rustc_data_structures/src/functor.rs @@ -34,43 +34,11 @@ impl IdFunctor for Vec { type Inner = T; #[inline] - fn try_map_id(self, mut f: F) -> Result + fn try_map_id(self, f: F) -> Result where F: FnMut(Self::Inner) -> Result, { - struct HoleVec { - vec: Vec>, - hole: Option, - } - - impl Drop for HoleVec { - fn drop(&mut self) { - unsafe { - for (index, slot) in self.vec.iter_mut().enumerate() { - if self.hole != Some(index) { - mem::ManuallyDrop::drop(slot); - } - } - } - } - } - - unsafe { - let (ptr, length, capacity) = self.into_raw_parts(); - let vec = Vec::from_raw_parts(ptr.cast(), length, capacity); - let mut hole_vec = HoleVec { vec, hole: None }; - - for (index, slot) in hole_vec.vec.iter_mut().enumerate() { - hole_vec.hole = Some(index); - let original = mem::ManuallyDrop::take(slot); - let mapped = f(original)?; - *slot = mem::ManuallyDrop::new(mapped); - hole_vec.hole = None; - } - - mem::forget(hole_vec); - Ok(Vec::from_raw_parts(ptr, length, capacity)) - } + self.into_iter().map(f).collect() } }