use alloc::boxed::Box;
use alloc::vec::Vec;
use core::ops::RangeBounds;
use crate::MelinoeCell;
use super::{BrandedDrain, BrandedVec};
impl<'brand, T> BrandedVec<'brand, T> {
#[inline]
pub fn push(&mut self, value: T) {
self.cells.push(MelinoeCell::new(value));
}
#[inline]
pub fn pop(&mut self) -> Option<T> {
self.cells.pop().map(MelinoeCell::into_inner)
}
#[inline]
pub fn insert(&mut self, index: usize, value: T) {
self.cells.insert(index, MelinoeCell::new(value));
}
#[inline]
pub fn remove(&mut self, index: usize) -> T {
self.cells.remove(index).into_inner()
}
#[inline]
pub fn swap_remove(&mut self, index: usize) -> T {
self.cells.swap_remove(index).into_inner()
}
#[inline]
pub fn swap(&mut self, a: usize, b: usize) {
self.cells.swap(a, b);
}
#[inline]
pub fn reserve(&mut self, additional: usize) {
self.cells.reserve(additional);
}
#[inline]
pub fn clear(&mut self) {
self.cells.clear();
}
#[inline]
pub fn truncate(&mut self, len: usize) {
self.cells.truncate(len);
}
#[inline]
pub fn shrink_to_fit(&mut self) {
self.cells.shrink_to_fit();
}
#[inline]
pub fn resize_with<F>(&mut self, new_len: usize, mut f: F)
where
F: FnMut() -> T,
{
self.cells.resize_with(new_len, || MelinoeCell::new(f()));
}
#[inline]
pub fn retain_mut<F>(&mut self, mut f: F)
where
F: FnMut(&mut T) -> bool,
{
self.cells.retain_mut(|cell| f(cell.get_mut()));
}
#[inline]
#[must_use]
pub fn into_boxed_cells(self) -> Box<[MelinoeCell<'brand, T>]> {
self.cells.into_boxed_slice()
}
#[inline]
#[must_use]
pub fn into_vec(self) -> Vec<T> {
unsafe {
let mut cells = core::mem::ManuallyDrop::new(self.cells);
let ptr = cells.as_mut_ptr().cast::<T>();
Vec::from_raw_parts(ptr, cells.len(), cells.capacity())
}
}
#[inline]
pub fn drain<R>(&mut self, range: R) -> BrandedDrain<'_, 'brand, T>
where
R: RangeBounds<usize>,
{
BrandedDrain::new(self.cells.drain(range))
}
#[inline]
#[must_use]
pub fn split_off(&mut self, at: usize) -> Self {
Self {
cells: self.cells.split_off(at),
}
}
#[inline]
pub fn append(&mut self, other: &mut Self) {
self.cells.append(&mut other.cells);
}
}