From 53859628d31bee9ebdacd4c05e70cf548cd2cd69 Mon Sep 17 00:00:00 2001 From: Matthijs Brobbel Date: Tue, 3 Sep 2024 22:48:02 +0200 Subject: [PATCH] feat: add `ArrayType` impl for `Box` (#238) --- examples/basic.rs | 6 ++-- src/logical/box.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++ src/logical/mod.rs | 3 ++ 3 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 src/logical/box.rs diff --git a/examples/basic.rs b/examples/basic.rs index 9ebc176..75e8553 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -19,6 +19,7 @@ fn main() { enum FooBar { Foo(Foo), Bar(Bar), + Box(Box), None, } @@ -45,13 +46,14 @@ fn main() { c: Some("hello world".to_owned()), }), FooBar::Bar(Bar(vec![1, 2, 3, 4])), + FooBar::Box(Box::new(1234)), FooBar::None, FooBar::None, ]; let union_array = foo_bars .clone() .into_iter() - .collect::>(); - assert_eq!(union_array.len(), 4); + .collect::>(); + assert_eq!(union_array.len(), 5); assert_eq!(union_array.into_iter().collect::>(), foo_bars); } diff --git a/src/logical/box.rs b/src/logical/box.rs new file mode 100644 index 0000000..8052ecb --- /dev/null +++ b/src/logical/box.rs @@ -0,0 +1,82 @@ +use crate::array::ArrayType; + +use super::{LogicalArray, LogicalArrayType}; + +impl> ArrayType> for Box +where + Option: ArrayType, +{ + type Array< + Buffer: crate::buffer::BufferType, + OffsetItem: crate::offset::OffsetElement, + UnionLayout: crate::array::UnionType, + > = LogicalArray; +} + +impl> ArrayType> for Option> +where + Option: ArrayType, +{ + type Array< + Buffer: crate::buffer::BufferType, + OffsetItem: crate::offset::OffsetElement, + UnionLayout: crate::array::UnionType, + > = LogicalArray, true, Buffer, OffsetItem, UnionLayout>; +} + +impl> LogicalArrayType> for Box +where + Option: ArrayType, +{ + type ArrayType = T; + + fn from_array_type(item: Self::ArrayType) -> Self { + Box::new(item) + } + + fn into_array_type(self) -> Self::ArrayType { + *self + } +} + +/// An array for [`Box`] items. +#[allow(unused)] +pub type BoxArray = + LogicalArray, NULLABLE, Buffer, crate::offset::NA, crate::array::union::NA>; + +#[cfg(test)] +mod tests { + use super::*; + use crate::Length; + + #[test] + fn from_iter() { + let array = [Box::new(1), Box::new(42)] + .into_iter() + .collect::>(); + assert_eq!(array.len(), 2); + assert_eq!(array.0.len(), 2); + + let array_nullable = [Some(Box::new(1)), None] + .into_iter() + .collect::>(); + assert_eq!(array_nullable.len(), 2); + assert_eq!(array_nullable.0.len(), 2); + } + + #[test] + fn into_iter() { + let input = [Box::new(1), Box::new(42)]; + let array = input.clone().into_iter().collect::>(); + let output = array.into_iter().collect::>(); + assert_eq!(input, output.as_slice()); + + let input_nullable = [Some(Box::new(1)), None]; + let array_nullable = input_nullable + .clone() + .into_iter() + .collect::>(); + let output_nullable = array_nullable.into_iter().collect::>(); + assert_eq!(input_nullable, output_nullable.as_slice()); + } +} diff --git a/src/logical/mod.rs b/src/logical/mod.rs index 9e06761..230099f 100644 --- a/src/logical/mod.rs +++ b/src/logical/mod.rs @@ -10,6 +10,9 @@ use crate::{ Length, }; +/// Box support via logical arrays. +pub mod r#box; + #[cfg(feature = "chrono")] /// Chrono support via logical arrays. pub mod chrono;