Add Array Impl Lang Item in various places
Add basic test And also run fmt which is where the other changes are from Fix mut issues These only appear when running tests, so resolved by adding mut Swap order of forget Add pub and rm guard impl Add explicit type to guard Add safety note Change guard type from T to S It should never have been T, as it guards over [MaybeUninit<S>; N] Also add feature to test
This commit is contained in:
@@ -377,7 +377,7 @@ impl<T, const N: usize> [T; N] {
|
||||
/// assert_eq!(y, [2,3,4]);
|
||||
/// ```
|
||||
#[unstable(feature = "array_map", issue = "77777")]
|
||||
fn map<F, S>(self, f: F) -> [S; N]
|
||||
pub fn map<F, S>(self, mut f: F) -> [S; N]
|
||||
where
|
||||
F: FnMut(T) -> S,
|
||||
{
|
||||
@@ -387,12 +387,6 @@ impl<T, const N: usize> [T; N] {
|
||||
curr_init: usize,
|
||||
}
|
||||
|
||||
impl<T, const N: usize> Guard<T, N> {
|
||||
fn new(dst: &mut [MaybeUninit<T>; N]) -> Self {
|
||||
Guard { dst: dst as *mut _ as *mut T, curr_init: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const N: usize> Drop for Guard<T, N> {
|
||||
fn drop(&mut self) {
|
||||
debug_assert!(self.curr_init <= N);
|
||||
@@ -406,14 +400,17 @@ impl<T, const N: usize> [T; N] {
|
||||
}
|
||||
}
|
||||
}
|
||||
let dst = MaybeUninit::uninit_array::<N>();
|
||||
let mut guard = Guard::new(&mut dst);
|
||||
for (i, e) in self.into_iter().enumerate() {
|
||||
let mut dst = MaybeUninit::uninit_array::<N>();
|
||||
let mut guard: Guard<S, N> = Guard { dst: &mut dst as *mut _ as *mut S, curr_init: 0 };
|
||||
for (i, e) in IntoIter::new(self).enumerate() {
|
||||
dst[i] = MaybeUninit::new(f(e));
|
||||
guard.curr_init += 1;
|
||||
}
|
||||
// FIXME convert to crate::mem::transmute when works with generics
|
||||
// unsafe { crate::mem::transmute::<[MaybeUninit<S>; N], [S; N]>(dst) }
|
||||
crate::mem::forget(guard);
|
||||
// SAFETY: At this point we've properly initialized the whole array
|
||||
// and we just need to cast it to the correct type
|
||||
unsafe { (&mut dst as *mut _ as *mut [S; N]).read() }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user