vortex_scalar/scalarvalue/
primitive.rs1use paste::paste;
2use vortex_dtype::half::f16;
3use vortex_error::{VortexError, vortex_err};
4
5use crate::ScalarValue;
6use crate::scalarvalue::InnerScalarValue;
7
8macro_rules! primitive_scalar {
9 ($T:ty) => {
10 impl TryFrom<&ScalarValue> for $T {
11 type Error = VortexError;
12
13 fn try_from(value: &ScalarValue) -> Result<Self, Self::Error> {
14 <Option<$T>>::try_from(value)?
15 .ok_or_else(|| vortex_err!("Can't extract present value from null scalar"))
16 }
17 }
18
19 impl TryFrom<&ScalarValue> for Option<$T> {
20 type Error = VortexError;
21
22 fn try_from(value: &ScalarValue) -> Result<Self, Self::Error> {
23 paste! {
24 Ok(value.as_pvalue()?.and_then(|v| v.[<as_ $T>]()))
25 }
26 }
27 }
28
29 impl From<$T> for ScalarValue {
30 fn from(value: $T) -> Self {
31 ScalarValue(InnerScalarValue::Primitive(value.into()))
32 }
33 }
34 };
35}
36
37primitive_scalar!(u8);
38primitive_scalar!(u16);
39primitive_scalar!(u32);
40primitive_scalar!(u64);
41primitive_scalar!(i8);
42primitive_scalar!(i16);
43primitive_scalar!(i32);
44primitive_scalar!(i64);
45primitive_scalar!(f16);
46primitive_scalar!(f32);
47primitive_scalar!(f64);
48
49impl TryFrom<&ScalarValue> for usize {
51 type Error = VortexError;
52
53 fn try_from(value: &ScalarValue) -> Result<Self, Self::Error> {
54 let prim = value
55 .as_pvalue()?
56 .and_then(|v| v.as_u64())
57 .ok_or_else(|| vortex_err!("cannot convert Null to usize"))?;
58 Ok(usize::try_from(prim)?)
59 }
60}
61
62impl From<usize> for ScalarValue {
64 fn from(value: usize) -> Self {
65 ScalarValue(InnerScalarValue::Primitive((value as u64).into()))
66 }
67}