1#![no_std]
7
8#![allow(incomplete_features)]
10
11#![feature(const_cmp)]
13#![feature(deref_pure_trait)]
14#![feature(const_trait_impl)]
15#![feature(trusted_len)]
16#![feature(const_slice_make_iter)]
17#![feature(test)]
18#![feature(generic_const_exprs)]
19#![feature(const_index)]
20#![feature(const_iter)]
21#![feature(const_convert)]
22#![feature(const_default)]
23
24extern crate alloc;
26extern crate test;
27
28#[cfg(test)]
30mod benches;
31mod comparisons;
32mod conversions;
33mod index;
34mod iterators;
35mod references;
36#[cfg(test)]
37mod tests;
38
39use core::{
41 fmt::{
42 Debug,
43 Formatter,
44 Result as Format
45 },
46 mem::MaybeUninit,
47 ops::Drop,
48 ptr::{
49 NonNull,
50 copy
51 },
52 array::from_fn as arrayfn
53};
54
55
56pub struct Array<Type, const N: usize> {
62 length: usize,
63 data: MaybeUninit<[Type; N]>
64}
65
66impl<Type, const N: usize> Array<Type, N> {
68 const fn pointer(&mut self) -> NonNull<Type> {return NonNull::new(self.data.as_mut_ptr()).unwrap().cast()}
69}
70
71impl<Type, const N: usize> Array<Type, N> {
73 pub const fn new() -> Self {return Self::default()}
74 pub const fn push(&mut self, value: Type) -> () {
75 assert!(self.length != N, "array capacity exceeded");
76 unsafe {self.pointer().add(self.length).write(value)};
77 self.length += 1;
78 }
79 pub const fn push_mut<'valid>(&'valid mut self, value: Type) -> &'valid mut Type {
80 let index = self.length;
81 self.push(value);
82 return &mut self[index];
83 }
84 pub const fn pop(&mut self) -> Option<Type> {return if self.length == 0 {None} else {
85 self.length -= 1;
86 Some(unsafe {self.pointer().add(self.length).read()})
87 }}
88 pub fn clear(&mut self) -> () {return self.truncate(0)}
89 pub fn truncate(&mut self, length: usize) -> () {
90 for index in length..self.length {unsafe {drop(self.pointer().add(index).read())}}
91 self.length = length
92 }
93 pub const fn insert(&mut self, index: usize, value: Type) -> () {
94 assert!(index <= self.length, "tried to insert out of bounds");
95 assert!(self.length != N, "array capacity exceeded");
96 let pointer = unsafe {self.pointer().add(index)};
97 unsafe {copy(pointer.as_ptr(), pointer.add(1).as_ptr(), self.length - index)}
98 unsafe {pointer.write(value)}
99 self.length += 1;
100 }
101 pub const fn insert_mut<'valid>(&'valid mut self, index: usize, value: Type) -> &'valid mut Type {
102 self.insert(index, value);
103 return &mut self[index];
104 }
105 pub const fn remove(&mut self, index: usize) -> Type {
106 assert!(index < self.length, "tried to remove out of bounds");
107 let pointer = unsafe {self.pointer().add(index)};
108 let value = unsafe {pointer.read()};
109 unsafe {copy(pointer.add(1).as_ptr(), pointer.as_ptr(), self.length - 1 - index)};
110 self.length -= 1;
111 return value;
112 }
113 pub fn retain(&mut self, mut closure: impl FnMut(&Type) -> bool) -> () {
114 let mut new = Array::new();
115 for (index, passes) in arrayfn::<Option<bool>, N, _>(|index| self.get(index).map(&mut closure)).into_iter().enumerate() {match passes {
116 None => break,
117 Some(true) => new.push(unsafe {self.pointer().add(index).read()}),
118 Some(false) => unsafe {self.pointer().add(index).read();}
119 }}
120 *self = new;
121 }
122}
123
124impl<Type, const N: usize> Drop for Array<Type, N> {
126 fn drop(&mut self) {self.clear()}
127}
128
129impl<Type: Debug, const N: usize> Debug for Array<Type, N> {
131 fn fmt(&self, formatter: &mut Formatter<'_>) -> Format {Debug::fmt(self.as_ref(), formatter)}
132}
133
134impl<Type, const N: usize> Extend<Type> for Array<Type, N> {
136 fn extend<T: IntoIterator<Item = Type>>(&mut self, iter: T) {for item in iter {self.push(item)}}
137}
138
139impl<Type: Clone, const N: usize> Clone for Array<Type, N> {
141 fn clone(&self) -> Self {return Self::from_iter(self.as_ref().into_iter().cloned())}
142}
143
144const impl<Type, const N: usize> Default for Array<Type, N> {
146 fn default() -> Self {return Self {
147 data: MaybeUninit::uninit(),
148 length: 0
149 }}
150}