use core::mem::MaybeUninit;
#[cfg(feature = "alloc")]
use std::boxed::Box;
mod array;
#[cfg(any(doc, feature = "alloc"))]
pub(crate) mod heap;
mod slice;
mod capacity;
pub struct AllocError;
pub type AllocResult = Result<(), AllocError>;
pub unsafe trait Storage: AsRef<[MaybeUninit<Self::Item>]> + AsMut<[MaybeUninit<Self::Item>]> {
type Item;
#[doc(hidden)]
const CONST_CAPACITY: Option<usize> = None;
fn reserve(&mut self, new_capacity: usize);
fn try_reserve(&mut self, new_capacity: usize) -> AllocResult;
}
pub unsafe trait StorageWithCapacity: Storage + Sized {
fn with_capacity(capacity: usize) -> Self;
#[doc(hidden)]
#[allow(non_snake_case)]
fn __with_capacity__const_capacity_checked(capacity: usize, _old_capacity: Option<usize>) -> Self {
Self::with_capacity(capacity)
}
}
unsafe impl<S: ?Sized + Storage> Storage for &mut S {
type Item = S::Item;
#[doc(hidden)]
const CONST_CAPACITY: Option<usize> = S::CONST_CAPACITY;
#[inline]
fn reserve(&mut self, new_capacity: usize) { S::reserve(self, new_capacity); }
#[inline]
fn try_reserve(&mut self, new_capacity: usize) -> AllocResult { S::try_reserve(self, new_capacity) }
}
#[cfg(any(doc, feature = "alloc"))]
pub struct BoxStorage<S: ?Sized + Storage>(pub Box<S>);
#[cfg(any(doc, feature = "alloc"))]
impl<S: ?Sized + Storage> AsRef<[MaybeUninit<S::Item>]> for BoxStorage<S> {
fn as_ref(&self) -> &[MaybeUninit<S::Item>] { self.0.as_ref().as_ref() }
}
#[cfg(any(doc, feature = "alloc"))]
impl<S: ?Sized + Storage> AsMut<[MaybeUninit<S::Item>]> for BoxStorage<S> {
fn as_mut(&mut self) -> &mut [MaybeUninit<S::Item>] { self.0.as_mut().as_mut() }
}
#[cfg(any(doc, feature = "alloc"))]
unsafe impl<S: ?Sized + Storage> Storage for BoxStorage<S> {
type Item = S::Item;
#[doc(hidden)]
const CONST_CAPACITY: Option<usize> = S::CONST_CAPACITY;
#[inline]
fn reserve(&mut self, new_capacity: usize) { S::reserve(&mut self.0, new_capacity); }
#[inline]
fn try_reserve(&mut self, new_capacity: usize) -> AllocResult { S::try_reserve(&mut self.0, new_capacity) }
}
#[cfg(any(doc, feature = "alloc"))]
unsafe impl<S: ?Sized + StorageWithCapacity> StorageWithCapacity for BoxStorage<S> {
fn with_capacity(capacity: usize) -> Self { Self(Box::new(S::with_capacity(capacity))) }
#[doc(hidden)]
#[allow(non_snake_case)]
fn __with_capacity__const_capacity_checked(capacity: usize, old_capacity: Option<usize>) -> Self {
Self(Box::new(S::__with_capacity__const_capacity_checked(
capacity,
old_capacity,
)))
}
}