Skip to content

Commit

Permalink
Add Iterator subtraits
Browse files Browse the repository at this point in the history
  • Loading branch information
workingjubilee committed Sep 28, 2024
1 parent d2241d7 commit 46d9e78
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pgrx-tests/src/tests/array_borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions pgrx/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<ArrayType>` with low-level accessors.
Expand Down

0 comments on commit 46d9e78

Please sign in to comment.