use std::time::{Duration, Instant}; /// Cast into an `Iterator` of `Instant`s (i.e. times) pub trait IntoInstantIter { /// The output `Iterator` type type IterType: Iterator; /// Cast into `Self::IterType` fn into_instant_iter(self) -> Self::IterType; } impl IntoInstantIter for Vec { type IterType = ::std::vec::IntoIter; fn into_instant_iter(self) -> Self::IterType { self.into_iter() } } impl IntoInstantIter for Vec { type IterType = DurationToInstantIter<::std::vec::IntoIter>; fn into_instant_iter(self) -> Self::IterType { DurationToInstantIter::new(self.into_iter()) } } impl IntoInstantIter for Instant { type IterType = ::std::vec::IntoIter; fn into_instant_iter(self) -> Self::IterType { vec![self].into_iter() } } impl IntoInstantIter for Duration { type IterType = ::std::vec::IntoIter; fn into_instant_iter(self) -> Self::IterType { vec![Instant::now() + self].into_iter() } } pub struct DurationToInstantIter> { prev_iter: I, now: Instant, } impl> DurationToInstantIter { fn new(prev_iter: I) -> Self { DurationToInstantIter { prev_iter, now: Instant::now(), } } } impl> Iterator for DurationToInstantIter { type Item = Instant; fn next(&mut self) -> Option { self.prev_iter.next().map(|duration| self.now + duration) } }