#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::vec::Vec;
use core::{
mem::{self, ManuallyDrop, MaybeUninit},
pin::Pin,
};
pub(crate) struct FutureVec<T> {
futures: Vec<ManuallyDrop<T>>,
}
impl<T> FutureVec<T> {
pub(crate) fn new(futures: Vec<T>) -> 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_slice();
ManuallyDrop::drop(&mut futures[idx]);
};
}
}