Coverage Report

Created: 2026-08-31 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-stream-0.1.19/src/wrappers/interval.rs
Line
Count
Source
1
use crate::Stream;
2
use futures_core::stream::FusedStream;
3
use std::pin::Pin;
4
use std::task::{Context, Poll};
5
use tokio::time::{Instant, Interval};
6
7
/// A wrapper around [`Interval`] that implements [`Stream`].
8
///
9
/// # Example
10
///
11
/// ```
12
/// use tokio::time::{Duration, Instant, interval};
13
/// use tokio_stream::wrappers::IntervalStream;
14
/// use tokio_stream::StreamExt;
15
///
16
/// # #[tokio::main(flavor = "current_thread")]
17
/// # async fn main() {
18
/// let start = Instant::now();
19
/// let interval = interval(Duration::from_millis(10));
20
/// let mut stream = IntervalStream::new(interval);
21
/// for _ in 0..3 {
22
///     if let Some(instant) = stream.next().await {
23
///         println!("elapsed: {:.1?}", instant.duration_since(start));
24
///     }
25
/// }
26
/// # }
27
/// ```
28
///
29
/// [`Interval`]: struct@tokio::time::Interval
30
/// [`Stream`]: trait@crate::Stream
31
#[derive(Debug)]
32
#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
33
pub struct IntervalStream {
34
    inner: Interval,
35
}
36
37
impl IntervalStream {
38
    /// Create a new `IntervalStream`.
39
0
    pub fn new(interval: Interval) -> Self {
40
0
        Self { inner: interval }
41
0
    }
42
43
    /// Get back the inner `Interval`.
44
0
    pub fn into_inner(self) -> Interval {
45
0
        self.inner
46
0
    }
47
}
48
49
impl Stream for IntervalStream {
50
    type Item = Instant;
51
52
0
    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Instant>> {
53
0
        self.inner.poll_tick(cx).map(Some)
54
0
    }
55
56
0
    fn size_hint(&self) -> (usize, Option<usize>) {
57
0
        (usize::MAX, None)
58
0
    }
59
}
60
61
impl FusedStream for IntervalStream {
62
0
    fn is_terminated(&self) -> bool {
63
0
        false
64
0
    }
65
}
66
67
impl AsRef<Interval> for IntervalStream {
68
0
    fn as_ref(&self) -> &Interval {
69
0
        &self.inner
70
0
    }
71
}
72
73
impl AsMut<Interval> for IntervalStream {
74
0
    fn as_mut(&mut self) -> &mut Interval {
75
0
        &mut self.inner
76
0
    }
77
}