From 46d9e782f3575a667d9f3514d49805ff1874982a Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sat, 28 Sep 2024 16:11:49 -0700 Subject: [PATCH] Add Iterator subtraits --- pgrx-tests/src/tests/array_borrowed.rs | 2 +- pgrx/src/array.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pgrx-tests/src/tests/array_borrowed.rs b/pgrx-tests/src/tests/array_borrowed.rs index eb7b874bb..63e97aaca 100644 --- a/pgrx-tests/src/tests/array_borrowed.rs +++ b/pgrx-tests/src/tests/array_borrowed.rs @@ -22,7 +22,7 @@ fn borrow_sum_array_i32(values: &FlatArray<'_, i32>) -> i32 { // we implement it this way so we can trap an overflow (as we have a test for this) and // catch it correctly in both --debug and --release modes let mut sum = 0_i32; - for v in values.iter() { + for v in values { let v = v.into_option().copied().unwrap_or(0); let (val, overflow) = sum.overflowing_add(v); if overflow { diff --git a/pgrx/src/array.rs b/pgrx/src/array.rs index 68851f205..26e4444a3 100644 --- a/pgrx/src/array.rs +++ b/pgrx/src/array.rs @@ -20,6 +20,7 @@ use crate::toast::{Toast, Toasty}; use crate::{layout, pg_sys, varlena}; use bitvec::ptr::{self as bitptr, BitPtr, BitPtrError, Const, Mut}; use bitvec::slice::{self as bitslice, BitSlice}; +use core::iter::{ExactSizeIterator, FusedIterator}; use core::marker::PhantomData; use core::ptr::{self, NonNull}; use core::{ffi, mem, slice}; @@ -257,6 +258,20 @@ where } } +impl<'arr, 'mcx, T> IntoIterator for &'arr FlatArray<'mcx, T> +where + T: ?Sized + BorrowDatum, +{ + type IntoIter = ArrayIter<'arr, T>; + type Item = Nullable<&'arr T>; + fn into_iter(self) -> Self::IntoIter { + self.iter() + } +} + +impl<'arr, T> ExactSizeIterator for ArrayIter<'arr, T> where T: ?Sized + BorrowDatum {} +impl<'arr, T> FusedIterator for ArrayIter<'arr, T> where T: ?Sized + BorrowDatum {} + /** An aligned, dereferenceable `NonNull` with low-level accessors.