Skip to main content

libutils_array/
mod.rs

1//^
2//^ HEAD
3//^
4
5//> HEAD -> NO_STD
6#![no_std]
7
8//> HEAD -> LINTS
9#![allow(incomplete_features)]
10
11//> HEAD -> FEATURES
12#![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
24//> HEAD -> CRATES
25extern crate alloc;
26extern crate test;
27
28//> HEAD -> MODULES
29#[cfg(test)]
30mod benches;
31mod comparisons;
32mod conversions;
33mod index;
34mod iterators;
35mod references;
36#[cfg(test)]
37mod tests;
38
39//> HEAD -> CORE
40use 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
56//^
57//^ ARRAY
58//^
59
60//> ARRAY -> STRUCT
61pub struct Array<Type, const N: usize> {
62    length: usize,
63    data: MaybeUninit<[Type; N]>
64}
65
66//> ARRAY -> INTERNALS
67impl<Type, const N: usize> Array<Type, N> {
68    #[inline]
69    const fn pointer(&mut self) -> NonNull<Type> {return NonNull::new(self.data.as_mut_ptr()).unwrap().cast()}
70}
71
72//> ARRAY -> IMPLEMENTATION
73impl<Type, const N: usize> Array<Type, N> {
74    #[inline]
75    pub const fn new() -> Self {return Self::default()}
76    #[inline]
77    pub const fn push(&mut self, value: Type) -> () {
78        assert!(self.length != N, "array capacity exceeded");
79        unsafe {self.pointer().add(self.length).write(value)};
80        self.length += 1;
81    }
82    #[inline]
83    pub const fn push_mut<'valid>(&'valid mut self, value: Type) -> &'valid mut Type {
84        let index = self.length;
85        self.push(value);
86        return &mut self[index];
87    }
88    #[inline]
89    pub const fn pop(&mut self) -> Option<Type> {return if self.length == 0 {None} else {
90        self.length -= 1;
91        Some(unsafe {self.pointer().add(self.length).read()})
92    }}
93    #[inline]
94    pub fn clear(&mut self) -> () {return self.truncate(0)}
95    #[inline]
96    pub fn truncate(&mut self, length: usize) -> () {
97        for index in length..self.length {unsafe {drop(self.pointer().add(index).read())}}
98        self.length = length
99    }
100    #[inline]
101    pub const fn insert(&mut self, index: usize, value: Type) -> () {
102        assert!(index <= self.length, "tried to insert out of bounds");
103        assert!(self.length != N, "array capacity exceeded");
104        let pointer = unsafe {self.pointer().add(index)};
105        unsafe {copy(pointer.as_ptr(), pointer.add(1).as_ptr(), self.length - index)}
106        unsafe {pointer.write(value)}
107        self.length += 1;
108    }
109    #[inline]
110    pub const fn insert_mut<'valid>(&'valid mut self, index: usize, value: Type) -> &'valid mut Type {
111        self.insert(index, value);
112        return &mut self[index];
113    }
114    #[inline]
115    pub const fn remove(&mut self, index: usize) -> Type {
116        assert!(index < self.length, "tried to remove out of bounds");
117        let pointer = unsafe {self.pointer().add(index)};
118        let value = unsafe {pointer.read()};
119        unsafe {copy(pointer.add(1).as_ptr(), pointer.as_ptr(), self.length - 1 - index)};
120        self.length -= 1;
121        return value;
122    }
123    #[inline]
124    pub fn retain(&mut self, mut closure: impl FnMut(&Type) -> bool) -> () {
125        let mut new = Array::new();
126        for (index, passes) in arrayfn::<Option<bool>, N, _>(|index| self.get(index).map(&mut closure)).into_iter().enumerate() {match passes {
127            None => break,
128            Some(true) => new.push(unsafe {self.pointer().add(index).read()}),
129            Some(false) => unsafe {self.pointer().add(index).read();}
130        }}
131        *self = new;
132    }
133}
134
135//> ARRAY -> DROP
136impl<Type, const N: usize> Drop for Array<Type, N> {
137    #[inline]
138    fn drop(&mut self) {self.clear()}
139}
140
141//> ARRAY -> DEBUG
142impl<Type: Debug, const N: usize> Debug for Array<Type, N> {
143    fn fmt(&self, formatter: &mut Formatter<'_>) -> Format {Debug::fmt(self.as_ref(), formatter)}
144}
145
146//> ARRAY -> EXTEND
147impl<Type, const N: usize> Extend<Type> for Array<Type, N> {
148    #[inline]
149    fn extend<T: IntoIterator<Item = Type>>(&mut self, iter: T) {for item in iter {self.push(item)}}
150}
151
152//> ARRAY -> CLONE
153impl<Type: Clone, const N: usize> Clone for Array<Type, N> {
154    #[inline]
155    fn clone(&self) -> Self {return Self::from_iter(self.as_ref().into_iter().cloned())}
156}
157
158//> ARRAY -> DEFAULT
159const impl<Type, const N: usize> Default for Array<Type, N> {
160    #[inline]
161    fn default() -> Self {return Self {
162        data: MaybeUninit::uninit(),
163        length: 0
164    }}
165}