1 use rustc_index::vec::{Idx, IndexVec};
8 fn map_id<F>(self, f: F) -> Self
10 F: FnMut(Self::Inner) -> Self::Inner;
13 impl<T> IdFunctor for Box<T> {
17 fn map_id<F>(self, mut f: F) -> Self
19 F: FnMut(Self::Inner) -> Self::Inner,
21 let raw = Box::into_raw(self);
23 // SAFETY: The raw pointer points to a valid value of type `T`.
24 let value = ptr::read(raw);
25 // SAFETY: Converts `Box<T>` to `Box<MaybeUninit<T>>` which is the
26 // inverse of `Box::assume_init()` and should be safe.
27 let mut raw: Box<mem::MaybeUninit<T>> = Box::from_raw(raw.cast());
28 // SAFETY: Write the mapped value back into the `Box`.
29 ptr::write(raw.as_mut_ptr(), f(value));
30 // SAFETY: We just initialized `raw`.
36 impl<T> IdFunctor for Vec<T> {
40 fn map_id<F>(mut self, mut f: F) -> Self
42 F: FnMut(Self::Inner) -> Self::Inner,
44 // FIXME: We don't really care about panics here and leak
45 // far more than we should, but that should be fine for now.
49 let start = self.as_mut_ptr();
52 ptr::write(p, f(ptr::read(p)));
60 impl<T> IdFunctor for Box<[T]> {
64 fn map_id<F>(self, f: F) -> Self
66 F: FnMut(Self::Inner) -> Self::Inner,
68 Vec::from(self).map_id(f).into()
72 impl<I: Idx, T> IdFunctor for IndexVec<I, T> {
76 fn map_id<F>(self, f: F) -> Self
78 F: FnMut(Self::Inner) -> Self::Inner,
80 IndexVec::from_raw(self.raw.map_id(f))