@@ -40,6 +40,10 @@ mod concat;
40
40
#[ allow( unreachable_pub) ] // https://github.com/rust-lang/rust/issues/57411
41
41
pub use self :: concat:: Concat ;
42
42
43
+ mod count;
44
+ #[ allow( unreachable_pub) ] // https://github.com/rust-lang/rust/issues/57411
45
+ pub use self :: count:: Count ;
46
+
43
47
mod cycle;
44
48
#[ allow( unreachable_pub) ] // https://github.com/rust-lang/rust/issues/57411
45
49
pub use self :: cycle:: Cycle ;
@@ -576,6 +580,38 @@ pub trait StreamExt: Stream {
576
580
assert_future :: < Self :: Item , _ > ( Concat :: new ( self ) )
577
581
}
578
582
583
+ /// Drives the stream to completion, counting the number of items.
584
+ ///
585
+ /// # Overflow Behavior
586
+ ///
587
+ /// The method does no guarding against overflows, so counting elements of a
588
+ /// stream with more than [`usize::MAX`] elements either produces the wrong
589
+ /// result or panics. If debug assertions are enabled, a panic is guaranteed.
590
+ ///
591
+ /// # Panics
592
+ ///
593
+ /// This function might panic if the iterator has more than [`usize::MAX`]
594
+ /// elements.
595
+ ///
596
+ /// # Examples
597
+ ///
598
+ /// ```
599
+ /// # futures::executor::block_on(async {
600
+ /// use futures::stream::{self, StreamExt};
601
+ ///
602
+ /// let stream = stream::iter(1..=10);
603
+ /// let count = stream.count().await;
604
+ ///
605
+ /// assert_eq!(count, 10);
606
+ /// # });
607
+ /// ```
608
+ fn count ( self ) -> Count < Self >
609
+ where
610
+ Self : Sized ,
611
+ {
612
+ assert_future :: < usize , _ > ( Count :: new ( self ) )
613
+ }
614
+
579
615
/// Repeats a stream endlessly.
580
616
///
581
617
/// The stream never terminates. Note that you likely want to avoid
0 commit comments