#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::{vec, vec::Vec};
use core::mem::{self, MaybeUninit};
pub(crate) struct OutputVec<T> {
data: Vec<T>,
capacity: usize,
}
impl<T> OutputVec<T> {
pub(crate) fn uninit(capacity: usize) -> Self {
Self {
data: Vec::with_capacity(capacity),
capacity,
}
}
pub(crate) fn write(&mut self, idx: usize, value: T) {
let data = self.data.spare_capacity_mut();
data[idx] = MaybeUninit::new(value);
}
pub(crate) unsafe fn drop(&mut self, idx: usize) {
let data = self.data.spare_capacity_mut();
unsafe { data[idx].assume_init_drop() };
}
pub(crate) unsafe fn take(&mut self) -> Vec<T> {
let mut data = vec![];
mem::swap(&mut self.data, &mut data);
unsafe { data.set_len(self.capacity) };
data
}
}