Skip to content

Commit 9f714ef

Browse files
committed
Delegate from map_id to try_map_id
1 parent 04f1c09 commit 9f714ef

File tree

2 files changed

+7
-57
lines changed

2 files changed

+7
-57
lines changed

compiler/rustc_data_structures/src/functor.rs

+6-57
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ use std::ptr;
55
pub trait IdFunctor: Sized {
66
type Inner;
77

8-
fn map_id<F>(self, f: F) -> Self
8+
#[inline]
9+
fn map_id<F>(self, mut f: F) -> Self
910
where
10-
F: FnMut(Self::Inner) -> Self::Inner;
11+
F: FnMut(Self::Inner) -> Self::Inner,
12+
{
13+
self.try_map_id::<_, !>(|value| Ok(f(value))).into_ok()
14+
}
1115

1216
fn try_map_id<F, E>(self, f: F) -> Result<Self, E>
1317
where
@@ -17,25 +21,6 @@ pub trait IdFunctor: Sized {
1721
impl<T> IdFunctor for Box<T> {
1822
type Inner = T;
1923

20-
#[inline]
21-
fn map_id<F>(self, mut f: F) -> Self
22-
where
23-
F: FnMut(Self::Inner) -> Self::Inner,
24-
{
25-
let raw = Box::into_raw(self);
26-
unsafe {
27-
// SAFETY: The raw pointer points to a valid value of type `T`.
28-
let value = ptr::read(raw);
29-
// SAFETY: Converts `Box<T>` to `Box<MaybeUninit<T>>` which is the
30-
// inverse of `Box::assume_init()` and should be safe.
31-
let mut raw: Box<mem::MaybeUninit<T>> = Box::from_raw(raw.cast());
32-
// SAFETY: Write the mapped value back into the `Box`.
33-
raw.write(f(value));
34-
// SAFETY: We just initialized `raw`.
35-
raw.assume_init()
36-
}
37-
}
38-
3924
#[inline]
4025
fn try_map_id<F, E>(self, mut f: F) -> Result<Self, E>
4126
where
@@ -59,26 +44,6 @@ impl<T> IdFunctor for Box<T> {
5944
impl<T> IdFunctor for Vec<T> {
6045
type Inner = T;
6146

62-
#[inline]
63-
fn map_id<F>(mut self, mut f: F) -> Self
64-
where
65-
F: FnMut(Self::Inner) -> Self::Inner,
66-
{
67-
// FIXME: We don't really care about panics here and leak
68-
// far more than we should, but that should be fine for now.
69-
let len = self.len();
70-
unsafe {
71-
self.set_len(0);
72-
let start = self.as_mut_ptr();
73-
for i in 0..len {
74-
let p = start.add(i);
75-
ptr::write(p, f(ptr::read(p)));
76-
}
77-
self.set_len(len);
78-
}
79-
self
80-
}
81-
8247
#[inline]
8348
fn try_map_id<F, E>(mut self, mut f: F) -> Result<Self, E>
8449
where
@@ -119,14 +84,6 @@ impl<T> IdFunctor for Vec<T> {
11984
impl<T> IdFunctor for Box<[T]> {
12085
type Inner = T;
12186

122-
#[inline]
123-
fn map_id<F>(self, f: F) -> Self
124-
where
125-
F: FnMut(Self::Inner) -> Self::Inner,
126-
{
127-
Vec::from(self).map_id(f).into()
128-
}
129-
13087
#[inline]
13188
fn try_map_id<F, E>(self, f: F) -> Result<Self, E>
13289
where
@@ -139,14 +96,6 @@ impl<T> IdFunctor for Box<[T]> {
13996
impl<I: Idx, T> IdFunctor for IndexVec<I, T> {
14097
type Inner = T;
14198

142-
#[inline]
143-
fn map_id<F>(self, f: F) -> Self
144-
where
145-
F: FnMut(Self::Inner) -> Self::Inner,
146-
{
147-
IndexVec::from_raw(self.raw.map_id(f))
148-
}
149-
15099
#[inline]
151100
fn try_map_id<F, E>(self, f: F) -> Result<Self, E>
152101
where

compiler/rustc_data_structures/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#![feature(once_cell)]
2626
#![feature(test)]
2727
#![feature(thread_id_value)]
28+
#![feature(unwrap_infallible)]
2829
#![allow(rustc::default_hash_types)]
2930
#![deny(unaligned_references)]
3031

0 commit comments

Comments
 (0)