@@ -5,9 +5,13 @@ use std::ptr;
55pub 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 {
1721impl < 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> {
5944impl < 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> {
11984impl < 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]> {
13996impl < 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
0 commit comments