@@ -2473,6 +2473,45 @@ where
24732473 self
24742474 }
24752475
2476+ /// Consume the array, call `f` by **v**alue on each element, and return an
2477+ /// owned array with the new values. Works for **any** `F: FnMut(A)->B`.
2478+ ///
2479+ /// If `A` and `B` are the same type then the map is performed by delegating
2480+ /// to [`mapv_into()`] and then converting into an owned array. This avoids
2481+ /// unnecessary memory allocations in [`mapv()`].
2482+ ///
2483+ /// If `A` and `B` are different types then a new array is allocated and the
2484+ /// map is performed as in [`mapv()`].
2485+ ///
2486+ /// Elements are visited in arbitrary order.
2487+ pub fn mapv_into_any < B , F > ( self , mut f : F ) -> Array < B , D >
2488+ where
2489+ S : DataMut ,
2490+ F : FnMut ( A ) -> B ,
2491+ A : Clone + ' static ,
2492+ B : ' static ,
2493+ {
2494+ if core:: any:: TypeId :: of :: < A > ( ) == core:: any:: TypeId :: of :: < B > ( ) {
2495+ // A and B are the same type.
2496+ // Wrap f in a closure of type FnMut(A) -> A .
2497+ let f = |a| {
2498+ let b = f ( a) ;
2499+ // Safe because A and B are the same type.
2500+ unsafe { unlimited_transmute :: < B , A > ( b) }
2501+ } ;
2502+ // Delegate to mapv_into() using the wrapped closure.
2503+ // Convert output to a uniquely owned array of type Array<A, D>.
2504+ let output = self . mapv_into ( f) . into_owned ( ) ;
2505+ // Change the return type from Array<A, D> to Array<B, D>.
2506+ // Again, safe because A and B are the same type.
2507+ unsafe { unlimited_transmute :: < Array < A , D > , Array < B , D > > ( output) }
2508+ } else {
2509+ // A and B are not the same type.
2510+ // Fallback to mapv().
2511+ self . mapv ( f)
2512+ }
2513+ }
2514+
24762515 /// Modify the array in place by calling `f` by mutable reference on each element.
24772516 ///
24782517 /// Elements are visited in arbitrary order.
0 commit comments