@@ -39,7 +39,7 @@ use std::borrow::{Borrow, BorrowMut};
39
39
use std:: cmp;
40
40
use std:: fmt;
41
41
use std:: hash:: { Hash , Hasher } ;
42
- use std:: iter:: { IntoIterator , FromIterator } ;
42
+ use std:: iter:: { IntoIterator , FromIterator , repeat } ;
43
43
use std:: mem;
44
44
use std:: ops;
45
45
use std:: ptr;
@@ -731,6 +731,24 @@ impl<A: Array> SmallVec<A> where A::Item: Copy {
731
731
}
732
732
}
733
733
734
+ impl < A : Array > SmallVec < A > where A :: Item : Clone {
735
+ /// Resizes the vector so that its length is equal to `len`.
736
+ ///
737
+ /// If `len` is less than the current length, the vector simply truncated.
738
+ ///
739
+ /// If `len` is greater than the current length, `value` is appended to the
740
+ /// vector until its length equals `len`.
741
+ pub fn resize ( & mut self , len : usize , value : A :: Item ) {
742
+ let old_len = self . len ( ) ;
743
+
744
+ if len > old_len {
745
+ self . extend ( repeat ( value) . take ( len - old_len) ) ;
746
+ } else {
747
+ self . truncate ( len) ;
748
+ }
749
+ }
750
+ }
751
+
734
752
impl < A : Array > ops:: Deref for SmallVec < A > {
735
753
type Target = [ A :: Item ] ;
736
754
#[ inline]
@@ -1733,6 +1751,17 @@ mod tests {
1733
1751
assert_eq ! ( no_dupes. len( ) , 5 ) ;
1734
1752
}
1735
1753
1754
+ #[ test]
1755
+ fn test_resize ( ) {
1756
+ let mut v: SmallVec < [ i32 ; 8 ] > = SmallVec :: new ( ) ;
1757
+ v. push ( 1 ) ;
1758
+ v. resize ( 5 , 0 ) ;
1759
+ assert_eq ! ( v[ ..] , [ 1 , 0 , 0 , 0 , 0 ] [ ..] ) ;
1760
+
1761
+ v. resize ( 2 , -1 ) ;
1762
+ assert_eq ! ( v[ ..] , [ 1 , 0 ] [ ..] ) ;
1763
+ }
1764
+
1736
1765
#[ cfg( feature = "std" ) ]
1737
1766
#[ test]
1738
1767
fn test_write ( ) {
0 commit comments