@@ -60,8 +60,9 @@ impl<T: ByteArrayType> GenericByteBuilder<T> {
60
60
/// Creates a new [`GenericByteBuilder`] from buffers.
61
61
///
62
62
/// # Safety
63
- /// This doesn't verify buffer contents as it assumes the buffers are from existing and
64
- /// valid [`GenericByteArray`].
63
+ ///
64
+ /// This doesn't verify buffer contents as it assumes the buffers are from
65
+ /// existing and valid [`GenericByteArray`].
65
66
pub unsafe fn new_from_buffer (
66
67
offsets_buffer : MutableBuffer ,
67
68
value_buffer : MutableBuffer ,
@@ -88,9 +89,19 @@ impl<T: ByteArrayType> GenericByteBuilder<T> {
88
89
89
90
/// Appends a value into the builder.
90
91
///
92
+ /// See the [GenericStringBuilder] documentation for examples of
93
+ /// incrementally building string values with multiple `write!` calls.
94
+ ///
91
95
/// # Panics
92
96
///
93
- /// Panics if the resulting length of [`Self::values_slice`] would exceed `T::Offset::MAX`
97
+ /// Panics if the resulting length of [`Self::values_slice`] would exceed
98
+ /// `T::Offset::MAX` bytes.
99
+ ///
100
+ /// For example, this can happen with [`StringArray`] or [`BinaryArray`]
101
+ /// where the total length of all values exceeds 2GB
102
+ ///
103
+ /// [`StringArray`]: crate::StringArray
104
+ /// [`BinaryArray`]: crate::BinaryArray
94
105
#[ inline]
95
106
pub fn append_value ( & mut self , value : impl AsRef < T :: Native > ) {
96
107
self . value_builder . append_slice ( value. as_ref ( ) . as_ref ( ) ) ;
@@ -99,6 +110,11 @@ impl<T: ByteArrayType> GenericByteBuilder<T> {
99
110
}
100
111
101
112
/// Append an `Option` value into the builder.
113
+ ///
114
+ /// - A `None` value will append a null value.
115
+ /// - A `Some` value will append the value.
116
+ ///
117
+ /// See [`Self::append_value`] for more panic information.
102
118
#[ inline]
103
119
pub fn append_option ( & mut self , value : Option < impl AsRef < T :: Native > > ) {
104
120
match value {
@@ -227,30 +243,51 @@ impl<T: ByteArrayType, V: AsRef<T::Native>> Extend<Option<V>> for GenericByteBui
227
243
/// Values can be appended using [`GenericByteBuilder::append_value`], and nulls with
228
244
/// [`GenericByteBuilder::append_null`].
229
245
///
230
- /// Additionally, implements [`std::fmt::Write`] with any written data included in the next
231
- /// appended value. This allows use with [`std::fmt::Display`] without intermediate allocations
246
+ /// This builder also implements [`std::fmt::Write`] with any written data
247
+ /// included in the next appended value. This allows using [`std::fmt::Display`]
248
+ /// with standard Rust idioms like `write!` and `writeln!` to write data
249
+ /// directly to the builder without intermediate allocations.
250
+ ///
251
+ /// # Example writing strings with `append_value`
252
+ /// ```
253
+ /// # use arrow_array::builder::GenericStringBuilder;
254
+ /// let mut builder = GenericStringBuilder::<i32>::new();
255
+ ///
256
+ /// // Write one string value
257
+ /// builder.append_value("foobarbaz");
258
+ ///
259
+ /// // Write a second string
260
+ /// builder.append_value("v2");
261
+ ///
262
+ /// let array = builder.finish();
263
+ /// assert_eq!(array.value(0), "foobarbaz");
264
+ /// assert_eq!(array.value(1), "v2");
265
+ /// ```
266
+ ///
267
+ /// # Example incrementally writing strings with `std::fmt::Write`
232
268
///
233
- /// # Example
234
269
/// ```
235
270
/// # use std::fmt::Write;
236
271
/// # use arrow_array::builder::GenericStringBuilder;
237
272
/// let mut builder = GenericStringBuilder::<i32>::new();
238
273
///
239
- /// // Write data
274
+ /// // Write data in multiple `write!` calls
240
275
/// write!(builder, "foo").unwrap();
241
276
/// write!(builder, "bar").unwrap();
242
- ///
243
- /// // Finish value
277
+ /// // The next call to append_value finishes the current string
278
+ /// // including all previously written strings.
244
279
/// builder.append_value("baz");
245
280
///
246
- /// // Write second value
281
+ /// // Write second value with a single write call
247
282
/// write!(builder, "v2").unwrap();
283
+ /// // finish the value by calling append_value with an empty string
248
284
/// builder.append_value("");
249
285
///
250
286
/// let array = builder.finish();
251
287
/// assert_eq!(array.value(0), "foobarbaz");
252
288
/// assert_eq!(array.value(1), "v2");
253
289
/// ```
290
+ ///
254
291
pub type GenericStringBuilder < O > = GenericByteBuilder < GenericStringType < O > > ;
255
292
256
293
impl < O : OffsetSizeTrait > Write for GenericStringBuilder < O > {
0 commit comments