From aa68080fdc13d01ab72ec41b6f8da752261e29c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Cerni=C4=8D?= Date: Mon, 26 Feb 2024 21:37:20 +0100 Subject: [PATCH] feat: implement iterator for history --- src/helpers/history.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/helpers/history.rs b/src/helpers/history.rs index 64ab21a..0a2589f 100644 --- a/src/helpers/history.rs +++ b/src/helpers/history.rs @@ -24,6 +24,11 @@ impl WithHistory { pub fn get(&self, index: usize) -> Option { Buffered::get(self, index) } + + /// Iterate over historical values, starting from the oldest value + pub fn iter(&self) -> impl Iterator { + self.history.iter() + } } impl Buffered for WithHistory { @@ -57,6 +62,24 @@ where } } +impl<'a, T, V> IntoIterator for &'a WithHistory { + type Item = &'a V; + type IntoIter = std::slice::Iter<'a, V>; + + fn into_iter(self) -> Self::IntoIter { + self.history.iter() + } +} + +impl IntoIterator for WithHistory { + type Item = V; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.history.into_iter() + } +} + /// Wrapper for keeping last produced value #[derive(Debug, Clone)] pub struct WithLastValue {