@@ -17,6 +17,7 @@ use super::{Flatten, FlatMap, flatten_compat};
17
17
use super :: { Inspect , Map , Peekable , Scan , Skip , SkipWhile , StepBy , Take , TakeWhile , Rev } ;
18
18
use super :: { Zip , Sum , Product } ;
19
19
use super :: { ChainState , FromIterator , ZipImpl } ;
20
+ use super :: sources:: once;
20
21
21
22
fn _assert_is_object_safe ( _: & Iterator < Item =( ) > ) { }
22
23
@@ -2265,6 +2266,37 @@ pub trait Iterator {
2265
2266
Sum :: sum ( self )
2266
2267
}
2267
2268
2269
+ /// Iterates over the entire iterator, adding all the elements
2270
+ ///
2271
+ /// An empty iterator returns `None`, otherwise `Some(sum)`.
2272
+ ///
2273
+ /// # Panics
2274
+ ///
2275
+ /// When calling `sum_nonempty()` and a primitive integer type is being returned, this
2276
+ /// method will panic if the computation overflows and debug assertions are
2277
+ /// enabled.
2278
+ ///
2279
+ /// # Examples
2280
+ ///
2281
+ /// ```
2282
+ /// #![feature(nonempty_iter_arith)]
2283
+ /// let empty_sum = (1..1).sum_nonempty::<i32>();
2284
+ /// assert_eq!(empty_sum, None);
2285
+ ///
2286
+ /// let nonempty_sum = (1..=10).sum_nonempty::<i32>();
2287
+ /// assert_eq!(nonempty_sum, Some(55));
2288
+ /// ```
2289
+ #[ unstable( feature = "nonempty_iter_arith" ,
2290
+ reason = "recently added unstable API" ,
2291
+ issue = "0" ) ]
2292
+ fn sum_nonempty < S > ( mut self ) -> Option < S >
2293
+ where Self : Sized ,
2294
+ S : Sum < Self :: Item > ,
2295
+ {
2296
+ self . next ( )
2297
+ . map ( |first| once ( first) . chain ( self ) . sum ( ) )
2298
+ }
2299
+
2268
2300
/// Iterates over the entire iterator, multiplying all the elements
2269
2301
///
2270
2302
/// An empty iterator returns the one value of the type.
@@ -2293,6 +2325,36 @@ pub trait Iterator {
2293
2325
Product :: product ( self )
2294
2326
}
2295
2327
2328
+ /// Iterates over the entire iterator, multiplying all the elements
2329
+ ///
2330
+ /// An empty iterator returns `None`, otherwise `Some(product)`.
2331
+ ///
2332
+ /// # Panics
2333
+ ///
2334
+ /// When calling `product_nonempty()` and a primitive integer type is being returned,
2335
+ /// method will panic if the computation overflows and debug assertions are
2336
+ /// enabled.
2337
+ ///
2338
+ /// # Examples
2339
+ /// ```
2340
+ /// #![feature(nonempty_iter_arith)]
2341
+ /// let empty_product = (1..1).product_nonempty::<i32>();
2342
+ /// assert_eq!(empty_product, None);
2343
+ ///
2344
+ /// let nonempty_product = (1..=10).product_nonempty::<i32>();
2345
+ /// assert_eq!(nonempty_product, Some(3628800));
2346
+ /// ```
2347
+ #[ unstable( feature = "nonempty_iter_arith" ,
2348
+ reason = "recently added unstable API" ,
2349
+ issue = "0" ) ]
2350
+ fn product_nonempty < P > ( mut self ) -> Option < P >
2351
+ where Self : Sized ,
2352
+ P : Product < Self :: Item > ,
2353
+ {
2354
+ self . next ( )
2355
+ . map ( |first| once ( first) . chain ( self ) . product ( ) )
2356
+ }
2357
+
2296
2358
/// Lexicographically compares the elements of this `Iterator` with those
2297
2359
/// of another.
2298
2360
#[ stable( feature = "iter_order" , since = "1.5.0" ) ]
0 commit comments