tokio_stream/wrappers/
interval.rs1use crate::Stream;
2use futures_core::stream::FusedStream;
3use std::pin::Pin;
4use std::task::{Context, Poll};
5use tokio::time::{Instant, Interval};
6
7#[derive(Debug)]
32#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
33pub struct IntervalStream {
34 inner: Interval,
35}
36
37impl IntervalStream {
38 pub fn new(interval: Interval) -> Self {
40 Self { inner: interval }
41 }
42
43 pub fn into_inner(self) -> Interval {
45 self.inner
46 }
47}
48
49impl Stream for IntervalStream {
50 type Item = Instant;
51
52 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Instant>> {
53 self.inner.poll_tick(cx).map(Some)
54 }
55
56 fn size_hint(&self) -> (usize, Option<usize>) {
57 (usize::MAX, None)
58 }
59}
60
61impl FusedStream for IntervalStream {
62 fn is_terminated(&self) -> bool {
63 false
64 }
65}
66
67impl AsRef<Interval> for IntervalStream {
68 fn as_ref(&self) -> &Interval {
69 &self.inner
70 }
71}
72
73impl AsMut<Interval> for IntervalStream {
74 fn as_mut(&mut self) -> &mut Interval {
75 &mut self.inner
76 }
77}