From f213089fd50ed97dd3281f5c8b2a466cce6981c5 Mon Sep 17 00:00:00 2001 From: Matthijs Brobbel Date: Mon, 11 Dec 2023 15:20:30 +0100 Subject: [PATCH] impl `From>` for `ScalarBuffer` --- arrow-buffer/src/buffer/scalar.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/arrow-buffer/src/buffer/scalar.rs b/arrow-buffer/src/buffer/scalar.rs index 3b75d5384046..be76528010cb 100644 --- a/arrow-buffer/src/buffer/scalar.rs +++ b/arrow-buffer/src/buffer/scalar.rs @@ -18,7 +18,7 @@ use crate::alloc::Deallocation; use crate::buffer::Buffer; use crate::native::ArrowNativeType; -use crate::MutableBuffer; +use crate::{BufferBuilder, MutableBuffer}; use std::fmt::Formatter; use std::marker::PhantomData; use std::ops::Deref; @@ -154,6 +154,13 @@ impl From> for ScalarBuffer { } } +impl From> for ScalarBuffer { + fn from(mut value: BufferBuilder) -> Self { + let len = value.len(); + Self::new(value.finish(), 0, len) + } +} + impl FromIterator for ScalarBuffer { fn from_iter>(iter: I) -> Self { iter.into_iter().collect::>().into() @@ -263,4 +270,12 @@ mod tests { let buffer = Buffer::from_iter([0_i32, 1, 2]); ScalarBuffer::::new(buffer, 0, usize::MAX / 4 + 1); } + + #[test] + fn convert_from_buffer_builder() { + let input = vec![1, 2, 3, 4]; + let buffer_builder = BufferBuilder::from(input.clone()); + let scalar_buffer = ScalarBuffer::from(buffer_builder); + assert_eq!(scalar_buffer.as_ref(), input); + } }