From 55a2ed2a94a4a6f0a43d4a78e348ebec411dff41 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 24 Feb 2021 14:22:45 -0500 Subject: exhaustive: Add a non-consuming iter() --- src/exhaustive.rs | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/exhaustive.rs b/src/exhaustive.rs index 320994e..442850c 100644 --- a/src/exhaustive.rs +++ b/src/exhaustive.rs @@ -15,9 +15,9 @@ impl ExhaustiveSearch { Self(Vec::new()) } - /// Add a new item to the index. - pub fn push(&mut self, item: T) { - self.0.push(item); + /// Iterate over the items stored in this index. + pub fn iter(&self) -> Iter { + (&self).into_iter() } /// Get the size of this index. @@ -29,6 +29,11 @@ impl ExhaustiveSearch { pub fn is_empty(&self) -> bool { self.0.is_empty() } + + /// Add a new item to the index. + pub fn push(&mut self, item: T) { + self.0.push(item); + } } impl Default for ExhaustiveSearch { @@ -64,6 +69,35 @@ impl IntoIterator for ExhaustiveSearch { } } +/// An iterator over the values in an exhaustive index. +#[derive(Debug)] +pub struct Iter<'a, T>(std::slice::Iter<'a, T>); + +impl<'a, T> Iterator for Iter<'a, T> { + type Item = &'a T; + + fn next(&mut self) -> Option<&'a T> { + self.0.next() + } +} + +impl<'a, T> IntoIterator for &'a ExhaustiveSearch { + type Item = &'a T; + type IntoIter = Iter<'a, T>; + + fn into_iter(self) -> Self::IntoIter { + Iter(self.0.iter()) + } +} + +impl Extend for ExhaustiveSearch { + fn extend>(&mut self, iter: I) { + for value in iter { + self.push(value); + } + } +} + impl, V> NearestNeighbors for ExhaustiveSearch { fn search<'k, 'v, N>(&'v self, mut neighborhood: N) -> N where -- cgit v1.2.3