use crate::{alloc, base::marker::ThreadBound};
#[derive(Debug)]
#[repr(transparent)]
#[must_use = "insert a value or abandon the reserved position"]
pub struct Vacant<'a, T> {
pub(in crate::alloc) values: &'a mut Vec<T>,
pub(in crate::alloc) _bound: ThreadBound,
}
impl<'a, T> Vacant<'a, T> {
pub fn try_new(values: &'a mut Vec<T>) -> Result<Self, alloc::Error> {
use alloc::raw::VecExt as _;
alloc::VecExt::try_reserve(values, 1)?;
Ok(unsafe { values.vacancy() })
}
pub fn insert(self, value: T) {
let len = self.values.len();
unsafe {
self.values.as_mut_ptr().add(len).write(value);
self.values.set_len(len + 1);
}
}
}
const _: () = {
assert!(size_of::<Vacant<'_, ()>>() == size_of::<usize>());
assert!(size_of::<Option<Vacant<'_, ()>>>() == size_of::<usize>());
assert!(!std::mem::needs_drop::<Vacant<'_, ()>>());
};