arrow_array

Macro downcast_primitive

Source
macro_rules! downcast_primitive {
    ($($data_type:expr),+ => ($m:path $(, $args:tt)*), $($p:pat => $fallback:expr $(,)*)*) => { ... };
}
Expand description

Given one or more expressions evaluating to primitive DataType invokes the provided macro m with the corresponding ArrowPrimitiveType, followed by any additional arguments


macro_rules! primitive_size_helper {
  ($t:ty, $o:ty) => {
      std::mem::size_of::<<$t as ArrowPrimitiveType>::Native>() as $o
  };
}

fn primitive_size(t: &DataType) -> u8 {
    downcast_primitive! {
        t => (primitive_size_helper, u8),
        _ => u8::MAX
    }
}

assert_eq!(primitive_size(&DataType::Int32), 4);
assert_eq!(primitive_size(&DataType::Int64), 8);
assert_eq!(primitive_size(&DataType::Float16), 2);
assert_eq!(primitive_size(&DataType::Decimal128(38, 10)), 16);
assert_eq!(primitive_size(&DataType::Decimal256(76, 20)), 32);