twizzler/collections/vec/
vec_object.rs1use std::{mem::MaybeUninit, ops::RangeBounds};
2
3use twizzler_rt_abi::error::ArgumentError;
4
5use super::{Vec, VecObjectAlloc};
6use crate::{
7 alloc::{Allocator, SingleObjectAllocator},
8 marker::{Invariant, StoreCopy},
9 object::{Object, ObjectBuilder, TypedObject},
10 ptr::{Ref, RefMut, RefSlice},
11 Result,
12};
13
14pub struct VecObject<T: Invariant, A: Allocator> {
15 obj: Object<Vec<T, A>>,
16}
17
18impl<T: Invariant, A: Allocator> Clone for VecObject<T, A> {
19 fn clone(&self) -> Self {
20 Self {
21 obj: self.obj.clone(),
22 }
23 }
24}
25
26impl<T: Invariant, A: Allocator> From<Object<Vec<T, A>>> for VecObject<T, A> {
27 fn from(value: Object<Vec<T, A>>) -> Self {
28 Self { obj: value }
29 }
30}
31
32impl<T: Invariant, A: Allocator> VecObject<T, A> {
33 pub fn object(&self) -> &Object<Vec<T, A>> {
34 &self.obj
35 }
36
37 pub fn into_object(self) -> Object<Vec<T, A>> {
38 self.obj
39 }
40
41 pub fn iter(&self) -> VecIter<'_, T> {
42 if self.len() == 0 {
43 return VecIter {
44 pos: 0,
45 data: core::ptr::null(),
46 len: 0,
47 _ref: None,
48 };
49 }
50 let base = self.object().base();
51 let data = unsafe { base.inner.start.resolve().owned() };
52 VecIter {
53 pos: 0,
54 data: data.raw(),
55 len: self.len(),
56 _ref: Some(data),
57 }
58 }
59
60 pub fn is_empty(&self) -> bool {
61 self.len() == 0
62 }
63
64 pub fn len(&self) -> usize {
65 self.obj.base().len()
66 }
67
68 pub fn capacity(&self) -> usize {
69 self.obj.base().capacity()
70 }
71
72 pub fn reserve(&mut self, additional: usize) -> Result<()> {
73 self.obj.with_tx(|tx| {
74 let mut base = tx.base_mut();
75 base.reserve(additional)
76 })
77 }
78
79 pub fn shrink_to_fit(&mut self) -> Result<()> {
80 self.obj.with_tx(|tx| tx.base_mut().shrink_to_fit())
81 }
82
83 pub fn truncate(&mut self, len: usize) -> Result<()> {
84 self.obj.with_tx(|tx| tx.base_mut().truncate(len))
85 }
86
87 pub fn as_slice(&self) -> RefSlice<'_, T> {
88 self.obj.base().as_slice()
89 }
90
91 pub fn slice(&self, range: impl RangeBounds<usize>) -> RefSlice<'_, T> {
92 self.obj.base().as_slice().slice(range)
93 }
94
95 pub fn with_mut_slice<R>(
96 &mut self,
97 range: impl RangeBounds<usize>,
98 f: impl FnOnce(&mut [T]) -> Result<R>,
99 ) -> Result<R> {
100 self.obj
101 .with_tx(|tx| tx.base_mut().with_mut_slice(range, f))
102 }
103
104 #[inline]
105 pub fn get_ref(&self, idx: usize) -> Option<Ref<'_, T>> {
106 self.object().base().get_ref(idx)
107 }
108
109 pub fn swap(&mut self, a: usize, b: usize) -> Result<()> {
122 if a >= self.len() || b >= self.len() {
123 return Err(ArgumentError::InvalidArgument.into());
124 }
125 self.obj.with_tx(|tx| Ok(tx.base_mut().swap(a, b)))
126 }
127
128 pub fn clear(&mut self) -> Result<()> {
129 self.obj.with_tx(|tx| tx.base_mut().clear())
130 }
131
132 pub fn retain<F>(&mut self, f: F) -> Result<()>
133 where
134 F: FnMut(&T) -> bool,
135 T: StoreCopy,
136 {
137 self.obj.with_tx(|tx| tx.base_mut().retain(f))
138 }
139
140 pub fn first_ref(&self) -> Option<Ref<'_, T>> {
182 if self.is_empty() {
183 None
184 } else {
185 self.get_ref(0)
186 }
187 }
188
189 pub fn last_ref(&self) -> Option<Ref<'_, T>> {
190 if self.is_empty() {
191 None
192 } else {
193 self.get_ref(self.len() - 1)
194 }
195 }
196
197 pub fn binary_search(&self, x: &T) -> core::result::Result<usize, usize>
198 where
199 T: Ord,
200 {
201 self.as_slice().as_slice().binary_search(x)
202 }
203
204 pub fn binary_search_by<F>(&self, f: F) -> core::result::Result<usize, usize>
205 where
206 F: FnMut(&T) -> core::cmp::Ordering,
207 {
208 self.as_slice().as_slice().binary_search_by(f)
209 }
210
211 pub fn binary_search_by_key<B, F>(&self, b: &B, f: F) -> core::result::Result<usize, usize>
212 where
213 F: FnMut(&T) -> B,
214 B: Ord,
215 {
216 self.as_slice().as_slice().binary_search_by_key(b, f)
217 }
218
219 pub fn sort(&mut self) -> Result<()>
220 where
221 T: Ord,
222 {
223 self.with_mut_slice(.., |slice| {
224 slice.sort();
225 Ok(())
226 })
227 }
228
229 pub fn sort_by<F>(&mut self, compare: F) -> Result<()>
230 where
231 F: FnMut(&T, &T) -> core::cmp::Ordering,
232 {
233 self.with_mut_slice(.., |slice| {
234 slice.sort_by(compare);
235 Ok(())
236 })
237 }
238
239 pub fn sort_by_key<K, F>(&mut self, f: F) -> Result<()>
240 where
241 F: FnMut(&T) -> K,
242 K: Ord,
243 {
244 self.with_mut_slice(.., |slice| {
245 slice.sort_by_key(f);
246 Ok(())
247 })
248 }
249
250 pub fn sort_unstable(&mut self) -> Result<()>
251 where
252 T: Ord,
253 {
254 self.with_mut_slice(.., |slice| {
255 slice.sort_unstable();
256 Ok(())
257 })
258 }
259
260 pub fn sort_unstable_by<F>(&mut self, compare: F) -> Result<()>
261 where
262 F: FnMut(&T, &T) -> core::cmp::Ordering,
263 {
264 self.with_mut_slice(.., |slice| {
265 slice.sort_unstable_by(compare);
266 Ok(())
267 })
268 }
269
270 pub fn sort_unstable_by_key<K, F>(&mut self, f: F) -> Result<()>
271 where
272 F: FnMut(&T) -> K,
273 K: Ord,
274 {
275 self.with_mut_slice(.., |slice| {
276 slice.sort_unstable_by_key(f);
277 Ok(())
278 })
279 }
280
281 pub fn reverse(&mut self) -> Result<()> {
282 self.with_mut_slice(.., |slice| {
283 slice.reverse();
284 Ok(())
285 })
286 }
287
288 pub fn starts_with(&self, needle: &[T]) -> bool
289 where
290 T: PartialEq,
291 {
292 self.as_slice().as_slice().starts_with(needle)
293 }
294
295 pub fn ends_with(&self, needle: &[T]) -> bool
296 where
297 T: PartialEq,
298 {
299 self.as_slice().as_slice().ends_with(needle)
300 }
301
302 pub fn contains(&self, x: &T) -> bool
303 where
304 T: PartialEq,
305 {
306 self.as_slice().as_slice().contains(x)
307 }
308
309 pub fn with_slice<R>(&self, f: impl FnOnce(&[T]) -> R) -> R {
310 f(self.as_slice().as_slice())
311 }
312
313 pub fn with_slice_mut<R>(&mut self, f: impl FnOnce(&mut [T]) -> Result<R>) -> Result<R> {
314 self.with_mut_slice(.., f)
315 }
316}
317
318impl<T: Invariant + StoreCopy, A: Allocator> VecObject<T, A> {
319 pub fn push(&mut self, val: T) -> Result<()> {
320 self.obj.with_tx(|tx| {
321 tx.base_mut().push(val)?;
322 Ok(())
323 })?;
324 Ok(())
325 }
326
327 pub fn append(&mut self, vals: impl IntoIterator<Item = T>) -> Result<()> {
328 self.obj.with_tx(|tx| {
329 for val in vals {
330 tx.base_mut().push(val)?;
331 }
332 Ok(())
333 })
334 }
335
336 pub fn pop(&mut self) -> Result<Option<T>> {
337 if self.is_empty() {
338 return Ok(None);
339 }
340 Ok(Some(self.remove(self.len() - 1)?))
341 }
342
343 pub fn remove(&mut self, idx: usize) -> Result<T> {
344 if idx >= self.len() {
345 return Err(ArgumentError::InvalidArgument.into());
346 }
347 self.obj.with_tx(|tx| tx.base_mut().remove(idx))
348 }
349
350 pub fn split_off(&mut self, _point: usize) -> Result<Self> {
351 todo!()
352 }
353
354 pub fn swap_remove(&mut self, _idx: usize) -> Result<T> {
355 todo!()
356 }
357}
358
359impl<T: Invariant> VecObject<T, VecObjectAlloc> {
360 pub fn new(builder: ObjectBuilder<Vec<T, VecObjectAlloc>>) -> Result<Self> {
361 Ok(Self {
362 obj: builder.build_inplace(|tx| tx.write(Vec::new_in(VecObjectAlloc)))?,
363 })
364 }
365}
366
367impl<T: Invariant, A: Allocator + SingleObjectAllocator> VecObject<T, A> {
368 pub fn push_inplace(&mut self, val: T) -> Result<()> {
369 self.obj.with_tx(|tx| tx.base_mut().push_inplace(val))
370 }
371
372 pub fn append_inplace(&mut self, vals: impl IntoIterator<Item = T>) -> Result<()> {
373 self.obj.with_tx(|tx| {
374 for val in vals {
375 tx.base_mut().push_inplace(val)?;
376 }
377 Ok(())
378 })
379 }
380
381 pub fn push_ctor<F>(&mut self, ctor: F) -> Result<()>
382 where
383 F: FnOnce(RefMut<MaybeUninit<T>>) -> Result<RefMut<T>>,
384 {
385 self.obj.with_tx(|tx| tx.base_mut().push_ctor(ctor))
386 }
387
388 pub fn remove_inplace(&mut self, idx: usize) -> Result<()> {
389 if idx >= self.len() {
390 return Err(ArgumentError::InvalidArgument.into());
391 }
392 self.obj.with_tx(|tx| tx.base_mut().remove_inplace(idx))
393 }
394
395 pub fn swap_remove_inplace(&mut self, _idx: usize) -> Result<()> {
396 todo!()
397 }
398}
399
400impl<T: Invariant + StoreCopy, A: Allocator> Extend<T> for VecObject<T, A> {
401 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
402 self.append(iter).unwrap();
403 }
404}
405
406pub struct VecIter<'a, T> {
407 pos: usize,
408 data: *const T,
409 len: usize,
410 _ref: Option<Ref<'a, T>>,
411}
412
413impl<'a, T> VecIter<'a, T> {
414 #[inline]
415 pub fn slice(&self) -> &'a [T] {
416 unsafe { core::slice::from_raw_parts(self.data, self.len) }
417 }
418}
419
420impl<'a, T: 'a> Iterator for VecIter<'a, T> {
421 type Item = &'a T;
422
423 fn next(&mut self) -> Option<Self::Item> {
424 let pos = self.pos;
425 self.pos += 1;
426 self.slice().get(pos)
427 }
428}
429
430impl<'a, T: Invariant, A: Allocator> IntoIterator for &'a VecObject<T, A> {
431 type Item = &'a T;
432
433 type IntoIter = VecIter<'a, T>;
434
435 fn into_iter(self) -> Self::IntoIter {
436 self.iter()
437 }
438}