@@ -60,7 +60,7 @@ use core::cmp::{self, Ordering};
60
60
use core:: fmt;
61
61
use core:: hash:: { self , Hash } ;
62
62
use core:: intrinsics:: { arith_offset, assume} ;
63
- use core:: iter:: { FromIterator , FusedIterator , TrustedLen } ;
63
+ use core:: iter:: { FromIterator , FusedIterator , TrustedLen , OptimisticCollect } ;
64
64
use core:: marker:: PhantomData ;
65
65
use core:: mem;
66
66
use core:: ops:: Bound :: { Excluded , Included , Unbounded } ;
@@ -1813,8 +1813,8 @@ impl<T, I> SpecExtend<T, I> for Vec<T>
1813
1813
let mut vector = match iterator. next ( ) {
1814
1814
None => return Vec :: new ( ) ,
1815
1815
Some ( element) => {
1816
- let ( lower , _ ) = iterator . size_hint ( ) ;
1817
- let mut vector = Vec :: with_capacity ( lower . saturating_add ( 1 ) ) ;
1816
+ let mut vector = Vec :: with_capacity (
1817
+ iterator . optimistic_collect_count ( ) . saturating_add ( 1 ) ) ;
1818
1818
unsafe {
1819
1819
ptr:: write ( vector. get_unchecked_mut ( 0 ) , element) ;
1820
1820
vector. set_len ( 1 ) ;
@@ -1933,8 +1933,7 @@ impl<T> Vec<T> {
1933
1933
while let Some ( element) = iterator. next ( ) {
1934
1934
let len = self . len ( ) ;
1935
1935
if len == self . capacity ( ) {
1936
- let ( lower, _) = iterator. size_hint ( ) ;
1937
- self . reserve ( lower. saturating_add ( 1 ) ) ;
1936
+ self . reserve ( iterator. optimistic_collect_count ( ) . saturating_add ( 1 ) ) ;
1938
1937
}
1939
1938
unsafe {
1940
1939
ptr:: write ( self . get_unchecked_mut ( len) , element) ;
@@ -2589,9 +2588,9 @@ impl<'a, I: Iterator> Drop for Splice<'a, I> {
2589
2588
2590
2589
// There may be more elements. Use the lower bound as an estimate.
2591
2590
// FIXME: Is the upper bound a better guess? Or something else?
2592
- let ( lower_bound , _upper_bound ) = self . replace_with . size_hint ( ) ;
2593
- if lower_bound > 0 {
2594
- self . drain . move_tail ( lower_bound ) ;
2591
+ let optimistic_collect_count = self . replace_with . optimistic_collect_count ( ) ;
2592
+ if optimistic_collect_count > 0 {
2593
+ self . drain . move_tail ( optimistic_collect_count ) ;
2595
2594
if !self . drain . fill ( & mut self . replace_with ) {
2596
2595
return
2597
2596
}
0 commit comments