Skip to content

Commit

Permalink
impl FromIterator for MutableBuffer (#4624)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrobbel authored Aug 2, 2023
1 parent de5aa48 commit 7488925
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions arrow-buffer/src/buffer/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,14 @@ impl std::iter::FromIterator<bool> for MutableBuffer {
}
}

impl<T: ArrowNativeType> std::iter::FromIterator<T> for MutableBuffer {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let mut buffer = Self::default();
buffer.extend_from_iter(iter.into_iter());
buffer
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -985,4 +993,11 @@ mod tests {
let mut buffer = MutableBuffer::new(0);
buffer.set_null_bits(1, usize::MAX);
}

#[test]
fn from_iter() {
let buffer = [1u16, 2, 3, 4].into_iter().collect::<MutableBuffer>();
assert_eq!(buffer.len(), 4 * mem::size_of::<u16>());
assert_eq!(buffer.as_slice(), &[1, 0, 2, 0, 3, 0, 4, 0]);
}
}

0 comments on commit 7488925

Please sign in to comment.