@@ -130,6 +130,7 @@ cfg_unstable! {
130130 pub use flat_map:: FlatMap ;
131131 pub use timeout:: { TimeoutError , Timeout } ;
132132 pub use throttle:: Throttle ;
133+ pub use delay:: Delay ;
133134
134135 mod count;
135136 mod merge;
@@ -138,6 +139,7 @@ cfg_unstable! {
138139 mod partition;
139140 mod timeout;
140141 mod throttle;
142+ mod delay;
141143 mod unzip;
142144}
143145
@@ -564,6 +566,47 @@ extension_trait! {
564566 Enumerate :: new( self )
565567 }
566568
569+ #[ doc = r#"
570+ Creates a stream that is delayed before it starts yielding items.
571+
572+ # Examples
573+
574+ ```
575+ # fn main() { async_std::task::block_on(async {
576+ #
577+ use async_std::prelude::*;
578+ use async_std::stream;
579+ use std::time::{Duration, Instant};
580+
581+ let start = Instant::now();
582+ let mut s = stream::from_iter(vec![0u8, 1, 2]).delay(Duration::from_millis(200));
583+
584+ assert_eq!(s.next().await, Some(0));
585+ // The first time will take more than 200ms due to delay.
586+ assert!(start.elapsed().as_millis() >= 200);
587+
588+ assert_eq!(s.next().await, Some(1));
589+ // There will be no delay after the first time.
590+ assert!(start.elapsed().as_millis() <= 210);
591+
592+ assert_eq!(s.next().await, Some(2));
593+ assert!(start.elapsed().as_millis() <= 210);
594+
595+ assert_eq!(s.next().await, None);
596+ assert!(start.elapsed().as_millis() <= 210);
597+ #
598+ # }) }
599+ ```
600+ "# ]
601+ #[ cfg( any( feature = "unstable" , feature = "docs" ) ) ]
602+ #[ cfg_attr( feature = "docs" , doc( cfg( unstable) ) ) ]
603+ fn delay( self , dur: std:: time:: Duration ) -> Delay <Self >
604+ where
605+ Self : Sized ,
606+ {
607+ Delay :: new( self , dur)
608+ }
609+
567610 #[ doc = r#"
568611 Takes a closure and creates a stream that calls that closure on every element of this stream.
569612
0 commit comments