Skip to content

Commit

Permalink
feat: implement iterator for history (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
cernicc authored Feb 27, 2024
1 parent 6ee36e1 commit 1b9c099
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/helpers/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ impl<T: ?Sized, V: Clone> WithHistory<T, V> {
pub fn get(&self, index: usize) -> Option<V> {
Buffered::get(self, index)
}

/// Iterate over historical values, starting from the oldest value
pub fn iter(&self) -> impl Iterator<Item = &V> {
self.history.iter()
}
}

impl<T: ?Sized, V: Clone> Buffered<V> for WithHistory<T, V> {
Expand Down Expand Up @@ -57,6 +62,24 @@ where
}
}

impl<'a, T, V> IntoIterator for &'a WithHistory<T, V> {
type Item = &'a V;
type IntoIter = std::slice::Iter<'a, V>;

fn into_iter(self) -> Self::IntoIter {
self.history.iter()
}
}

impl<T, V> IntoIterator for WithHistory<T, V> {
type Item = V;
type IntoIter = std::vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
self.history.into_iter()
}
}

/// Wrapper for keeping last produced value
#[derive(Debug, Clone)]
pub struct WithLastValue<T: ?Sized, V> {
Expand Down

0 comments on commit 1b9c099

Please sign in to comment.