polars_arrow/scalar/
primitive.rs1use super::Scalar;
2use crate::datatypes::ArrowDataType;
3use crate::types::NativeType;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct PrimitiveScalar<T: NativeType> {
9 value: Option<T>,
10 dtype: ArrowDataType,
11}
12
13impl<T: NativeType> PrimitiveScalar<T> {
14 #[inline]
16 pub fn new(dtype: ArrowDataType, value: Option<T>) -> Self {
17 if !dtype.to_physical_type().eq_primitive(T::PRIMITIVE) {
18 panic!(
19 "Type {} does not support logical type {:?}",
20 std::any::type_name::<T>(),
21 dtype
22 )
23 }
24 Self { value, dtype }
25 }
26
27 #[inline]
29 pub fn value(&self) -> &Option<T> {
30 &self.value
31 }
32
33 pub fn to(self, dtype: ArrowDataType) -> Self {
37 Self::new(dtype, self.value)
38 }
39}
40
41impl<T: NativeType> From<Option<T>> for PrimitiveScalar<T> {
42 #[inline]
43 fn from(v: Option<T>) -> Self {
44 Self::new(T::PRIMITIVE.into(), v)
45 }
46}
47
48impl<T: NativeType> Scalar for PrimitiveScalar<T> {
49 #[inline]
50 fn as_any(&self) -> &dyn std::any::Any {
51 self
52 }
53
54 #[inline]
55 fn is_valid(&self) -> bool {
56 self.value.is_some()
57 }
58
59 #[inline]
60 fn dtype(&self) -> &ArrowDataType {
61 &self.dtype
62 }
63}