Skip to content

Commit

Permalink
Delegate from map_id to try_map_id
Browse files Browse the repository at this point in the history
  • Loading branch information
eggyal committed Nov 27, 2021
1 parent 04f1c09 commit 9f714ef
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 57 deletions.
63 changes: 6 additions & 57 deletions compiler/rustc_data_structures/src/functor.rs
Expand Up @@ -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
Expand All @@ -17,25 +21,6 @@ pub trait IdFunctor: Sized {
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
Expand All @@ -59,26 +44,6 @@ impl<T> IdFunctor for Box<T> {
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
Expand Down Expand Up @@ -119,14 +84,6 @@ impl<T> IdFunctor for Vec<T> {
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
Expand All @@ -139,14 +96,6 @@ impl<T> IdFunctor for Box<[T]> {
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
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_data_structures/src/lib.rs
Expand Up @@ -25,6 +25,7 @@
#![feature(once_cell)]
#![feature(test)]
#![feature(thread_id_value)]
#![feature(unwrap_infallible)]
#![allow(rustc::default_hash_types)]
#![deny(unaligned_references)]

Expand Down

0 comments on commit 9f714ef

Please sign in to comment.