tobixdev commented on code in PR #8543:
URL: https://github.com/apache/arrow-rs/pull/8543#discussion_r2404425638
##########
arrow-array/src/array/boolean_array.rs:
##########
@@ -436,11 +436,78 @@ impl<'a> BooleanArray {
}
}
-impl<Ptr: std::borrow::Borrow<Option<bool>>> FromIterator<Ptr> for
BooleanArray {
+/// An optional boolean value
+///
+/// This struct is used as an adapter when creating `BooleanArray` from an
iterator.
+/// `FromIterator` for `BooleanArray` takes an iterator where the elements can
be `into`
+/// this struct. So once implementing `From` or `Into` trait for a type, an
iterator of
+/// the type can be collected to `BooleanArray`.
+///
+/// See also [NativeAdapter](crate::array::NativeAdapter).
+#[derive(Debug)]
+pub struct BooleanAdapter {
+ /// Corresponding Rust native type if available
+ pub native: Option<bool>,
+}
+
+impl From<bool> for BooleanAdapter {
+ fn from(value: bool) -> Self {
+ BooleanAdapter {
+ native: Some(value),
+ }
+ }
+}
+
+impl From<&bool> for BooleanAdapter {
+ fn from(value: &bool) -> Self {
+ BooleanAdapter {
+ native: Some(*value),
+ }
+ }
+}
+
+impl From<Option<bool>> for BooleanAdapter {
+ fn from(value: Option<bool>) -> Self {
+ BooleanAdapter { native: value }
+ }
+}
+
+impl From<&Option<bool>> for BooleanAdapter {
+ fn from(value: &Option<bool>) -> Self {
+ BooleanAdapter { native: *value }
+ }
+}
+
+impl<Ptr: Into<BooleanAdapter>> FromIterator<Ptr> for BooleanArray {
fn from_iter<I: IntoIterator<Item = Ptr>>(iter: I) -> Self {
+ let vec = iter.into_iter().collect::<Vec<_>>();
+ unsafe {
+ // SAFETY: Vec iterator is trusted len
+ BooleanArray::from_trusted_len_iter(vec)
+ }
+ }
+}
+
+impl BooleanArray {
+ /// Creates a [`BooleanArray`] from an iterator of trusted length.
+ ///
+ /// # Safety
+ ///
+ /// The iterator must be
[`TrustedLen`](https://doc.rust-lang.org/std/iter/trait.TrustedLen.html).
+ /// I.e. that `size_hint().1` correctly reports its length.
+ ///
+ /// # Panics
+ ///
+ /// Panics if the iterator does not report an upper bound on `size_hint()`.
+ #[inline]
+ pub unsafe fn from_trusted_len_iter<I, P>(iter: I) -> Self
+ where
+ P: Into<BooleanAdapter>,
+ I: IntoIterator<Item = P>,
+ {
let iter = iter.into_iter();
let (_, data_len) = iter.size_hint();
- let data_len = data_len.expect("Iterator must be sized"); // panic if
no upper bound.
+ let data_len = data_len.expect("Iterator must be sized");
Review Comment:
I've re-applied the commit as I cannot seem to reproduce this behavior on
the old benchmarks. If you have any doubts that this could lead to a
performance regression, we can investigate this further.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]