iceoryx2_bb_container/vector/
mod.rs1use alloc::format;
14use core::mem::MaybeUninit;
15use core::ops::{Deref, DerefMut};
16
17pub mod polymorphic_vec;
19pub mod relocatable_vec;
21pub mod static_vec;
23
24use iceoryx2_log::fail;
25pub use polymorphic_vec::*;
26pub use relocatable_vec::*;
27pub use static_vec::*;
28
29#[derive(Debug, PartialEq, Eq, Clone, Copy)]
31pub enum VectorModificationError {
32 OutOfBounds,
34 InsertWouldExceedCapacity,
37}
38
39impl core::fmt::Display for VectorModificationError {
40 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
41 write!(f, "VectorModificationError::{self:?}")
42 }
43}
44
45impl core::error::Error for VectorModificationError {}
46
47pub(crate) mod internal {
48 use super::*;
49
50 #[doc(hidden)]
51 pub trait VectorView<T> {
52 fn data(&self) -> &[MaybeUninit<T>];
53
54 unsafe fn data_mut(&mut self) -> &mut [MaybeUninit<T>];
59
60 unsafe fn set_len(&mut self, len: u64);
65 }
66}
67
68pub trait Vector<T>: Deref<Target = [T]> + DerefMut + internal::VectorView<T> {
70 fn as_mut_slice(&mut self) -> &mut [T] {
72 let len = self.len();
73 unsafe {
74 core::mem::transmute::<&mut [MaybeUninit<T>], &mut [T]>(&mut self.data_mut()[0..len])
75 }
76 }
77
78 fn as_slice(&self) -> &[T] {
80 let len = self.len();
81 unsafe { core::mem::transmute::<&[MaybeUninit<T>], &[T]>(&self.data()[0..len]) }
82 }
83
84 fn capacity(&self) -> usize;
86
87 fn clear(&mut self) {
89 let len = self.len();
90 let data = unsafe { self.data_mut() };
91 for idx in (0..len).rev() {
92 unsafe { data[idx].assume_init_drop() };
93 }
94
95 unsafe { self.set_len(0) };
96 }
97
98 fn extend_from_slice(&mut self, other: &[T]) -> Result<(), VectorModificationError>
100 where
101 T: Clone,
102 {
103 if self.capacity() < self.len() + other.len() {
104 let origin = format!(
105 "Vector::<{}>::extend_from_slice()",
106 core::any::type_name::<T>()
107 );
108 fail!(from origin, with VectorModificationError::InsertWouldExceedCapacity,
109 "Unable to extend vector from slice with length {} since it would exceed the vectors capacity of {}.",
110 other.len(), self.capacity());
111 }
112
113 let len = self.len();
114 let data = unsafe { self.data_mut() };
115 for (i, element) in other.iter().enumerate() {
116 data[i + len].write(element.clone());
117 }
118
119 unsafe { self.set_len(self.len() as u64 + other.len() as u64) };
120
121 Ok(())
122 }
123
124 unsafe fn extend_from_slice_unchecked(&mut self, other: &[T])
131 where
132 T: Clone,
133 {
134 let len = self.len();
135 let data = unsafe { self.data_mut() };
136 for (i, element) in other.iter().enumerate() {
137 data[i + len].write(element.clone());
138 }
139
140 unsafe { self.set_len(self.len() as u64 + other.len() as u64) };
141 }
142
143 fn insert(&mut self, index: usize, element: T) -> Result<(), VectorModificationError> {
146 if self.is_full() {
147 let origin = format!("Vector::<{}>::insert()", core::any::type_name::<T>());
148
149 fail!(from origin, with VectorModificationError::InsertWouldExceedCapacity,
150 "Failed to insert element into vector since it would exceed the vectors capacity of {}.",
151 self.capacity());
152 }
153
154 let len = self.len();
155 if index > len {
156 let origin = format!("Vector::<{}>::insert()", core::any::type_name::<T>());
157
158 fail!(from origin, with VectorModificationError::OutOfBounds,
159 "Failed to insert element into vector of length {} since the index {} is out-of-bounds.",
160 self.len(), index);
161 }
162
163 let data = unsafe { self.data_mut() };
164 if index != len {
165 let ptr = data.as_mut_ptr();
166 unsafe { core::ptr::copy(ptr.add(index), ptr.add(index + 1), len - index) };
167 }
168
169 data[index].write(element);
170 unsafe { self.set_len(len as u64 + 1) };
171
172 Ok(())
173 }
174
175 fn is_empty(&self) -> bool {
177 self.len() == 0
178 }
179
180 fn is_full(&self) -> bool {
182 self.len() == self.capacity()
183 }
184
185 fn len(&self) -> usize;
187
188 fn pop(&mut self) -> Option<T> {
191 if self.is_empty() {
192 return None;
193 }
194
195 let len = self.len();
196 let data = unsafe { self.data_mut() };
197 let value = core::mem::replace(&mut data[len - 1], MaybeUninit::uninit());
198 unsafe { self.set_len(len as u64 - 1) };
199 Some(unsafe { value.assume_init() })
200 }
201
202 fn push(&mut self, value: T) -> Result<(), VectorModificationError> {
205 if self.is_full() {
206 let origin = format!("Vector::<{}>::push()", core::any::type_name::<T>());
207
208 fail!(from origin, with VectorModificationError::InsertWouldExceedCapacity,
209 "Failed to push element into vector since it would exceed the vectors capacity of {}.",
210 self.capacity());
211 }
212
213 unsafe { self.push_unchecked(value) };
214 Ok(())
215 }
216
217 unsafe fn push_unchecked(&mut self, value: T) {
224 let len = self.len();
225 unsafe { self.data_mut()[len].write(value) };
226 unsafe { self.set_len(len as u64 + 1) };
227 }
228
229 fn remove(&mut self, index: usize) -> Option<T> {
231 let len = self.len();
232 if len <= index {
233 return None;
234 }
235
236 let data = unsafe { self.data_mut() };
237 let value = unsafe { core::ptr::read(data[index].as_ptr()) };
238
239 let ptr = data.as_mut_ptr();
240 unsafe { core::ptr::copy(ptr.add(index + 1), ptr.add(index), len - index - 1) };
241
242 unsafe { self.set_len(len as u64 - 1) };
243
244 Some(value)
245 }
246
247 fn resize(&mut self, new_len: usize, value: T) -> Result<(), VectorModificationError>
249 where
250 T: Clone,
251 {
252 self.resize_with(new_len, || value.clone())
253 }
254
255 fn resize_with<F: FnMut() -> T>(
257 &mut self,
258 new_len: usize,
259 mut f: F,
260 ) -> Result<(), VectorModificationError> {
261 let capacity = self.capacity();
262 if capacity < new_len {
263 let origin = format!("Vector::<{}>::resize_with()", core::any::type_name::<T>());
264
265 fail!(from origin, with VectorModificationError::InsertWouldExceedCapacity,
266 "Failed to resize vector to {} since it would exceed the vectors capacity of {}.",
267 new_len, self.capacity());
268 }
269
270 if new_len < self.len() {
271 self.truncate(new_len);
272 } else {
273 let len = self.len();
274 let data = unsafe { self.data_mut() };
275 for item in data.iter_mut().take(new_len).skip(len) {
276 item.write(f());
277 }
278
279 unsafe { self.set_len(new_len as u64) };
280 }
281
282 Ok(())
283 }
284
285 fn truncate(&mut self, new_len: usize) {
288 let len = self.len();
289 if len <= new_len {
290 return;
291 }
292
293 let data = unsafe { self.data_mut() };
294 for idx in (new_len..len).rev() {
295 unsafe { data[idx].assume_init_drop() };
296 }
297
298 unsafe { self.set_len(new_len as u64) };
299 }
300}