use core::{
mem::{self, ManuallyDrop, MaybeUninit},
pin::Pin,
};
pub(crate) struct FutureArray<T, const N: usize> {
futures: [ManuallyDrop<T>; N],
}
impl<T, const N: usize> FutureArray<T, N> {
pub(crate) fn new(futures: [T; N]) -> Self {
let futures = MaybeUninit::new(futures);
let futures = unsafe { mem::transmute_copy(&mem::ManuallyDrop::new(futures)) };
Self { futures }
}
pub(crate) fn iter(self: Pin<&mut Self>) -> impl Iterator<Item = Pin<&mut ManuallyDrop<T>>> {
unsafe { self.get_unchecked_mut() }
.futures
.iter_mut()
.map(|t| unsafe { Pin::new_unchecked(t) })
}
pub(crate) unsafe fn drop(mut self: Pin<&mut Self>, idx: usize) {
unsafe {
let futures = self.as_mut().get_unchecked_mut().futures.as_mut();
ManuallyDrop::drop(&mut futures[idx]);
};
}
}