Skip to content

Commit

Permalink
feat: add ArrayType impl for Box<T> (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrobbel authored Sep 3, 2024
1 parent 9b42ca2 commit 5385962
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
6 changes: 4 additions & 2 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn main() {
enum FooBar {
Foo(Foo),
Bar(Bar),
Box(Box<u32>),
None,
}

Expand All @@ -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::<UnionArray<FooBar, 3>>();
assert_eq!(union_array.len(), 4);
.collect::<UnionArray<FooBar, 4>>();
assert_eq!(union_array.len(), 5);
assert_eq!(union_array.into_iter().collect::<Vec<_>>(), foo_bars);
}
82 changes: 82 additions & 0 deletions src/logical/box.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use crate::array::ArrayType;

use super::{LogicalArray, LogicalArrayType};

impl<T: ArrayType<T>> ArrayType<Box<T>> for Box<T>
where
Option<T>: ArrayType<T>,
{
type Array<
Buffer: crate::buffer::BufferType,
OffsetItem: crate::offset::OffsetElement,
UnionLayout: crate::array::UnionType,
> = LogicalArray<Self, false, Buffer, OffsetItem, UnionLayout>;
}

impl<T: ArrayType<T>> ArrayType<Box<T>> for Option<Box<T>>
where
Option<T>: ArrayType<T>,
{
type Array<
Buffer: crate::buffer::BufferType,
OffsetItem: crate::offset::OffsetElement,
UnionLayout: crate::array::UnionType,
> = LogicalArray<Box<T>, true, Buffer, OffsetItem, UnionLayout>;
}

impl<T: ArrayType<T>> LogicalArrayType<Box<T>> for Box<T>
where
Option<T>: ArrayType<T>,
{
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<T, const NULLABLE: bool = false, Buffer = crate::buffer::VecBuffer> =
LogicalArray<Box<T>, 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::<BoxArray<i32>>();
assert_eq!(array.len(), 2);
assert_eq!(array.0.len(), 2);

let array_nullable = [Some(Box::new(1)), None]
.into_iter()
.collect::<BoxArray<i32, true>>();
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::<BoxArray<i32>>();
let output = array.into_iter().collect::<Vec<_>>();
assert_eq!(input, output.as_slice());

let input_nullable = [Some(Box::new(1)), None];
let array_nullable = input_nullable
.clone()
.into_iter()
.collect::<BoxArray<i32, true>>();
let output_nullable = array_nullable.into_iter().collect::<Vec<_>>();
assert_eq!(input_nullable, output_nullable.as_slice());
}
}
3 changes: 3 additions & 0 deletions src/logical/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5385962

Please sign in to comment.