use async_repeater::{Repeater, RepeaterEntry};
use std::time::Duration;
use tokio_stream::StreamExt;
#[derive(Debug, Clone)]
pub struct Entry {
id: u8,
interval: Duration,
}
impl Entry {
pub fn new(id: u8, interval: Duration) -> Self {
Self { id, interval }
}
}
impl RepeaterEntry for Entry {
type Key = u8;
fn interval(&self) -> Duration {
self.interval
}
fn key(&self) -> Self::Key {
self.id
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
let mut repeater = Repeater::<Entry>::with_capacity(7);
let repetitions = tokio_stream::iter(vec![
Entry::new(0, Duration::from_millis(3000)),
Entry::new(1, Duration::from_millis(1000)),
Entry::new(2, Duration::from_millis(2000)),
]);
tokio::pin!(repetitions);
loop {
tokio::select! {
Some(item) = repeater.next() => {
println!("Popped: {item:?}");
repeater.insert(item);
}
Some(item) = repetitions.next() => {
repeater.insert(item);
}
_ = tokio::signal::ctrl_c() => {
break
}
}
}
}