polars_arrow/scalar/
primitive.rsuse super::Scalar;
use crate::datatypes::ArrowDataType;
use crate::types::NativeType;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PrimitiveScalar<T: NativeType> {
value: Option<T>,
dtype: ArrowDataType,
}
impl<T: NativeType> PrimitiveScalar<T> {
#[inline]
pub fn new(dtype: ArrowDataType, value: Option<T>) -> Self {
if !dtype.to_physical_type().eq_primitive(T::PRIMITIVE) {
panic!(
"Type {} does not support logical type {:?}",
std::any::type_name::<T>(),
dtype
)
}
Self { value, dtype }
}
#[inline]
pub fn value(&self) -> &Option<T> {
&self.value
}
pub fn to(self, dtype: ArrowDataType) -> Self {
Self::new(dtype, self.value)
}
}
impl<T: NativeType> From<Option<T>> for PrimitiveScalar<T> {
#[inline]
fn from(v: Option<T>) -> Self {
Self::new(T::PRIMITIVE.into(), v)
}
}
impl<T: NativeType> Scalar for PrimitiveScalar<T> {
#[inline]
fn as_any(&self) -> &dyn std::any::Any {
self
}
#[inline]
fn is_valid(&self) -> bool {
self.value.is_some()
}
#[inline]
fn dtype(&self) -> &ArrowDataType {
&self.dtype
}
}