Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(array): implement TrustedLen for ArrayIterator and ArrayImplIterator #2309

Merged
merged 3 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/common/src/array/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::iter::TrustedLen;

use super::Array;
use crate::array::ArrayImpl;
use crate::types::DatumRef;
Expand All @@ -27,6 +29,8 @@ impl<'a, A: Array> ArrayIterator<'a, A> {
}
}

unsafe impl<'a, A: Array> TrustedLen for ArrayIterator<'a, A> {}

impl<'a, A: Array> Iterator for ArrayIterator<'a, A> {
type Item = Option<A::RefItem<'a>>;

Expand All @@ -39,6 +43,11 @@ impl<'a, A: Array> Iterator for ArrayIterator<'a, A> {
Some(item)
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
let size = self.data.len() - self.pos;
(size, Some(size))
}
}

pub struct ArrayImplIterator<'a> {
Expand All @@ -52,6 +61,8 @@ impl<'a> ArrayImplIterator<'a> {
}
}

unsafe impl<'a> TrustedLen for ArrayImplIterator<'a> {}

impl<'a> Iterator for ArrayImplIterator<'a> {
type Item = DatumRef<'a>;

Expand All @@ -64,4 +75,52 @@ impl<'a> Iterator for ArrayImplIterator<'a> {
Some(item)
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
let size = self.data.len() - self.pos;
(size, Some(size))
}
}

#[cfg(test)]
mod tests {
use paste::paste;

use super::*;
use crate::array::ArrayBuilder;
use crate::for_all_variants;

macro_rules! test_trusted_len {
([], $( { $variant_name:ident, $suffix_name:ident, $array:ty, $builder:ty } ),*) => {
$(
paste! {
#[test]
fn [<test_trusted_len_for_ $suffix_name _array>]() {
use crate::array::$builder;
let mut builder = $builder::new(3).unwrap();
for _ in 0..3 {
builder.append_null().unwrap();
}
let array = builder.finish().unwrap();
let mut iter = array.iter();

assert_eq!(iter.size_hint(), (3, Some(3))); iter.next();
assert_eq!(iter.size_hint(), (2, Some(2))); iter.next();
assert_eq!(iter.size_hint(), (1, Some(1))); iter.next();
assert_eq!(iter.size_hint(), (0, Some(0)));

let array_impl = ArrayImpl::from(array);
let mut iter = array_impl.iter();

assert_eq!(iter.size_hint(), (3, Some(3))); iter.next();
assert_eq!(iter.size_hint(), (2, Some(2))); iter.next();
assert_eq!(iter.size_hint(), (1, Some(1))); iter.next();
assert_eq!(iter.size_hint(), (0, Some(0)));
}
}
)*
};
}

for_all_variants! {test_trusted_len}
}
1 change: 1 addition & 0 deletions src/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#![feature(fn_traits)]
#![feature(type_alias_impl_trait)]
#![feature(test)]
#![feature(trusted_len)]

#[macro_use]
pub mod error;
Expand Down