@@ -190,13 +190,11 @@ impl<T, const CAPACITY: usize> Vec<T, CAPACITY> {
190
190
#[ doc( alias( "add" , "append" , "insert" ) ) ]
191
191
pub fn push ( & mut self , value : T ) -> Result < ( ) , CapacityError > {
192
192
if self . is_full ( ) {
193
- return Err ( CapacityError ) ;
193
+ Err ( CapacityError )
194
+ } else {
195
+ self . push_unchecked ( value) ;
196
+ Ok ( ( ) )
194
197
}
195
-
196
- self . data [ self . length ] . write ( value) ;
197
- self . length += 1 ;
198
-
199
- Ok ( ( ) )
200
198
}
201
199
202
200
/// Removes all elements. Size will be zero.
@@ -272,14 +270,14 @@ impl<T, const CAPACITY: usize> Vec<T, CAPACITY> {
272
270
}
273
271
274
272
if new_length > self . length {
275
- for i in self . length .. new_length {
276
- self . data [ i ] . write ( T :: default ( ) ) ;
273
+ while self . length < new_length {
274
+ self . push_unchecked ( T :: default ( ) ) ;
277
275
}
278
276
} else {
279
277
self . drop_range ( new_length, self . length ) ;
278
+ self . length = new_length;
280
279
}
281
280
282
- self . length = new_length;
283
281
Ok ( ( ) )
284
282
}
285
283
@@ -632,15 +630,21 @@ impl<T, const CAPACITY: usize> Vec<T, CAPACITY> {
632
630
return Err ( CapacityError ) ;
633
631
}
634
632
635
- for ( index , value) in slice. iter ( ) . enumerate ( ) {
636
- self . data [ self . len ( ) + index ] . write ( value. clone ( ) ) ;
633
+ for value in slice {
634
+ self . push_unchecked ( value. clone ( ) ) ;
637
635
}
638
636
639
- self . length += slice. len ( ) ;
640
-
641
637
Ok ( ( ) )
642
638
}
643
639
640
+ /// Adds the given `value` to the end of the vector without checking bounds.
641
+ /// For internal and controlled use only.
642
+ fn push_unchecked ( & mut self , value : T ) {
643
+ debug_assert ! ( !self . is_full( ) , "cannot push to full vector" ) ;
644
+ self . data [ self . length ] . write ( value) ;
645
+ self . length += 1 ;
646
+ }
647
+
644
648
/// Drops all elements in given range. Needed when elements are considered to be going out of scope.
645
649
/// E.g.: when the vector is going out of scope, when methods such as [`Vec::clear()`] and [`Vec::set_len()`] are called.
646
650
fn drop_range ( & mut self , from : usize , to : usize ) {
@@ -661,6 +665,16 @@ impl<T, const CAPACITY: usize> Drop for Vec<T, CAPACITY> {
661
665
}
662
666
}
663
667
668
+ impl < T : Clone , const CAPACITY : usize > Clone for Vec < T , CAPACITY > {
669
+ fn clone ( & self ) -> Self {
670
+ let mut vec = Self :: new ( ) ;
671
+ for value in self {
672
+ vec. push_unchecked ( value. clone ( ) ) ;
673
+ }
674
+ vec
675
+ }
676
+ }
677
+
664
678
/// Immutable iterator over a [`Vec`].
665
679
///
666
680
/// Created by calling [`Vec::iter()`].
@@ -1133,6 +1147,17 @@ mod tests {
1133
1147
assert_eq ! ( dst. as_slice( ) , [ 1 , 2 , 3 ] ) ;
1134
1148
}
1135
1149
1150
+ #[ test]
1151
+ fn clone ( ) {
1152
+ let mut vec = Vec :: < i32 , 5 > :: new ( ) ;
1153
+ let elements = [ 1 , 2 , 3 ] ;
1154
+ vec. extend_from_slice ( & elements) . unwrap ( ) ;
1155
+
1156
+ let new = vec. clone ( ) ;
1157
+ assert_eq ! ( new. len( ) , 3 ) ;
1158
+ assert_eq ! ( new. as_slice( ) , elements) ;
1159
+ }
1160
+
1136
1161
#[ test]
1137
1162
fn construct_should_not_create_default_elements ( ) {
1138
1163
let _ = Vec :: < Struct , 10 > :: new ( ) ;
0 commit comments