pub struct VecAllocation<'a, T: 'a> {
vec: &'a mut Vec<T>,
index: usize,
}
impl<T> VecAllocation<'_, T> {
#[inline(always)]
pub fn init(self, value: T) -> usize {
unsafe {
std::ptr::write(self.vec.as_mut_ptr().add(self.index), value);
self.vec.set_len(self.index + 1);
}
self.index
}
}
pub trait VecHelper<T> {
fn alloc(&mut self) -> VecAllocation<T>;
}
impl<T> VecHelper<T> for Vec<T> {
fn alloc(&mut self) -> VecAllocation<T> {
let index = self.len();
if self.capacity() == index {
self.reserve(1);
}
VecAllocation { vec: self, index }
}
}