verity_memory/macros/
match_number.rs

1#[derive(Debug)]
2#[allow(dead_code)]
3pub(crate) enum NumberType<'a> {
4    Float(FloatType<'a>),
5    Integer(IntegerType<'a>),
6    Integral(IntegralType<'a>),
7    Unknown,
8}
9
10#[derive(Debug)]
11#[allow(dead_code)]
12pub(crate) enum FloatType<'a> {
13    F32(&'a f32),
14    F64(&'a f64),
15}
16
17#[derive(Debug)]
18#[allow(dead_code)]
19pub(crate) enum IntegerType<'a> {
20    I32(&'a i32),
21    I64(&'a i64),
22}
23
24#[derive(Debug)]
25#[allow(dead_code)]
26pub(crate) enum IntegralType<'a> {
27    U8(&'a u8),
28    U16(&'a u16),
29    U32(&'a u32),
30    U64(&'a u64),
31}
32
33#[macro_export]
34macro_rules! match_number {
35    ($number:expr) => {{
36        let any_ref: &dyn std::any::Any = &$number;
37        let type_id = any_ref.type_id();
38
39        let number_type = match type_id {
40            
41            t if t == std::any::TypeId::of::<f32>() => {
42                NumberType::Float(FloatType::F32(any_ref.downcast_ref::<f32>().expect("Failed to downcast to f32")))
43            }
44            t if t == std::any::TypeId::of::<f64>() => {
45                NumberType::Float(FloatType::F64(any_ref.downcast_ref::<f64>().expect("Failed to downcast to f64")))
46            }
47
48            t if t == std::any::TypeId::of::<i32>() => {
49                NumberType::Integer(IntegerType::I32(any_ref.downcast_ref::<i32>().expect("Failed to downcast to i32")))
50            }
51            t if t == std::any::TypeId::of::<i64>() => {
52                NumberType::Integer(IntegerType::I64(any_ref.downcast_ref::<i64>().expect("Failed to downcast to i64")))
53            }
54
55            t if t == std::any::TypeId::of::<u8>() => {
56                NumberType::Integral(IntegralType::U8(any_ref.downcast_ref::<u8>().expect("Failed to downcast to u8")))
57            }
58            t if t == std::any::TypeId::of::<u16>() => {
59                NumberType::Integral(IntegralType::U16(any_ref.downcast_ref::<u16>().expect("Failed to downcast to u16")))
60            }
61            t if t == std::any::TypeId::of::<u32>() => {
62                NumberType::Integral(IntegralType::U32(any_ref.downcast_ref::<u32>().expect("Failed to downcast to u32")))
63            }
64            t if t == std::any::TypeId::of::<u64>() => {
65                NumberType::Integral(IntegralType::U64(any_ref.downcast_ref::<u64>().expect("Failed to downcast to u64")))
66            }
67            _ => NumberType::Unknown,
68        };
69
70        Some(number_type)
71    }};
72}