@@ -294,6 +294,17 @@ pub mod details {
294
294
true
295
295
}
296
296
297
+ unsafe fn remove_impl ( & mut self , index : usize ) -> T {
298
+ self . verify_init ( "remove()" ) ;
299
+ debug_assert ! ( index < self . len( ) ) ;
300
+
301
+ let ptr = self . as_mut_ptr ( ) . add ( index) ;
302
+ let value = core:: ptr:: read ( ptr) ;
303
+ core:: ptr:: copy ( ptr. add ( 1 ) , ptr, self . len - index - 1 ) ;
304
+ self . len -= 1 ;
305
+ value
306
+ }
307
+
297
308
unsafe fn pop_impl ( & mut self ) -> Option < T > {
298
309
if self . is_empty ( ) {
299
310
return None ;
@@ -329,7 +340,7 @@ pub mod details {
329
340
}
330
341
331
342
impl < T > MetaVec < T , GenericOwningPointer > {
332
- /// Creates a new [`Queue `] with the provided capacity
343
+ /// Creates a new [`Vec `] with the provided capacity
333
344
pub fn new ( capacity : usize ) -> Self {
334
345
Self {
335
346
data_ptr : OwningPointer :: < MaybeUninit < T > > :: new_with_alloc ( capacity) ,
@@ -340,6 +351,16 @@ pub mod details {
340
351
}
341
352
}
342
353
354
+ /// Creates a new [`Vec`] with the provided capacity and fills it by
355
+ /// using the provided callback
356
+ pub fn from_fn < F : FnMut ( usize ) -> T > ( capacity : usize , mut callback : F ) -> Self {
357
+ let mut new_self = Self :: new ( capacity) ;
358
+ for n in 0 ..capacity {
359
+ new_self. push ( callback ( n) ) ;
360
+ }
361
+ new_self
362
+ }
363
+
343
364
/// Adds an element at the end of the vector. If the vector is full and the element cannot be
344
365
/// added it returns false, otherwise true.
345
366
pub fn push ( & mut self , value : T ) -> bool {
@@ -373,6 +394,11 @@ pub mod details {
373
394
unsafe { self . pop_impl ( ) }
374
395
}
375
396
397
+ /// Removes the element at the provided index and returns it.
398
+ pub fn remove ( & mut self , index : usize ) -> T {
399
+ unsafe { self . remove_impl ( index) }
400
+ }
401
+
376
402
/// Removes all elements from the vector
377
403
pub fn clear ( & mut self ) {
378
404
unsafe { self . clear_impl ( ) }
@@ -455,6 +481,16 @@ pub mod details {
455
481
self . pop_impl ( )
456
482
}
457
483
484
+ /// Removes the element at the provided index and returns it.
485
+ ///
486
+ /// # Safety
487
+ ///
488
+ /// * [`RelocatableVec::init()`] must be called once before
489
+ ///
490
+ pub unsafe fn remove ( & mut self , index : usize ) -> T {
491
+ unsafe { self . remove_impl ( index) }
492
+ }
493
+
458
494
/// Removes all elements from the vector
459
495
///
460
496
/// # Safety
@@ -681,6 +717,11 @@ impl<T, const CAPACITY: usize> FixedSizeVec<T, CAPACITY> {
681
717
unsafe { self . state . pop ( ) }
682
718
}
683
719
720
+ /// Removes the element at the provided index and returns it.
721
+ pub fn remove ( & mut self , index : usize ) -> T {
722
+ unsafe { self . state . remove ( index) }
723
+ }
724
+
684
725
/// Removes all elements from the vector
685
726
pub fn clear ( & mut self ) {
686
727
unsafe { self . state . clear ( ) }
0 commit comments