#![allow(unused_mut)]
use core::{cmp::Ordering, fmt, hash, iter::FromIterator, ops, slice};
#[cfg(feature = "alloc")]
pub(crate) type Inner<T, const N: usize> = alloc::vec::Vec<T>;
#[cfg(not(feature = "alloc"))]
pub(crate) type Inner<T, const N: usize> = heapless::Vec<T, N>;
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Vec<T, const N: usize>(Inner<T, N>);
impl<T, const N: usize> Vec<T, N> {
#[allow(clippy::reserve_after_initialization)]
#[inline]
pub fn new() -> Self {
let mut v = Inner::new();
#[cfg(feature = "alloc")]
v.reserve(N);
Self(v)
}
#[inline]
pub fn from_slice(other: &[T]) -> crate::Result<Self>
where
T: Clone,
{
let mut v = Self::new();
v.extend_from_slice(other)?;
Ok(v)
}
#[inline]
pub fn as_ptr(&self) -> *const T {
self.0.as_ptr()
}
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut T {
self.0.as_mut_ptr()
}
#[inline]
pub fn as_slice(&self) -> &[T] {
&self.0
}
#[inline]
pub fn into_array<const M: usize>(self) -> Result<[T; M], Self> {
#[cfg(feature = "alloc")]
{
self.0.try_into().map_err(Self)
}
#[cfg(not(feature = "alloc"))]
{
self.0.into_array().map_err(Self)
}
}
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [T] {
&mut self.0
}
#[inline]
pub fn capacity(&self) -> usize {
self.0.capacity()
}
#[inline]
pub fn clear(&mut self) {
self.0.clear();
}
#[inline]
pub fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = T>,
{
self.0.extend(iter)
}
#[inline]
pub fn extend_from_slice(&mut self, other: &[T]) -> crate::Result<()>
where
T: Clone,
{
#[cfg(feature = "alloc")]
{
self.0.extend_from_slice(other);
Ok(())
}
#[cfg(not(feature = "alloc"))]
{
self.0
.extend_from_slice(other)
.map_err(|_| crate::Error::BufferOverflow)
}
}
#[inline]
pub fn pop(&mut self) -> Option<T> {
self.0.pop()
}
#[inline]
pub fn push(&mut self, item: T) -> Result<(), T> {
#[cfg(feature = "alloc")]
{
self.0.push(item);
Ok(())
}
#[cfg(not(feature = "alloc"))]
{
self.0.push(item)
}
}
#[inline]
pub unsafe fn pop_unchecked(&mut self) -> T {
self.0.pop().unwrap()
}
#[inline]
pub unsafe fn push_unchecked(&mut self, item: T) {
#[cfg(feature = "alloc")]
self.0.push(item);
#[cfg(not(feature = "alloc"))]
self.0.push_unchecked(item);
}
#[inline]
pub fn truncate(&mut self, len: usize) {
self.0.truncate(len)
}
#[inline]
pub fn resize(&mut self, new_len: usize, value: T) -> crate::Result<()>
where
T: Clone,
{
#[cfg(feature = "alloc")]
{
self.0.resize(new_len, value);
Ok(())
}
#[cfg(not(feature = "alloc"))]
{
self.0
.resize(new_len, value)
.map_err(|_| crate::Error::BufferOverflow)
}
}
#[inline]
pub fn resize_default(&mut self, new_len: usize) -> crate::Result<()>
where
T: Clone + Default,
{
self.resize(new_len, T::default())
}
#[inline]
pub fn swap_remove(&mut self, index: usize) -> T {
self.0.swap_remove(index)
}
#[inline]
pub unsafe fn swap_remove_unchecked(&mut self, index: usize) -> T {
self.swap_remove(index)
}
#[inline]
pub fn is_full(&self) -> bool {
self.0.len() == self.0.capacity()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[inline]
pub fn starts_with(&self, needle: &[T]) -> bool
where
T: PartialEq,
{
self.0.starts_with(needle)
}
#[inline]
pub fn ends_with(&self, needle: &[T]) -> bool
where
T: PartialEq,
{
self.0.ends_with(needle)
}
#[inline]
pub fn insert(&mut self, index: usize, element: T) -> Result<(), T> {
#[cfg(feature = "alloc")]
{
self.0.insert(index, element);
Ok(())
}
#[cfg(not(feature = "alloc"))]
{
self.0.insert(index, element)
}
}
#[inline]
pub fn remove(&mut self, index: usize) -> T {
self.0.remove(index)
}
#[inline]
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&T) -> bool,
{
self.0.retain(f)
}
#[inline]
pub fn retain_mut<F>(&mut self, mut f: F)
where
F: FnMut(&mut T) -> bool,
{
self.0.retain_mut(f)
}
#[inline]
pub fn inner(&self) -> &Inner<T, N> {
&self.0
}
#[inline]
pub fn inner_mut(&mut self) -> &mut Inner<T, N> {
&mut self.0
}
#[inline]
pub fn into_inner(self) -> Inner<T, N> {
self.0
}
}
impl<T, const N: usize> Default for Vec<T, N> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<const N: usize> fmt::Write for Vec<u8, N> {
#[inline]
fn write_str(&mut self, s: &str) -> fmt::Result {
#[cfg(feature = "alloc")]
{
self.0.extend_from_slice(s.as_bytes());
Ok(())
}
#[cfg(not(feature = "alloc"))]
{
self.0.write_str(s)
}
}
}
impl<'a, T: Clone, const N: usize> TryFrom<&'a [T]> for Vec<T, N> {
type Error = crate::Error;
#[inline]
fn try_from(slice: &'a [T]) -> Result<Self, Self::Error> {
Vec::from_slice(slice)
}
}
impl<T, const N: usize> From<Vec<T, N>> for Inner<T, N> {
#[inline]
fn from(vec: Vec<T, N>) -> Self {
vec.0
}
}
impl<T, const N: usize> From<Inner<T, N>> for Vec<T, N> {
#[inline]
fn from(inner: Inner<T, N>) -> Self {
Self(inner)
}
}
impl<T, const N: usize> Extend<T> for Vec<T, N> {
#[inline]
fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = T>,
{
self.extend(iter)
}
}
impl<'a, T, const N: usize> Extend<&'a T> for Vec<T, N>
where
T: 'a + Copy,
{
#[inline]
fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = &'a T>,
{
self.extend(iter.into_iter().cloned())
}
}
impl<T, const N: usize> hash::Hash for Vec<T, N>
where
T: core::hash::Hash,
{
#[inline]
fn hash<H: hash::Hasher>(&self, state: &mut H) {
hash::Hash::hash(&self.0, state);
}
}
impl<'a, T, const N: usize> IntoIterator for &'a Vec<T, N> {
type Item = &'a T;
type IntoIter = slice::Iter<'a, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a, T, const N: usize> IntoIterator for &'a mut Vec<T, N> {
type Item = &'a mut T;
type IntoIter = slice::IterMut<'a, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
impl<T, const N: usize> FromIterator<T> for Vec<T, N> {
#[inline]
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = T>,
{
Self(FromIterator::<T>::from_iter(iter))
}
}
#[derive(Clone, Debug)]
pub struct IntoIter<T, const N: usize> {
#[cfg(feature = "alloc")]
iter: alloc::vec::IntoIter<T>,
#[cfg(not(feature = "alloc"))]
vec: heapless::Vec<T, N>,
#[cfg(not(feature = "alloc"))]
next: usize,
}
impl<T, const N: usize> Iterator for IntoIter<T, N> {
type Item = T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
#[cfg(feature = "alloc")]
{
self.iter.next()
}
#[cfg(not(feature = "alloc"))]
{
if self.next < self.vec.len() {
let item = unsafe { (self.vec.as_ptr().add(self.next)).read() };
self.next += 1;
Some(item)
} else {
None
}
}
}
}
impl<T, const N: usize> IntoIterator for Vec<T, N> {
type Item = T;
type IntoIter = IntoIter<T, N>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
IntoIter {
#[cfg(feature = "alloc")]
iter: self.0.into_iter(),
#[cfg(not(feature = "alloc"))]
vec: self.0,
#[cfg(not(feature = "alloc"))]
next: 0,
}
}
}
#[cfg(not(feature = "alloc"))]
impl<T, const N: usize> Drop for IntoIter<T, N> {
fn drop(&mut self) {
unsafe {
core::ptr::drop_in_place(&mut self.vec.as_mut_slice()[self.next..]);
self.vec.set_len(0);
}
}
}
impl<A, B, const N1: usize, const N2: usize> PartialEq<Vec<B, N2>> for Vec<A, N1>
where
A: PartialEq<B>,
{
#[inline]
fn eq(&self, other: &Vec<B, N2>) -> bool {
self.0.eq(&other.0)
}
}
impl<A, B, const N: usize> PartialEq<[B]> for Vec<A, N>
where
A: PartialEq<B>,
{
#[inline]
fn eq(&self, other: &[B]) -> bool {
self.0.eq(other)
}
}
impl<A, B, const N: usize> PartialEq<Vec<A, N>> for [B]
where
A: PartialEq<B>,
{
#[inline]
fn eq(&self, other: &Vec<A, N>) -> bool {
other.0.eq(self)
}
}
impl<A, B, const N: usize> PartialEq<&[B]> for Vec<A, N>
where
A: PartialEq<B>,
{
#[inline]
fn eq(&self, other: &&[B]) -> bool {
self.0.eq(*other)
}
}
impl<A, B, const N: usize> PartialEq<Vec<A, N>> for &[B]
where
A: PartialEq<B>,
{
#[inline]
fn eq(&self, other: &Vec<A, N>) -> bool {
other.0.eq(self)
}
}
impl<A, B, const N: usize> PartialEq<&mut [B]> for Vec<A, N>
where
A: PartialEq<B>,
{
#[inline]
fn eq(&self, other: &&mut [B]) -> bool {
self.0.eq(*other)
}
}
impl<A, B, const N: usize> PartialEq<Vec<A, N>> for &mut [B]
where
A: PartialEq<B>,
{
#[inline]
fn eq(&self, other: &Vec<A, N>) -> bool {
other.0.eq(self)
}
}
impl<A, B, const N: usize, const M: usize> PartialEq<[B; M]> for Vec<A, N>
where
A: PartialEq<B>,
{
#[inline]
fn eq(&self, other: &[B; M]) -> bool {
self.0.eq(other)
}
}
impl<A, B, const N: usize, const M: usize> PartialEq<Vec<A, N>> for [B; M]
where
A: PartialEq<B>,
{
#[inline]
fn eq(&self, other: &Vec<A, N>) -> bool {
other.0.eq(self)
}
}
impl<A, B, const N: usize, const M: usize> PartialEq<&[B; M]> for Vec<A, N>
where
A: PartialEq<B>,
{
#[inline]
fn eq(&self, other: &&[B; M]) -> bool {
self.0.eq(*other)
}
}
impl<A, B, const N: usize, const M: usize> PartialEq<Vec<A, N>> for &[B; M]
where
A: PartialEq<B>,
{
#[inline]
fn eq(&self, other: &Vec<A, N>) -> bool {
other.0.eq(self)
}
}
impl<T, const N: usize> Eq for Vec<T, N> where T: Eq {}
impl<T, const N1: usize, const N2: usize> PartialOrd<Vec<T, N2>> for Vec<T, N1>
where
T: PartialOrd,
{
#[inline]
fn partial_cmp(&self, other: &Vec<T, N2>) -> Option<Ordering> {
self.0.partial_cmp(&other.0)
}
}
impl<T, const N: usize> Ord for Vec<T, N>
where
T: Ord,
{
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.0.cmp(&other.0)
}
}
impl<T, const N: usize> ops::Deref for Vec<T, N> {
type Target = [T];
#[inline]
fn deref(&self) -> &[T] {
self.as_slice()
}
}
impl<T, const N: usize> ops::DerefMut for Vec<T, N> {
#[inline]
fn deref_mut(&mut self) -> &mut [T] {
self.as_mut_slice()
}
}
impl<T, const N: usize> AsRef<Vec<T, N>> for Vec<T, N> {
#[inline]
fn as_ref(&self) -> &Self {
self
}
}
impl<T, const N: usize> AsMut<Vec<T, N>> for Vec<T, N> {
#[inline]
fn as_mut(&mut self) -> &mut Self {
self
}
}
impl<T, const N: usize> AsRef<[T]> for Vec<T, N> {
#[inline]
fn as_ref(&self) -> &[T] {
self
}
}
impl<T, const N: usize> AsMut<[T]> for Vec<T, N> {
#[inline]
fn as_mut(&mut self) -> &mut [T] {
self
}
}