From 03275bebffbaf68fbfddee5822c4a7be85098a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9E=97=E4=BC=9F?= Date: Tue, 14 Nov 2023 10:37:25 +0800 Subject: [PATCH] Implement From for BroadcastablePrimitive (#247) * Improve BroadcastablePrimitive * Implement From trait for numeric --- src/algorithm/broadcasting/primitive.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/algorithm/broadcasting/primitive.rs b/src/algorithm/broadcasting/primitive.rs index 917bbb88..e0fcd967 100644 --- a/src/algorithm/broadcasting/primitive.rs +++ b/src/algorithm/broadcasting/primitive.rs @@ -1,6 +1,7 @@ use arrow_array::iterator::ArrayIter; use arrow_array::types::ArrowPrimitiveType; use arrow_array::PrimitiveArray; +use arrow_buffer::ArrowNativeType; /// An enum over primitive types defined by [`arrow2::types::NativeType`]. These include u8, i32, /// f64, etc. @@ -49,3 +50,24 @@ where } } } + +impl> From for BroadcastablePrimitive

{ + fn from(value: N) -> Self { + BroadcastablePrimitive::Scalar(value) + } +} + +#[cfg(test)] +mod tests { + use crate::algorithm::broadcasting::BroadcastablePrimitive; + use arrow_array::types::{Float64Type, UInt32Type}; + + #[test] + fn from_numeric() { + let scalar: BroadcastablePrimitive = 1u32.into(); + assert_eq!(scalar.into_iter().next(), Some(Some(1u32))); + + let scalar: BroadcastablePrimitive = 1.0f64.into(); + assert_eq!(scalar.into_iter().next(), Some(Some(1.0f64))); + } +}