static_collections/
vec.rs1use core::{mem::MaybeUninit, ops::{Deref, DerefMut}, ptr, slice};
4
5#[derive(Debug)]
6pub struct StaticVec<const N:usize,T>
7{
8 length:usize,
9 buff:MaybeUninit<[T;N]>
10}
11
12impl<const N:usize,T:Copy> Clone for StaticVec<N,T>
13{
14 fn clone(&self) -> Self
15 {
16 Self
17 {
18 length:self.length,
19 buff:self.buff
20 }
21 }
22}
23
24impl<const N:usize,T> Default for StaticVec<N,T>
25{
26 fn default() -> Self
27 {
28 Self::new()
29 }
30}
31
32impl<const N:usize,T> StaticVec<N,T>
33{
34 pub const fn new()->Self
44 {
45 Self
46 {
47 length:0,
48 buff:MaybeUninit::uninit()
49 }
50 }
51
52 pub const fn as_slice(&self)->&[T]
53 {
54 unsafe
55 {
56 slice::from_raw_parts(self.as_ptr(),self.length)
57 }
58 }
59
60 pub const fn as_mut_slice(&mut self)->&mut [T]
61 {
62 unsafe
63 {
64 slice::from_raw_parts_mut(self.as_mut_ptr(),self.length)
65 }
66 }
67
68 pub const fn as_ptr(&self)->*const T
69 {
70 unsafe
71 {
72 self.buff.assume_init_ref().as_ptr()
73 }
74 }
75
76 pub const fn as_mut_ptr(&mut self)->*mut T
77 {
78 unsafe
79 {
80 self.buff.assume_init_mut().as_mut_ptr()
81 }
82 }
83
84 pub fn push(&mut self,v:T)
96 {
97 if self.length<N
98 {
99 unsafe
100 {
101 let vector=self.buff.assume_init_mut();
102 ptr::write(&raw mut vector[self.length],v);
104 }
105 self.length+=1;
106 }
107 }
108
109 pub fn pop(&mut self)->Option<T>
119 {
120 if self.length>0
121 {
122 self.length-=1;
123 unsafe
125 {
126 Some(ptr::read(self.as_ptr().add(self.length)))
127 }
128 }
129 else
130 {
131 None
132 }
133 }
134
135 pub fn insert(&mut self,index:usize,v:T)
147 {
148 if self.length<N && index<=self.length
149 {
150 unsafe
152 {
153 let p=self.as_mut_ptr().add(index);
154 ptr::copy(p,p.add(1),self.length-index);
155 ptr::write(p,v);
156 }
157 self.length+=1;
158 }
159 }
160
161 pub fn remove(&mut self,index:usize)->Option<T>
175 {
176 if self.length>index
177 {
178 unsafe
180 {
181 let p=self.as_mut_ptr().add(index);
182 let v=ptr::read(self.as_ptr().add(index));
183 ptr::copy(p.add(1),p,self.length-index-1);
184 self.length-=1;
185 Some(v)
186 }
187 }
188 else
189 {
190 None
191 }
192 }
193
194 pub fn truncate(&mut self,new_len:usize)
210 {
211 if new_len<self.length
212 {
213 for item in &mut self[new_len..]
215 {
216 drop(unsafe{ptr::read(item)});
217 }
218 self.length=new_len;
219 }
220 }
221
222 pub fn clear(&mut self)
234 {
235 for item in self.as_mut_slice()
236 {
237 drop(unsafe{ptr::read(item)});
239 }
240 self.length=0;
241 }
242
243 pub fn is_empty(&self)->bool
256 {
257 self.len()==0
258 }
259
260 pub const fn len(&self)->usize
271 {
272 self.length
273 }
274
275 pub const fn capacity(&self)->usize
284 {
285 N
286 }
287
288 pub const unsafe fn force_assign(&mut self,index:usize,value:T)
298 {
299 assert!(index<N);
300 unsafe
301 {
302 ptr::write(self.buff.assume_init_mut().as_mut_ptr().add(index),value);
303 }
304 }
305
306 pub const unsafe fn force_resize(&mut self,length:usize)
317 {
318 assert!(length<=N,"The new length exceeds capacity!");
319 self.length=length;
320 }
321}
322
323impl<const N:usize,T> Drop for StaticVec<N,T>
324{
325 fn drop(&mut self)
326 {
327 self.clear();
328 }
329}
330
331impl<const N:usize,T> Deref for StaticVec<N,T>
332{
333 type Target = [T];
334
335 fn deref(&self) -> &Self::Target
336 {
337 self.as_slice()
338 }
339}
340
341impl<const N:usize,T> DerefMut for StaticVec<N,T>
342{
343 fn deref_mut(&mut self) -> &mut Self::Target
344 {
345 self.as_mut_slice()
346 }
347}
348
349#[macro_export] macro_rules! vec_static
361{
362 ()=>
363 (
364 $crate::vec::StaticVec::new()
365 );
366 ($elem:expr;$len:expr)=>
367 (
368 {
369 let mut v=StaticVec::new();
370 unsafe
371 {
372 let mut i:usize=0;
373 while i<$len
374 {
375 v.force_assign(i,$elem);
376 i+=1;
377 }
378 v.force_resize($len);
379 }
380 v
381 }
382 );
383 ($($x:expr),+$(,)?)=>
384 (
385 {
386 let mut v=StaticVec::new();
387 let mut index:usize=0;
388 unsafe
389 {
390 $(
391 {
392 v.force_assign(index,$x);
393 index+=1;
394 }
395 )*
396 v.force_resize(index);
397 }
398 v
399 }
400 );
401}
402
403#[cfg(test)] mod test
404{
405 extern crate std;
406
407 use std::println;
408 use crate::vec::StaticVec;
409
410 #[should_panic]
411 #[test] fn vec_macro_overflow()
412 {
413 let x:StaticVec<12,u64>=vec_static![1234;16];
414 println!("{x:?}")
415 }
416
417 #[test] fn clone()
418 {
419 let x:StaticVec<16,u64>=vec_static![1,2,3,4,5,6,7,8,9,10];
420 assert_eq!(x.len(),10);
421 let y=x.clone();
422 assert_eq!(*y,[1,2,3,4,5,6,7,8,9,10]);
423 }
424
425 #[test] fn drop()
426 {
427 use core::sync::atomic::{AtomicUsize,Ordering};
428
429 struct DropCounter<'a>
430 {
431 counter:&'a AtomicUsize
432 }
433
434 impl<'a> Drop for DropCounter<'a>
435 {
436 fn drop(&mut self)
437 {
438 self.counter.fetch_add(1,Ordering::SeqCst);
439 }
440 }
441
442 let drop_count:AtomicUsize=AtomicUsize::new(0);
443 {
444 let mut v:StaticVec<8,DropCounter>=StaticVec::new();
445 v.push(DropCounter{counter:&drop_count});
446 v.push(DropCounter{counter:&drop_count});
447 assert_eq!(drop_count.load(Ordering::SeqCst),0);
448 v.pop();
449 assert_eq!(drop_count.load(Ordering::SeqCst),1);
450 v.clear();
451 assert_eq!(drop_count.load(Ordering::SeqCst),2);
452 v.push(DropCounter{counter:&drop_count});
453 v.push(DropCounter{counter:&drop_count});
454 assert_eq!(drop_count.load(Ordering::SeqCst),2);
455 v.push(DropCounter{counter:&drop_count});
456 v.push(DropCounter{counter:&drop_count});
457 v.push(DropCounter{counter:&drop_count});
458 assert_eq!(drop_count.load(Ordering::SeqCst),2);
459 assert_eq!(v.len(),5);
460 v.truncate(3);
461 assert_eq!(drop_count.load(Ordering::SeqCst),4);
462 }
463 assert_eq!(drop_count.load(Ordering::SeqCst),7);
464 }
465}