From 4f9fe856a25ab29345b90e7725509e9ee38a37be Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:19:41 +0200 Subject: Adding upstream version 1.69.0+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_data_structures/src/functor.rs | 51 ++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'compiler/rustc_data_structures/src/functor.rs') diff --git a/compiler/rustc_data_structures/src/functor.rs b/compiler/rustc_data_structures/src/functor.rs index 84cb417dd..28fcf80b3 100644 --- a/compiler/rustc_data_structures/src/functor.rs +++ b/compiler/rustc_data_structures/src/functor.rs @@ -1,5 +1,5 @@ use rustc_index::vec::{Idx, IndexVec}; -use std::mem; +use std::{mem, rc::Rc, sync::Arc}; pub trait IdFunctor: Sized { type Inner; @@ -65,3 +65,52 @@ impl IdFunctor for IndexVec { self.raw.try_map_id(f).map(IndexVec::from_raw) } } + +macro_rules! rc { + ($($rc:ident),+) => {$( + impl IdFunctor for $rc { + type Inner = T; + + #[inline] + fn try_map_id(mut self, mut f: F) -> Result + where + F: FnMut(Self::Inner) -> Result, + { + // We merely want to replace the contained `T`, if at all possible, + // so that we don't needlessly allocate a new `$rc` or indeed clone + // the contained type. + unsafe { + // First step is to ensure that we have a unique reference to + // the contained type, which `$rc::make_mut` will accomplish (by + // allocating a new `$rc` and cloning the `T` only if required). + // This is done *before* casting to `$rc>` so that + // panicking during `make_mut` does not leak the `T`. + $rc::make_mut(&mut self); + + // Casting to `$rc>` is safe because `ManuallyDrop` + // is `repr(transparent)`. + let ptr = $rc::into_raw(self).cast::>(); + let mut unique = $rc::from_raw(ptr); + + // Call to `$rc::make_mut` above guarantees that `unique` is the + // sole reference to the contained value, so we can avoid doing + // a checked `get_mut` here. + let slot = $rc::get_mut_unchecked(&mut unique); + + // Semantically move the contained type out from `unique`, fold + // it, then move the folded value back into `unique`. Should + // folding fail, `ManuallyDrop` ensures that the "moved-out" + // value is not re-dropped. + let owned = mem::ManuallyDrop::take(slot); + let folded = f(owned)?; + *slot = mem::ManuallyDrop::new(folded); + + // Cast back to `$rc`. + Ok($rc::from_raw($rc::into_raw(unique).cast())) + } + } + } + )+}; +} + +rc! { Rc, Arc } -- cgit v1.2.3