From c082d63ef2632ec08c658f09930cbab69c2bb2e5 Mon Sep 17 00:00:00 2001 From: Aria Shrimpton Date: Wed, 31 Jan 2024 22:59:04 +0000 Subject: library: require intoiterator --- src/crates/library/src/eager_sorted_vector.rs | 9 +++++ src/crates/library/src/eager_unique_vector.rs | 9 +++++ src/crates/library/src/profiler.rs | 52 ++++++++++++++++----------- src/crates/library/src/traits.rs | 4 +-- 4 files changed, 51 insertions(+), 23 deletions(-) diff --git a/src/crates/library/src/eager_sorted_vector.rs b/src/crates/library/src/eager_sorted_vector.rs index bf67105..20d270a 100644 --- a/src/crates/library/src/eager_sorted_vector.rs +++ b/src/crates/library/src/eager_sorted_vector.rs @@ -211,6 +211,15 @@ impl Default for EagerSortedVec { } } +impl IntoIterator for EagerSortedVec { + type Item = T; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.v.into_iter() + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/crates/library/src/eager_unique_vector.rs b/src/crates/library/src/eager_unique_vector.rs index 632762b..a8f5abf 100644 --- a/src/crates/library/src/eager_unique_vector.rs +++ b/src/crates/library/src/eager_unique_vector.rs @@ -218,6 +218,15 @@ impl Default for EagerUniqueVec { } } +impl IntoIterator for EagerUniqueVec { + type Item = T; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.v.into_iter() + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/crates/library/src/profiler.rs b/src/crates/library/src/profiler.rs index 585d745..2385553 100644 --- a/src/crates/library/src/profiler.rs +++ b/src/crates/library/src/profiler.rs @@ -9,7 +9,7 @@ use std::{ use crate::traits::{Container, Indexable, Mapping, Stack}; pub struct ProfilerWrapper { - inner: T, + inner: Option, max_n: usize, n_contains: usize, n_insert: usize, @@ -27,7 +27,7 @@ pub struct ProfilerWrapper { impl Default for ProfilerWrapper { fn default() -> Self { Self { - inner: T::default(), + inner: Some(T::default()), max_n: 0, n_contains: 0, n_insert: 0, @@ -46,47 +46,47 @@ impl Default for ProfilerWrapper { impl, E> ProfilerWrapper { fn add_n(&mut self) { - self.max_n = self.max_n.max(self.inner.len()); + self.max_n = self.max_n.max(self.inner.as_ref().unwrap().len()); } } impl, K, V> ProfilerWrapper { fn add_n_map(&mut self) { - self.max_n = self.max_n.max(self.inner.len()); + self.max_n = self.max_n.max(self.inner.as_ref().unwrap().len()); } } impl, E> Container for ProfilerWrapper { fn len(&self) -> usize { - self.inner.len() + self.inner.as_ref().unwrap().len() } fn contains(&mut self, x: &E) -> bool { self.add_n(); self.n_contains += 1; - self.inner.contains(x) + self.inner.as_mut().unwrap().contains(x) } fn is_empty(&self) -> bool { - self.inner.is_empty() + self.inner.as_ref().unwrap().is_empty() } fn insert(&mut self, elt: E) { self.add_n(); self.n_insert += 1; - self.inner.insert(elt) + self.inner.as_mut().unwrap().insert(elt) } fn clear(&mut self) { self.add_n(); self.n_clear += 1; - self.inner.clear() + self.inner.as_mut().unwrap().clear() } fn remove(&mut self, elt: E) -> Option { self.add_n(); self.n_remove += 1; - self.inner.remove(elt) + self.inner.as_mut().unwrap().remove(elt) } } @@ -96,19 +96,19 @@ impl + Container, E> Indexable fn first(&mut self) -> Option<&E> { self.add_n(); self.n_first += 1; - self.inner.first() + self.inner.as_mut().unwrap().first() } fn last(&mut self) -> Option<&E> { self.add_n(); self.n_last += 1; - self.inner.last() + self.inner.as_mut().unwrap().last() } fn nth(&mut self, n: usize) -> Option<&E> { self.add_n(); self.n_nth += 1; - self.inner.nth(n) + self.inner.as_mut().unwrap().nth(n) } } @@ -116,49 +116,49 @@ impl + Container, E> Stack for ProfilerWrappe fn push(&mut self, elt: E) { self.add_n(); self.n_push += 1; - self.inner.push(elt) + self.inner.as_mut().unwrap().push(elt) } fn pop(&mut self) -> Option { self.add_n(); self.n_pop += 1; - self.inner.pop() + self.inner.as_mut().unwrap().pop() } } impl, K, V> Mapping for ProfilerWrapper { fn len(&self) -> usize { - self.inner.len() + self.inner.as_ref().unwrap().len() } fn contains(&mut self, x: &K) -> bool { self.add_n_map(); self.n_contains += 1; - self.inner.contains(x) + self.inner.as_mut().unwrap().contains(x) } fn insert(&mut self, key: K, val: V) -> Option { self.add_n_map(); self.n_insert += 1; - self.inner.insert(key, val) + self.inner.as_mut().unwrap().insert(key, val) } fn get(&mut self, key: &K) -> Option<&V> { self.add_n_map(); self.n_get += 1; - self.inner.get(key) + self.inner.as_mut().unwrap().get(key) } fn remove(&mut self, key: &K) -> Option { self.add_n_map(); self.n_remove += 1; - self.inner.remove(key) + self.inner.as_mut().unwrap().remove(key) } fn clear(&mut self) { self.add_n_map(); self.n_clear += 1; - self.inner.clear() + self.inner.as_mut().unwrap().clear() } } @@ -188,3 +188,13 @@ impl Drop for ProfilerWrapper { writeln!(f, "{}", self.n_get).unwrap(); } } + +impl IntoIterator for ProfilerWrapper { + type Item = T::Item; + + type IntoIter = T::IntoIter; + + fn into_iter(mut self) -> Self::IntoIter { + self.inner.take().unwrap().into_iter() + } +} diff --git a/src/crates/library/src/traits.rs b/src/crates/library/src/traits.rs index bf4616a..528e643 100644 --- a/src/crates/library/src/traits.rs +++ b/src/crates/library/src/traits.rs @@ -1,5 +1,5 @@ //! Common traits for primrose container types -pub trait Container: Default { +pub trait Container: Default + IntoIterator { /// Get the current number of elements fn len(&self) -> usize; @@ -30,7 +30,7 @@ pub trait Indexable { fn nth(&mut self, n: usize) -> Option<&T>; } -pub trait Mapping: Default { +pub trait Mapping: Default + IntoIterator { /// Get the current number of elements fn len(&self) -> usize; -- cgit v1.2.3