use alloc::collections::VecDeque;
use core::iter::FromIterator;
use crate::MelinoeCell;
#[derive(Debug, Default)]
pub struct BrandedVecDeque<'brand, T> {
pub(crate) cells: VecDeque<MelinoeCell<'brand, T>>,
}
impl<'brand, T> BrandedVecDeque<'brand, T> {
#[inline]
#[must_use]
pub fn new() -> Self {
Self {
cells: VecDeque::new(),
}
}
#[inline]
#[must_use]
pub fn with_capacity(capacity: usize) -> Self {
Self {
cells: VecDeque::with_capacity(capacity),
}
}
#[inline]
#[must_use]
pub fn len(&self) -> usize {
self.cells.len()
}
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.cells.is_empty()
}
#[inline]
#[must_use]
pub fn capacity(&self) -> usize {
self.cells.capacity()
}
#[inline]
#[must_use]
pub fn as_cells(&self) -> &VecDeque<MelinoeCell<'brand, T>> {
&self.cells
}
#[inline]
#[must_use]
pub fn as_mut_cells(&mut self) -> &mut VecDeque<MelinoeCell<'brand, T>> {
&mut self.cells
}
}
impl<'brand, T> FromIterator<T> for BrandedVecDeque<'brand, T> {
#[inline]
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Self {
cells: iter.into_iter().map(MelinoeCell::new).collect(),
}
}
}
impl<'brand, T> Extend<T> for BrandedVecDeque<'brand, T> {
#[inline]
fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = T>,
{
self.cells.extend(iter.into_iter().map(MelinoeCell::new));
}
}
impl<'brand, T> From<VecDeque<T>> for BrandedVecDeque<'brand, T> {
#[inline]
fn from(values: VecDeque<T>) -> Self {
unsafe {
let values = core::mem::ManuallyDrop::new(values);
let cells = core::ptr::read(
core::ptr::addr_of!(*values).cast::<VecDeque<MelinoeCell<'brand, T>>>(),
);
Self { cells }
}
}
}
impl<'brand, T> IntoIterator for BrandedVecDeque<'brand, T> {
type Item = T;
type IntoIter = alloc::collections::vec_deque::IntoIter<T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.into_vec_deque().into_iter()
}
}