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};
53
54
55//^
56//^ ARRAY
57//^
58
59//> ARRAY -> STRUCT
60pub struct Array<Type, const N: usize> {
61    length: usize,
62    data: MaybeUninit<[Type; N]>
63}
64
65//> ARRAY -> INTERNALS
66impl<Type, const N: usize> Array<Type, N> {
67    #[inline]
68    const fn pointer(&mut self) -> NonNull<Type> {return NonNull::new(self.data.as_mut_ptr()).unwrap().cast()}
69}
70
71//> ARRAY -> IMPLEMENTATION
72impl<Type, const N: usize> Array<Type, N> {
73    #[inline]
74    pub const fn capacity(&self) -> usize {return N}
75    #[inline]
76    pub const fn new() -> Self {return Self::default()}
77    #[inline]
78    pub const fn push(&mut self, value: Type) -> () {
79        assert!(self.length != N, "array capacity exceeded");
80        unsafe {self.pointer().add(self.length).write(value)};
81        self.length += 1;
82    }
83    #[inline]
84    pub const fn push_mut<'valid>(&'valid mut self, value: Type) -> &'valid mut Type {
85        let index = self.length;
86        self.push(value);
87        return &mut self[index];
88    }
89    #[inline]
90    pub const fn pop(&mut self) -> Option<Type> {return if self.length == 0 {None} else {
91        self.length -= 1;
92        Some(unsafe {self.pointer().add(self.length).read()})
93    }}
94    #[inline]
95    pub fn clear(&mut self) -> () {return self.truncate(0)}
96    #[inline]
97    pub fn truncate(&mut self, length: usize) -> () {
98        for index in length..self.length {unsafe {drop(self.pointer().add(index).read())}}
99        self.length = length
100    }
101    #[inline]
102    pub const fn insert(&mut self, index: usize, value: Type) -> () {
103        assert!(index <= self.length, "tried to insert out of bounds");
104        assert!(self.length != N, "array capacity exceeded");
105        let pointer = unsafe {self.pointer().add(index)};
106        unsafe {copy(pointer.as_ptr(), pointer.add(1).as_ptr(), self.length - index)}
107        unsafe {pointer.write(value)}
108        self.length += 1;
109    }
110    #[inline]
111    pub const fn insert_mut<'valid>(&'valid mut self, index: usize, value: Type) -> &'valid mut Type {
112        self.insert(index, value);
113        return &mut self[index];
114    }
115    #[inline]
116    pub const fn remove(&mut self, index: usize) -> Type {
117        assert!(index < self.length, "tried to remove out of bounds");
118        let pointer = unsafe {self.pointer().add(index)};
119        let value = unsafe {pointer.read()};
120        unsafe {copy(pointer.add(1).as_ptr(), pointer.as_ptr(), self.length - 1 - index)};
121        self.length -= 1;
122        return value;
123    }
124}
125
126//> ARRAY -> DROP
127impl<Type, const N: usize> Drop for Array<Type, N> {
128    #[inline]
129    fn drop(&mut self) {self.clear()}
130}
131
132//> ARRAY -> DEBUG
133impl<Type: Debug, const N: usize> Debug for Array<Type, N> {
134    fn fmt(&self, formatter: &mut Formatter<'_>) -> Format {Debug::fmt(self.as_ref(), formatter)}
135}
136
137//> ARRAY -> EXTEND
138impl<Type, const N: usize> Extend<Type> for Array<Type, N> {
139    #[inline]
140    fn extend<T: IntoIterator<Item = Type>>(&mut self, iter: T) {for item in iter {self.push(item)}}
141}
142
143//> ARRAY -> CLONE
144impl<Type: Clone, const N: usize> Clone for Array<Type, N> {
145    #[inline]
146    fn clone(&self) -> Self {return Self::from_iter(self.as_ref().into_iter().cloned())}
147}
148
149//> ARRAY -> DEFAULT
150const impl<Type, const N: usize> Default for Array<Type, N> {
151    #[inline]
152    fn default() -> Self {return Self {
153        data: MaybeUninit::uninit(),
154        length: 0
155    }}
156}