#[cfg(feature = "alloc")]
use alloc::collections::TryReserveError;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::fmt;
pub unsafe trait SlotStorage {
type Item;
type Error: fmt::Debug;
fn empty() -> Self;
fn with_capacity(capacity: usize) -> Self;
fn capacity(&self) -> usize;
fn as_slice(&self) -> &[Self::Item];
fn as_mut_slice(&mut self) -> &mut [Self::Item];
fn ensure_room(&mut self, additional: usize) -> Result<(), Self::Error>;
fn try_push(&mut self, item: Self::Item) -> Result<(), Self::Item>;
fn clear(&mut self);
#[inline]
fn len(&self) -> usize {
self.as_slice().len()
}
#[inline]
fn is_empty(&self) -> bool {
self.as_slice().is_empty()
}
}
pub trait ReserveStorage: SlotStorage {
fn reserve(&mut self, additional: usize);
fn try_reserve(&mut self, additional: usize) -> Result<(), Self::Error>;
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
unsafe impl<S> SlotStorage for Vec<S> {
type Item = S;
type Error = TryReserveError;
#[inline]
fn empty() -> Self {
Vec::new()
}
#[inline]
fn with_capacity(capacity: usize) -> Self {
Vec::with_capacity(capacity)
}
#[inline]
fn capacity(&self) -> usize {
Vec::capacity(self)
}
#[inline]
fn as_slice(&self) -> &[S] {
self
}
#[inline]
fn as_mut_slice(&mut self) -> &mut [S] {
self
}
#[inline]
fn ensure_room(&mut self, additional: usize) -> Result<(), TryReserveError> {
Vec::try_reserve(self, additional)
}
#[inline]
fn try_push(&mut self, item: S) -> Result<(), S> {
match Vec::try_reserve(self, 1) {
Ok(()) => {
Vec::push(self, item);
Ok(())
}
Err(_) => Err(item),
}
}
#[inline]
fn clear(&mut self) {
Vec::clear(self)
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<S> ReserveStorage for Vec<S> {
#[inline]
fn reserve(&mut self, additional: usize) {
Vec::reserve(self, additional);
}
#[inline]
fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
Vec::try_reserve(self, additional)
}
}
#[cfg(feature = "arrayvec")]
#[cfg_attr(docsrs, doc(cfg(feature = "arrayvec")))]
unsafe impl<S, const CAP: usize> SlotStorage for arrayvec::ArrayVec<S, CAP> {
type Item = S;
type Error = arrayvec::CapacityError;
#[inline]
fn empty() -> Self {
arrayvec::ArrayVec::new()
}
#[inline]
fn with_capacity(_capacity: usize) -> Self {
arrayvec::ArrayVec::new_const()
}
#[inline]
fn capacity(&self) -> usize {
CAP
}
#[inline]
fn as_slice(&self) -> &[S] {
self
}
#[inline]
fn as_mut_slice(&mut self) -> &mut [S] {
self
}
#[inline]
fn ensure_room(&mut self, additional: usize) -> Result<(), arrayvec::CapacityError> {
if additional <= arrayvec::ArrayVec::remaining_capacity(self) {
Ok(())
} else {
Err(arrayvec::CapacityError::new(()))
}
}
#[inline]
fn try_push(&mut self, item: S) -> Result<(), S> {
arrayvec::ArrayVec::try_push(self, item).map_err(arrayvec::CapacityError::element)
}
#[inline]
fn clear(&mut self) {
arrayvec::ArrayVec::clear(self);
}
}
#[cfg(feature = "smallvec")]
#[cfg_attr(docsrs, doc(cfg(feature = "smallvec")))]
unsafe impl<S, const N: usize> SlotStorage for smallvec::SmallVec<S, N> {
type Item = S;
type Error = smallvec::CollectionAllocErr;
#[inline]
fn empty() -> Self {
smallvec::SmallVec::new()
}
#[inline]
fn with_capacity(capacity: usize) -> Self {
smallvec::SmallVec::with_capacity(capacity)
}
#[inline]
fn capacity(&self) -> usize {
smallvec::SmallVec::capacity(self)
}
#[inline]
fn as_slice(&self) -> &[S] {
self
}
#[inline]
fn as_mut_slice(&mut self) -> &mut [S] {
self
}
#[inline]
fn ensure_room(&mut self, additional: usize) -> Result<(), smallvec::CollectionAllocErr> {
smallvec::SmallVec::try_reserve(self, additional)
}
#[inline]
fn try_push(&mut self, item: S) -> Result<(), S> {
match smallvec::SmallVec::try_reserve(self, 1) {
Ok(()) => {
smallvec::SmallVec::push(self, item);
Ok(())
}
Err(_) => Err(item),
}
}
#[inline]
fn clear(&mut self) {
smallvec::SmallVec::clear(self);
}
}
#[cfg(feature = "smallvec")]
#[cfg_attr(docsrs, doc(cfg(feature = "smallvec")))]
impl<S, const N: usize> ReserveStorage for smallvec::SmallVec<S, N> {
#[inline]
fn reserve(&mut self, additional: usize) {
smallvec::SmallVec::reserve(self, additional);
}
#[inline]
fn try_reserve(&mut self, additional: usize) -> Result<(), smallvec::CollectionAllocErr> {
smallvec::SmallVec::try_reserve(self, additional)
}
}