Skip to content

Commit

Permalink
feat: add fallible extend to mutable arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Aug 20, 2023
1 parent 7edf5f9 commit f1fc468
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/array/binary/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,18 @@ impl<O: Offset> MutableBinaryArray<O> {
let (offsets, values) = values_iter(iterator);
Self::try_new(Self::default_data_type(), offsets, values, None).unwrap()
}

/// Extend with a fallible iterator
pub fn extend_fallible<T, I, E>(&mut self, iter: I) -> std::result::Result<(), E>
where
E: std::error::Error,
I: IntoIterator<Item = std::result::Result<Option<T>, E>>,
T: AsRef<[u8]>,
{
let mut iter = iter.into_iter();
self.reserve(iter.size_hint().0, 0);
iter.try_for_each(|x| Ok(self.push(x?)))
}
}

impl<O: Offset, T: AsRef<[u8]>> Extend<Option<T>> for MutableBinaryArray<O> {
Expand Down
12 changes: 12 additions & 0 deletions src/array/binary/mutable_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,18 @@ impl<O: Offset> MutableBinaryValuesArray<O> {
}
Ok(array)
}

/// Extend with a fallible iterator
pub fn extend_fallible<T, I, E>(&mut self, iter: I) -> std::result::Result<(), E>
where
E: std::error::Error,
I: IntoIterator<Item = std::result::Result<T, E>>,
T: AsRef<[u8]>,
{
let mut iter = iter.into_iter();
self.reserve(iter.size_hint().0, 0);
iter.try_for_each(|x| Ok(self.push(x?)))
}
}

impl<O: Offset, T: AsRef<[u8]>> Extend<T> for MutableBinaryValuesArray<O> {
Expand Down
12 changes: 12 additions & 0 deletions src/array/utf8/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,18 @@ impl<O: Offset> MutableUtf8Array<O> {
pub fn from_iter_values<T: AsRef<str>, I: Iterator<Item = T>>(iterator: I) -> Self {
MutableUtf8ValuesArray::from_iter(iterator).into()
}

/// Extend with a fallible iterator
pub fn extend_fallible<T, I, E>(&mut self, iter: I) -> std::result::Result<(), E>
where
E: std::error::Error,
I: IntoIterator<Item = std::result::Result<Option<T>, E>>,
T: AsRef<str>,
{
let mut iter = iter.into_iter();
self.reserve(iter.size_hint().0, 0);
iter.try_for_each(|x| Ok(self.push(x?)))
}
}

impl<O: Offset, T: AsRef<str>> Extend<Option<T>> for MutableUtf8Array<O> {
Expand Down
12 changes: 12 additions & 0 deletions src/array/utf8/mutable_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,18 @@ impl<O: Offset> MutableUtf8ValuesArray<O> {
}
Ok(array)
}

/// Extend with a fallible iterator
pub fn extend_fallible<T, I, E>(&mut self, iter: I) -> std::result::Result<(), E>
where
E: std::error::Error,
I: IntoIterator<Item = std::result::Result<T, E>>,
T: AsRef<str>,
{
let mut iter = iter.into_iter();
self.reserve(iter.size_hint().0, 0);
iter.try_for_each(|x| Ok(self.push(x?)))
}
}

impl<O: Offset, T: AsRef<str>> Extend<T> for MutableUtf8ValuesArray<O> {
Expand Down

0 comments on commit f1fc468

Please sign in to comment.