@@ -40,6 +40,10 @@ mod concat;
4040#[ allow( unreachable_pub) ] // https://github.com/rust-lang/rust/issues/57411
4141pub use self :: concat:: Concat ;
4242
43+ mod count;
44+ #[ allow( unreachable_pub) ] // https://github.com/rust-lang/rust/issues/57411
45+ pub use self :: count:: Count ;
46+
4347mod cycle;
4448#[ allow( unreachable_pub) ] // https://github.com/rust-lang/rust/issues/57411
4549pub use self :: cycle:: Cycle ;
@@ -576,6 +580,38 @@ pub trait StreamExt: Stream {
576580 assert_future :: < Self :: Item , _ > ( Concat :: new ( self ) )
577581 }
578582
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+
579615 /// Repeats a stream endlessly.
580616 ///
581617 /// The stream never terminates. Note that you likely want to avoid
0 commit comments