Skip to content

Commit

Permalink
Implement From for BroadcastablePrimitive (#247)
Browse files Browse the repository at this point in the history
* Improve BroadcastablePrimitive

* Implement From trait for numeric
  • Loading branch information
lewiszlw authored Nov 14, 2023
1 parent aed9b91 commit 03275be
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/algorithm/broadcasting/primitive.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -49,3 +50,24 @@ where
}
}
}

impl<N: ArrowNativeType, P: ArrowPrimitiveType<Native = N>> From<N> for BroadcastablePrimitive<P> {
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<UInt32Type> = 1u32.into();
assert_eq!(scalar.into_iter().next(), Some(Some(1u32)));

let scalar: BroadcastablePrimitive<Float64Type> = 1.0f64.into();
assert_eq!(scalar.into_iter().next(), Some(Some(1.0f64)));
}
}

0 comments on commit 03275be

Please sign in to comment.