File tree Expand file tree Collapse file tree 4 files changed +24
-3
lines changed Expand file tree Collapse file tree 4 files changed +24
-3
lines changed Original file line number Diff line number Diff line change 125
125
#![ feature( alloc_layout_extra) ]
126
126
#![ feature( try_trait) ]
127
127
#![ feature( associated_type_bounds) ]
128
+ #![ feature( extend_with_capacity) ]
128
129
129
130
// Allow testing this library
130
131
Original file line number Diff line number Diff line change @@ -2048,6 +2048,11 @@ impl<T> Extend<T> for Vec<T> {
2048
2048
fn extend < I : IntoIterator < Item = T > > ( & mut self , iter : I ) {
2049
2049
<Self as SpecExtend < T , I :: IntoIter > >:: spec_extend ( self , iter. into_iter ( ) )
2050
2050
}
2051
+
2052
+ #[ inline]
2053
+ fn with_capacity ( capacity : usize ) -> Self {
2054
+ Self :: with_capacity ( capacity)
2055
+ }
2051
2056
}
2052
2057
2053
2058
// Specialization trait used for Vec::from_iter and Vec::extend
@@ -2097,7 +2102,7 @@ where
2097
2102
vector
2098
2103
}
2099
2104
2100
- default fn spec_extend ( & mut self , iterator : I ) {
2105
+ default fn spec_extend ( & mut self , mut iterator : I ) {
2101
2106
// This is the case for a TrustedLen iterator.
2102
2107
let ( low, high) = iterator. size_hint ( ) ;
2103
2108
if let Some ( high_value) = high {
@@ -2109,6 +2114,14 @@ where
2109
2114
) ;
2110
2115
}
2111
2116
if let Some ( additional) = high {
2117
+ if additional == 1 {
2118
+ // work around inefficiencies in generic vec.extend(Some(x))
2119
+ if let Some ( x) = iterator. next ( ) {
2120
+ self . push ( x) ;
2121
+ }
2122
+ return ;
2123
+ }
2124
+
2112
2125
self . reserve ( additional) ;
2113
2126
unsafe {
2114
2127
let mut ptr = self . as_mut_ptr ( ) . add ( self . len ( ) ) ;
Original file line number Diff line number Diff line change @@ -341,6 +341,12 @@ pub trait Extend<A> {
341
341
/// ```
342
342
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
343
343
fn extend < T : IntoIterator < Item = A > > ( & mut self , iter : T ) ;
344
+
345
+ /// Creates this collection with a reserved capacity.
346
+ #[ unstable( feature = "extend_with_capacity" , issue = "none" ) ]
347
+ fn with_capacity ( _capacity : usize ) -> Self where Self : Default {
348
+ Self :: default ( )
349
+ }
344
350
}
345
351
346
352
#[ stable( feature = "extend_for_unit" , since = "1.28.0" ) ]
Original file line number Diff line number Diff line change @@ -2672,8 +2672,9 @@ pub trait Iterator {
2672
2672
}
2673
2673
}
2674
2674
2675
- let mut ts: FromA = Default :: default ( ) ;
2676
- let mut us: FromB = Default :: default ( ) ;
2675
+ let cap = self . size_hint ( ) . 0 . saturating_add ( 1 ) ;
2676
+ let mut ts: FromA = Extend :: with_capacity ( cap) ;
2677
+ let mut us: FromB = Extend :: with_capacity ( cap) ;
2677
2678
2678
2679
self . for_each ( extend ( & mut ts, & mut us) ) ;
2679
2680
You can’t perform that action at this time.
0 commit comments