Skip to content

Commit 6fb59d0

Browse files
alambtustvold
andauthored
Improve GenericStringBuilder documentation (#6372)
* Improve GenericStringBuilder documentation * Update arrow-array/src/builder/generic_bytes_builder.rs Co-authored-by: Raphael Taylor-Davies <1781103+tustvold@users.noreply.github.com> --------- Co-authored-by: Raphael Taylor-Davies <1781103+tustvold@users.noreply.github.com>
1 parent 704f90b commit 6fb59d0

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

arrow-array/src/builder/generic_bytes_builder.rs

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ impl<T: ByteArrayType> GenericByteBuilder<T> {
6060
/// Creates a new [`GenericByteBuilder`] from buffers.
6161
///
6262
/// # 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`].
6566
pub unsafe fn new_from_buffer(
6667
offsets_buffer: MutableBuffer,
6768
value_buffer: MutableBuffer,
@@ -88,9 +89,19 @@ impl<T: ByteArrayType> GenericByteBuilder<T> {
8889

8990
/// Appends a value into the builder.
9091
///
92+
/// See the [GenericStringBuilder] documentation for examples of
93+
/// incrementally building string values with multiple `write!` calls.
94+
///
9195
/// # Panics
9296
///
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
94105
#[inline]
95106
pub fn append_value(&mut self, value: impl AsRef<T::Native>) {
96107
self.value_builder.append_slice(value.as_ref().as_ref());
@@ -99,6 +110,11 @@ impl<T: ByteArrayType> GenericByteBuilder<T> {
99110
}
100111

101112
/// 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.
102118
#[inline]
103119
pub fn append_option(&mut self, value: Option<impl AsRef<T::Native>>) {
104120
match value {
@@ -227,30 +243,51 @@ impl<T: ByteArrayType, V: AsRef<T::Native>> Extend<Option<V>> for GenericByteBui
227243
/// Values can be appended using [`GenericByteBuilder::append_value`], and nulls with
228244
/// [`GenericByteBuilder::append_null`].
229245
///
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`
232268
///
233-
/// # Example
234269
/// ```
235270
/// # use std::fmt::Write;
236271
/// # use arrow_array::builder::GenericStringBuilder;
237272
/// let mut builder = GenericStringBuilder::<i32>::new();
238273
///
239-
/// // Write data
274+
/// // Write data in multiple `write!` calls
240275
/// write!(builder, "foo").unwrap();
241276
/// 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.
244279
/// builder.append_value("baz");
245280
///
246-
/// // Write second value
281+
/// // Write second value with a single write call
247282
/// write!(builder, "v2").unwrap();
283+
/// // finish the value by calling append_value with an empty string
248284
/// builder.append_value("");
249285
///
250286
/// let array = builder.finish();
251287
/// assert_eq!(array.value(0), "foobarbaz");
252288
/// assert_eq!(array.value(1), "v2");
253289
/// ```
290+
///
254291
pub type GenericStringBuilder<O> = GenericByteBuilder<GenericStringType<O>>;
255292

256293
impl<O: OffsetSizeTrait> Write for GenericStringBuilder<O> {

0 commit comments

Comments
 (0)