@@ -5,9 +5,13 @@ use std::ptr;
5
5
pub trait IdFunctor : Sized {
6
6
type Inner ;
7
7
8
- fn map_id < F > ( self , f : F ) -> Self
8
+ #[ inline]
9
+ fn map_id < F > ( self , mut f : F ) -> Self
9
10
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
+ }
11
15
12
16
fn try_map_id < F , E > ( self , f : F ) -> Result < Self , E >
13
17
where
@@ -17,25 +21,6 @@ pub trait IdFunctor: Sized {
17
21
impl < T > IdFunctor for Box < T > {
18
22
type Inner = T ;
19
23
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
-
39
24
#[ inline]
40
25
fn try_map_id < F , E > ( self , mut f : F ) -> Result < Self , E >
41
26
where
@@ -59,26 +44,6 @@ impl<T> IdFunctor for Box<T> {
59
44
impl < T > IdFunctor for Vec < T > {
60
45
type Inner = T ;
61
46
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
-
82
47
#[ inline]
83
48
fn try_map_id < F , E > ( mut self , mut f : F ) -> Result < Self , E >
84
49
where
@@ -119,14 +84,6 @@ impl<T> IdFunctor for Vec<T> {
119
84
impl < T > IdFunctor for Box < [ T ] > {
120
85
type Inner = T ;
121
86
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
-
130
87
#[ inline]
131
88
fn try_map_id < F , E > ( self , f : F ) -> Result < Self , E >
132
89
where
@@ -139,14 +96,6 @@ impl<T> IdFunctor for Box<[T]> {
139
96
impl < I : Idx , T > IdFunctor for IndexVec < I , T > {
140
97
type Inner = T ;
141
98
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
-
150
99
#[ inline]
151
100
fn try_map_id < F , E > ( self , f : F ) -> Result < Self , E >
152
101
where
0 commit comments