Path: blob/main/crates/polars-arrow/src/scalar/primitive.rs
6939 views
use super::Scalar;1use crate::datatypes::ArrowDataType;2use crate::types::NativeType;34/// The implementation of [`Scalar`] for primitive, semantically equivalent to [`Option<T>`]5/// with [`ArrowDataType`].6#[derive(Debug, Clone, PartialEq, Eq)]7pub struct PrimitiveScalar<T: NativeType> {8value: Option<T>,9dtype: ArrowDataType,10}1112impl<T: NativeType> PrimitiveScalar<T> {13/// Returns a new [`PrimitiveScalar`].14#[inline]15pub fn new(dtype: ArrowDataType, value: Option<T>) -> Self {16if !dtype.to_physical_type().eq_primitive(T::PRIMITIVE) {17panic!(18"Type {} does not support logical type {:?}",19std::any::type_name::<T>(),20dtype21)22}23Self { value, dtype }24}2526/// Returns the optional value.27#[inline]28pub fn value(&self) -> &Option<T> {29&self.value30}3132/// Returns a new `PrimitiveScalar` with the same value but different [`ArrowDataType`]33/// # Panic34/// This function panics if the `dtype` is not valid for self's physical type `T`.35pub fn to(self, dtype: ArrowDataType) -> Self {36Self::new(dtype, self.value)37}38}3940impl<T: NativeType> From<Option<T>> for PrimitiveScalar<T> {41#[inline]42fn from(v: Option<T>) -> Self {43Self::new(T::PRIMITIVE.into(), v)44}45}4647impl<T: NativeType> Scalar for PrimitiveScalar<T> {48#[inline]49fn as_any(&self) -> &dyn std::any::Any {50self51}5253#[inline]54fn is_valid(&self) -> bool {55self.value.is_some()56}5758#[inline]59fn dtype(&self) -> &ArrowDataType {60&self.dtype61}62}636465