about summary refs log tree commit diff
path: root/compiler/rustc_data_structures
diff options
context:
space:
mode:
authorAlan Egerton <eggyal@gmail.com>2021-11-27 16:40:47 +0000
committerAlan Egerton <eggyal@gmail.com>2021-11-27 16:55:21 +0000
commit9f714ef03567d588fbb63c662670fd9326d7348e (patch)
tree8ed1aec35bd2ef4eaa239cd3c9cb378b51b78632 /compiler/rustc_data_structures
parent04f1c09f90abebd0c6a7658105dec57099a63caa (diff)
downloadrust-9f714ef03567d588fbb63c662670fd9326d7348e.tar.gz
rust-9f714ef03567d588fbb63c662670fd9326d7348e.zip
Delegate from `map_id` to `try_map_id`
Diffstat (limited to 'compiler/rustc_data_structures')
-rw-r--r--compiler/rustc_data_structures/src/functor.rs63
-rw-r--r--compiler/rustc_data_structures/src/lib.rs1
2 files changed, 7 insertions, 57 deletions
diff --git a/compiler/rustc_data_structures/src/functor.rs b/compiler/rustc_data_structures/src/functor.rs
index d3715a998ce..8f441a1841e 100644
--- a/compiler/rustc_data_structures/src/functor.rs
+++ b/compiler/rustc_data_structures/src/functor.rs
@@ -5,9 +5,13 @@ use std::ptr;
 pub trait IdFunctor: Sized {
     type Inner;
 
-    fn map_id<F>(self, f: F) -> Self
+    #[inline]
+    fn map_id<F>(self, mut f: F) -> Self
     where
-        F: FnMut(Self::Inner) -> Self::Inner;
+        F: FnMut(Self::Inner) -> Self::Inner,
+    {
+        self.try_map_id::<_, !>(|value| Ok(f(value))).into_ok()
+    }
 
     fn try_map_id<F, E>(self, f: F) -> Result<Self, E>
     where
@@ -18,25 +22,6 @@ impl<T> IdFunctor for Box<T> {
     type Inner = T;
 
     #[inline]
-    fn map_id<F>(self, mut f: F) -> Self
-    where
-        F: FnMut(Self::Inner) -> Self::Inner,
-    {
-        let raw = Box::into_raw(self);
-        unsafe {
-            // SAFETY: The raw pointer points to a valid value of type `T`.
-            let value = ptr::read(raw);
-            // SAFETY: Converts `Box<T>` to `Box<MaybeUninit<T>>` which is the
-            // inverse of `Box::assume_init()` and should be safe.
-            let mut raw: Box<mem::MaybeUninit<T>> = Box::from_raw(raw.cast());
-            // SAFETY: Write the mapped value back into the `Box`.
-            raw.write(f(value));
-            // SAFETY: We just initialized `raw`.
-            raw.assume_init()
-        }
-    }
-
-    #[inline]
     fn try_map_id<F, E>(self, mut f: F) -> Result<Self, E>
     where
         F: FnMut(Self::Inner) -> Result<Self::Inner, E>,
@@ -60,26 +45,6 @@ impl<T> IdFunctor for Vec<T> {
     type Inner = T;
 
     #[inline]
-    fn map_id<F>(mut self, mut f: F) -> Self
-    where
-        F: FnMut(Self::Inner) -> Self::Inner,
-    {
-        // FIXME: We don't really care about panics here and leak
-        // far more than we should, but that should be fine for now.
-        let len = self.len();
-        unsafe {
-            self.set_len(0);
-            let start = self.as_mut_ptr();
-            for i in 0..len {
-                let p = start.add(i);
-                ptr::write(p, f(ptr::read(p)));
-            }
-            self.set_len(len);
-        }
-        self
-    }
-
-    #[inline]
     fn try_map_id<F, E>(mut self, mut f: F) -> Result<Self, E>
     where
         F: FnMut(Self::Inner) -> Result<Self::Inner, E>,
@@ -120,14 +85,6 @@ impl<T> IdFunctor for Box<[T]> {
     type Inner = T;
 
     #[inline]
-    fn map_id<F>(self, f: F) -> Self
-    where
-        F: FnMut(Self::Inner) -> Self::Inner,
-    {
-        Vec::from(self).map_id(f).into()
-    }
-
-    #[inline]
     fn try_map_id<F, E>(self, f: F) -> Result<Self, E>
     where
         F: FnMut(Self::Inner) -> Result<Self::Inner, E>,
@@ -140,14 +97,6 @@ impl<I: Idx, T> IdFunctor for IndexVec<I, T> {
     type Inner = T;
 
     #[inline]
-    fn map_id<F>(self, f: F) -> Self
-    where
-        F: FnMut(Self::Inner) -> Self::Inner,
-    {
-        IndexVec::from_raw(self.raw.map_id(f))
-    }
-
-    #[inline]
     fn try_map_id<F, E>(self, f: F) -> Result<Self, E>
     where
         F: FnMut(Self::Inner) -> Result<Self::Inner, E>,
diff --git a/compiler/rustc_data_structures/src/lib.rs b/compiler/rustc_data_structures/src/lib.rs
index 77784bf1705..d4eb622e780 100644
--- a/compiler/rustc_data_structures/src/lib.rs
+++ b/compiler/rustc_data_structures/src/lib.rs
@@ -25,6 +25,7 @@
 #![feature(once_cell)]
 #![feature(test)]
 #![feature(thread_id_value)]
+#![feature(unwrap_infallible)]
 #![allow(rustc::default_hash_types)]
 #![deny(unaligned_references)]