Skip to content

Commit e207ee8

Browse files
committed
chore: add docs for how to use Extend for generic methods on ArrayBuilders
1 parent 4a0bdde commit e207ee8

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

arrow-array/src/builder/mod.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@
134134
//! }
135135
//! }
136136
//!
137+
//! /// For building arrays in generic code, use Extend instead of the append_* methods
138+
//! /// e.g. append_value, append_option, append_null
137139
//! impl<'a> Extend<&'a MyRow> for MyRowBuilder {
138140
//! fn extend<T: IntoIterator<Item = &'a MyRow>>(&mut self, iter: T) {
139141
//! iter.into_iter().for_each(|row| self.append(row));
@@ -193,8 +195,8 @@ use std::any::Any;
193195
///
194196
/// ```
195197
/// // Create
196-
/// # use arrow_array::{ArrayRef, StringArray};
197-
/// # use arrow_array::builder::{ArrayBuilder, Float64Builder, Int64Builder, StringBuilder};
198+
/// # use arrow_array::{Array, ArrayRef, StringArray};
199+
/// # use arrow_array::builder::{ArrayBuilder, Float64Builder, Int64Builder, ListBuilder, StringBuilder};
198200
///
199201
/// let mut data_builders: Vec<Box<dyn ArrayBuilder>> = vec![
200202
/// Box::new(Float64Builder::new()),
@@ -234,6 +236,47 @@ use std::any::Any;
234236
/// .value(0),
235237
/// "🍎"
236238
/// );
239+
///
240+
/// // For generic methods that fill a list of values for an [`ArrayBuilder`], use the [`Extend`] trait.
241+
/// fn filter_and_fill<V, I: IntoIterator<Item = V>>(builder: &mut impl Extend<V>, values: I, filter: V)
242+
/// where V: PartialEq
243+
/// {
244+
/// builder.extend(values.into_iter().filter(|v| *v == filter));
245+
/// }
246+
/// let mut builder = StringBuilder::new();
247+
/// filter_and_fill(
248+
/// &mut builder,
249+
/// vec![Some("🍐"), Some("🍎"), None],
250+
/// Some("🍎"),
251+
/// );
252+
/// assert_eq!(builder.finish().len(), 1);
253+
///
254+
/// // For generic methods that fill lists-of-lists for an [`ArrayBuilder`], use the [`Extend`] trait.
255+
/// fn filter_and_fill_if_contains<T, V, I: IntoIterator<Item = Option<V>>>(
256+
/// list_builder: &mut impl Extend<Option<V>>,
257+
/// values: I,
258+
/// filter: Option<T>,
259+
/// ) where
260+
/// T: PartialEq,
261+
/// for<'a> &'a V: IntoIterator<Item = &'a Option<T>>,
262+
/// {
263+
/// list_builder.extend(values.into_iter().filter(|string: &Option<V>| {
264+
/// string
265+
/// .as_ref()
266+
/// .map(|str: &V| str.into_iter().any(|ch: &Option<T>| ch == &filter))
267+
/// .unwrap_or(false)
268+
/// }));
269+
/// }
270+
/// let builder = StringBuilder::new();
271+
/// let mut list_builder = ListBuilder::new(builder);
272+
/// let pear_pear = vec![Some("🍐"),Some("🍐")];
273+
/// let pear_app = vec![Some("🍐"),Some("🍎")];
274+
/// filter_and_fill_if_contains(
275+
/// &mut list_builder,
276+
/// vec![Some(pear_pear), Some(pear_app), None],
277+
/// Some("🍎"),
278+
/// );
279+
/// assert_eq!(list_builder.finish().len(), 1);
237280
/// ```
238281
pub trait ArrayBuilder: Any + Send + Sync {
239282
/// Returns the number of array slots in the builder

0 commit comments

Comments
 (0)