@@ -637,6 +637,45 @@ impl<T, const CAPACITY: usize> Vec<T, CAPACITY> {
637
637
Ok ( ( ) )
638
638
}
639
639
640
+ /// Moves elements of `other` vector at the end of the current vector. `other` will be empty.
641
+ ///
642
+ /// # Errors
643
+ ///
644
+ /// Returns [`CapacityError`] if adding elements from `other` would result in current vector exceeding its capacity.
645
+ ///
646
+ /// # Example
647
+ ///
648
+ /// ```rust
649
+ /// use static_vector::Vec;
650
+ ///
651
+ /// let mut vec = Vec::<i32, 10>::new();
652
+ /// vec.push(1).unwrap();
653
+ /// vec.push(2).unwrap();
654
+ /// vec.push(3).unwrap();
655
+ ///
656
+ /// let mut other = Vec::<i32, 20>::new();
657
+ /// other.push(4).unwrap();
658
+ /// other.push(5).unwrap();
659
+ /// other.push(6).unwrap();
660
+ ///
661
+ /// vec.append(&mut other).unwrap();
662
+ /// assert_eq!(vec.as_slice(), [1, 2, 3, 4, 5, 6]);
663
+ /// assert_eq!(other.as_slice(), []);
664
+ /// ```
665
+ #[ inline]
666
+ pub fn append < const OTHER_CAPACITY : usize > (
667
+ & mut self ,
668
+ other : & mut Vec < T , OTHER_CAPACITY > ,
669
+ ) -> Result < ( ) , CapacityError >
670
+ where
671
+ T : Clone ,
672
+ {
673
+ self . extend_from_slice ( other. as_slice ( ) ) ?;
674
+ other. clear ( ) ;
675
+
676
+ Ok ( ( ) )
677
+ }
678
+
640
679
/// Adds the given `value` to the end of the vector without checking bounds.
641
680
/// For internal and controlled use only.
642
681
fn push_unchecked ( & mut self , value : T ) {
@@ -1397,6 +1436,44 @@ mod tests {
1397
1436
assert_eq ! ( dst. as_slice( ) , [ 1 , 2 , 3 ] ) ;
1398
1437
}
1399
1438
1439
+ #[ test]
1440
+ fn append_with_enough_room ( ) {
1441
+ let mut vec = Vec :: < i32 , 5 > :: new ( ) ;
1442
+ vec. push ( 1 ) . unwrap ( ) ;
1443
+ vec. push ( 2 ) . unwrap ( ) ;
1444
+
1445
+ let mut other = Vec :: < i32 , 20 > :: new ( ) ;
1446
+ other. push ( 3 ) . unwrap ( ) ;
1447
+ other. push ( 4 ) . unwrap ( ) ;
1448
+
1449
+ let result = vec. append ( & mut other) ;
1450
+
1451
+ assert ! ( result. is_ok( ) ) ;
1452
+ assert_eq ! ( vec. len( ) , 4 ) ;
1453
+ assert_eq ! ( vec. as_slice( ) , [ 1 , 2 , 3 , 4 ] ) ;
1454
+ assert ! ( other. is_empty( ) ) ;
1455
+ assert_eq ! ( other. as_slice( ) , [ ] ) ;
1456
+ }
1457
+
1458
+ #[ test]
1459
+ fn append_with_not_enough_room ( ) {
1460
+ let mut vec = Vec :: < i32 , 2 > :: new ( ) ;
1461
+ vec. push ( 1 ) . unwrap ( ) ;
1462
+ vec. push ( 2 ) . unwrap ( ) ;
1463
+
1464
+ let mut other = Vec :: < i32 , 20 > :: new ( ) ;
1465
+ other. push ( 3 ) . unwrap ( ) ;
1466
+ other. push ( 4 ) . unwrap ( ) ;
1467
+
1468
+ let result = vec. append ( & mut other) ;
1469
+
1470
+ assert ! ( result. is_err( ) ) ;
1471
+ assert_eq ! ( vec. len( ) , 2 ) ;
1472
+ assert_eq ! ( vec. as_slice( ) , [ 1 , 2 ] ) ;
1473
+ assert_eq ! ( other. len( ) , 2 ) ;
1474
+ assert_eq ! ( other. as_slice( ) , [ 3 , 4 ] ) ;
1475
+ }
1476
+
1400
1477
#[ test]
1401
1478
fn clone ( ) {
1402
1479
let mut vec = Vec :: < i32 , 5 > :: new ( ) ;
0 commit comments