File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -527,6 +527,39 @@ impl<A: Array> ArrayVec<A> {
527
527
self . len = Index :: from ( length) ;
528
528
}
529
529
530
+ /// Copy and appends all elements in a slice to the `ArrayVec`.
531
+ ///
532
+ /// ```
533
+ /// use arrayvec::ArrayVec;
534
+ ///
535
+ /// let mut vec: ArrayVec<[usize; 10]> = ArrayVec::new();
536
+ /// vec.push(1);
537
+ /// vec.extend_from_slice(&[2, 3]);
538
+ /// assert_eq!(&vec[..], &[1, 2, 3]);
539
+ /// ```
540
+ ///
541
+ /// # Panics
542
+ ///
543
+ /// This method will panic if the capacity left (see [`capacity_left`]) is
544
+ /// smaller then the length of the provided slice.
545
+ ///
546
+ /// [`capacity_left`]: #method.capacity_left
547
+ pub fn extend_from_slice ( & mut self , other : & [ A :: Item ] )
548
+ where A :: Item : Copy ,
549
+ {
550
+ if self . capacity_left ( ) < other. len ( ) {
551
+ panic ! ( "ArrayVec::extend_from_slice: slice is larger then capacity left" ) ;
552
+ }
553
+
554
+ let self_len = self . len ( ) ;
555
+ let other_len = other. len ( ) ;
556
+
557
+ unsafe {
558
+ let dst = self . xs . as_mut_ptr ( ) . offset ( self_len as isize ) ;
559
+ ptr:: copy_nonoverlapping ( other. as_ptr ( ) , dst, other_len) ;
560
+ self . set_len ( self_len + other_len) ;
561
+ }
562
+ }
530
563
531
564
/// Create a draining iterator that removes the specified range in the vector
532
565
/// and yields the removed items from start to end. The element range is
Original file line number Diff line number Diff line change @@ -41,6 +41,17 @@ fn test_capacity_left() {
41
41
assert_eq ! ( vec. capacity_left( ) , 0 ) ;
42
42
}
43
43
44
+ #[ test]
45
+ fn test_extend_from_slice ( ) {
46
+ let mut vec: ArrayVec < [ usize ; 10 ] > = ArrayVec :: new ( ) ;
47
+
48
+ vec. extend_from_slice ( & [ 1 , 2 , 3 ] ) ;
49
+ assert_eq ! ( vec. len( ) , 3 ) ;
50
+ assert_eq ! ( & vec[ ..] , & [ 1 , 2 , 3 ] ) ;
51
+ assert_eq ! ( vec. pop( ) , Some ( 3 ) ) ;
52
+ assert_eq ! ( & vec[ ..] , & [ 1 , 2 ] ) ;
53
+ }
54
+
44
55
#[ test]
45
56
fn test_u16_index ( ) {
46
57
const N : usize = 4096 ;
You can’t perform that action at this time.
0 commit comments