@@ -1586,36 +1586,34 @@ impl<'a, T> IntoIterator for &'a mut Vec<T> {
1586
1586
impl < T > Extend < T > for Vec < T > {
1587
1587
#[ inline]
1588
1588
fn extend < I : IntoIterator < Item = T > > ( & mut self , iter : I ) {
1589
- self . extend_desugared ( iter. into_iter ( ) )
1589
+ self . spec_extend ( iter. into_iter ( ) )
1590
1590
}
1591
1591
}
1592
1592
1593
- trait IsTrustedLen : Iterator {
1594
- fn trusted_len ( & self ) -> Option < usize > { None }
1593
+ trait SpecExtend < I > {
1594
+ fn spec_extend ( & mut self , iter : I ) ;
1595
1595
}
1596
- impl < I > IsTrustedLen for I where I : Iterator { }
1597
1596
1598
- impl < I > IsTrustedLen for I where I : TrustedLen
1597
+ impl < I , T > SpecExtend < I > for Vec < T >
1598
+ where I : Iterator < Item =T > ,
1599
1599
{
1600
- fn trusted_len ( & self ) -> Option < usize > {
1601
- let ( low, high) = self . size_hint ( ) ;
1600
+ default fn spec_extend ( & mut self , iter : I ) {
1601
+ self . extend_desugared ( iter)
1602
+ }
1603
+ }
1604
+
1605
+ impl < I , T > SpecExtend < I > for Vec < T >
1606
+ where I : TrustedLen < Item =T > ,
1607
+ {
1608
+ fn spec_extend ( & mut self , iterator : I ) {
1609
+ // This is the case for a TrustedLen iterator.
1610
+ let ( low, high) = iterator. size_hint ( ) ;
1602
1611
if let Some ( high_value) = high {
1603
1612
debug_assert_eq ! ( low, high_value,
1604
1613
"TrustedLen iterator's size hint is not exact: {:?}" ,
1605
1614
( low, high) ) ;
1606
1615
}
1607
- high
1608
- }
1609
- }
1610
-
1611
- impl < T > Vec < T > {
1612
- fn extend_desugared < I : Iterator < Item = T > > ( & mut self , mut iterator : I ) {
1613
- // This function should be the moral equivalent of:
1614
- //
1615
- // for item in iterator {
1616
- // self.push(item);
1617
- // }
1618
- if let Some ( additional) = iterator. trusted_len ( ) {
1616
+ if let Some ( additional) = high {
1619
1617
self . reserve ( additional) ;
1620
1618
unsafe {
1621
1619
let mut ptr = self . as_mut_ptr ( ) . offset ( self . len ( ) as isize ) ;
@@ -1628,17 +1626,30 @@ impl<T> Vec<T> {
1628
1626
}
1629
1627
}
1630
1628
} else {
1631
- while let Some ( element) = iterator. next ( ) {
1632
- let len = self . len ( ) ;
1633
- if len == self . capacity ( ) {
1634
- let ( lower, _) = iterator. size_hint ( ) ;
1635
- self . reserve ( lower. saturating_add ( 1 ) ) ;
1636
- }
1637
- unsafe {
1638
- ptr:: write ( self . get_unchecked_mut ( len) , element) ;
1639
- // NB can't overflow since we would have had to alloc the address space
1640
- self . set_len ( len + 1 ) ;
1641
- }
1629
+ self . extend_desugared ( iterator)
1630
+ }
1631
+ }
1632
+ }
1633
+
1634
+ impl < T > Vec < T > {
1635
+ fn extend_desugared < I : Iterator < Item = T > > ( & mut self , mut iterator : I ) {
1636
+ // This is the case for a general iterator.
1637
+ //
1638
+ // This function should be the moral equivalent of:
1639
+ //
1640
+ // for item in iterator {
1641
+ // self.push(item);
1642
+ // }
1643
+ while let Some ( element) = iterator. next ( ) {
1644
+ let len = self . len ( ) ;
1645
+ if len == self . capacity ( ) {
1646
+ let ( lower, _) = iterator. size_hint ( ) ;
1647
+ self . reserve ( lower. saturating_add ( 1 ) ) ;
1648
+ }
1649
+ unsafe {
1650
+ ptr:: write ( self . get_unchecked_mut ( len) , element) ;
1651
+ // NB can't overflow since we would have had to alloc the address space
1652
+ self . set_len ( len + 1 ) ;
1642
1653
}
1643
1654
}
1644
1655
}
0 commit comments