1use core::borrow::{Borrow, BorrowMut};
2use core::convert::{AsMut, AsRef, From};
3use core::fmt;
4use core::mem::MaybeUninit;
5use core::ops;
6use core::{ptr, slice};
7
8pub struct Vec<T, const N: usize> {
9 buf: MaybeUninit<[T; N]>,
10 len: usize,
11}
12impl<T, const N: usize> Vec<T, N> {
13 const CAPACITY: usize = N;
14 pub const fn new() -> Self {
15 Vec {
16 buf: MaybeUninit::uninit(),
17 len: 0,
18 }
19 }
20 pub fn capacity(&self) -> usize {
21 Self::CAPACITY
22 }
23 pub fn as_ptr(&self) -> *const T {
24 self.buf.as_ptr() as *const T
25 }
26 pub fn as_mut_ptr(&mut self) -> *mut T {
27 self.buf.as_mut_ptr() as *mut T
28 }
29 pub fn as_slice(&self) -> &[T] {
30 self
31 }
32 pub fn as_mut_slice(&mut self) -> &mut [T] {
33 self
34 }
35 pub fn clear(&mut self) {
36 self.truncate(0);
37 }
38 pub fn truncate(&mut self, len: usize) {
39 unsafe {
40 if len > self.len {
41 return;
42 }
43 let remove_len = self.len - len;
44 let s = ptr::slice_from_raw_parts_mut(self.as_mut_ptr().add(len), remove_len);
45 self.len = len;
46 ptr::drop_in_place(s);
47 }
48 }
49 pub unsafe fn set_len(&mut self, new_len: usize) {
50 debug_assert!(new_len <= self.capacity());
51
52 self.len = new_len;
53 }
54 pub fn pop(&mut self) -> Option<T> {
55 if self.len == 0 {
56 None
57 } else {
58 unsafe {
59 self.len -= 1;
60 Some(ptr::read(self.as_ptr().add(self.len())))
61 }
62 }
63 }
64 pub fn push(&mut self, value: T) -> Result<(), T> {
65 if self.len == self.capacity() {
66 return Err(value);
67 }
68 unsafe {
69 let end = self.as_mut_ptr().add(self.len);
70 ptr::write(end, value);
71 self.len += 1;
72 }
73 Ok(())
74 }
75 pub fn swap_remove(&mut self, index: usize) -> T {
76 let len = self.len();
77 if index >= len {
78 panic!("swap_remove index {} should < {}", index, len);
79 }
80 unsafe {
81 let last = ptr::read(self.as_ptr().add(len - 1));
82 let hole = self.as_mut_ptr().add(index);
83 self.set_len(len - 1);
84 ptr::replace(hole, last)
85 }
86 }
87}
88impl<T: Clone, const N: usize> Vec<T, N> {
89 pub fn extend_from_slice(&mut self, other: &[T]) {
90 assert!(self.len + other.len() <= self.capacity());
91
92 let buf = unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len + other.len()) };
93 for item in other {
94 buf[self.len] = item.clone();
95 self.len += 1;
96 }
97 }
98}
99impl<T, const N: usize> ops::Deref for Vec<T, N> {
100 type Target = [T];
101
102 fn deref(&self) -> &[T] {
103 unsafe { slice::from_raw_parts(self.as_ptr(), self.len) }
104 }
105}
106
107impl<T, const N: usize> ops::DerefMut for Vec<T, N> {
108 fn deref_mut(&mut self) -> &mut [T] {
109 unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
110 }
111}
112impl<T: Clone, const N: usize> From<&[T]> for Vec<T, N> {
113 fn from(slice: &[T]) -> Self {
114 let mut vec = Vec::new();
115 vec.extend_from_slice(slice);
116 vec
117 }
118}
119impl<T, const N: usize> AsRef<[T]> for Vec<T, N> {
120 fn as_ref(&self) -> &[T] {
121 self
122 }
123}
124impl<T, const N: usize> AsMut<[T]> for Vec<T, N> {
125 fn as_mut(&mut self) -> &mut [T] {
126 self
127 }
128}
129impl<T, const N: usize> Borrow<[T]> for Vec<T, N> {
130 fn borrow(&self) -> &[T] {
131 &self[..]
132 }
133}
134impl<T, const N: usize> BorrowMut<[T]> for Vec<T, N> {
135 fn borrow_mut(&mut self) -> &mut [T] {
136 &mut self[..]
137 }
138}
139impl<T: fmt::Debug, const N: usize> fmt::Debug for Vec<T, N> {
140 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141 fmt::Debug::fmt(&**self, f)
142 }
143}
144impl<T, const N: usize> Drop for Vec<T, N> {
145 fn drop(&mut self) {
146 self.clear();
147 }
148}