@@ -112,6 +112,7 @@ impl<A> Drop for RepeatN<A> {
112112
113113trait SpecRepeatN < A > {
114114 unsafe fn spec_next_unchecked ( & mut self ) -> A ;
115+ fn spec_fold < B , F : FnMut ( B , A ) -> B > ( self , b : B , f : F ) -> B ;
115116}
116117
117118impl < A : Clone > SpecRepeatN < A > for RepeatN < A > {
@@ -126,6 +127,18 @@ impl<A: Clone> SpecRepeatN<A> for RepeatN<A> {
126127 A :: clone ( & self . element )
127128 }
128129 }
130+
131+ default fn spec_fold < B , F : FnMut ( B , A ) -> B > ( mut self , mut b : B , mut f : F ) -> B {
132+ let mut count = self . count ;
133+ if let Some ( element) = self . take_element ( ) {
134+ while count > 1 {
135+ count -= 1 ;
136+ b = f ( b, element. clone ( ) ) ;
137+ }
138+ b = f ( b, element) ;
139+ }
140+ b
141+ }
129142}
130143
131144impl < A : Copy > SpecRepeatN < A > for RepeatN < A > {
@@ -136,6 +149,17 @@ impl<A: Copy> SpecRepeatN<A> for RepeatN<A> {
136149 // so skip having a branch that would need to be optimized out.
137150 * self . element
138151 }
152+
153+ fn spec_fold < B , F : FnMut ( B , A ) -> B > ( self , mut b : B , mut f : F ) -> B {
154+ let mut count = self . count ;
155+ let element = * self . element ;
156+
157+ while count > 0 {
158+ count -= 1 ;
159+ b = f ( b, element) ;
160+ }
161+ b
162+ }
139163}
140164
141165#[ unstable( feature = "iter_repeat_n" , issue = "104434" ) ]
@@ -175,6 +199,11 @@ impl<A: Clone> Iterator for RepeatN<A> {
175199 }
176200 }
177201
202+ #[ inline]
203+ fn fold < B , F : FnMut ( B , A ) -> B > ( self , init : B , f : F ) -> B {
204+ self . spec_fold ( init, f)
205+ }
206+
178207 #[ inline]
179208 fn last ( mut self ) -> Option < A > {
180209 self . take_element ( )
0 commit comments