verity_memory/macros/
match_number.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#[derive(Debug)]
#[allow(dead_code)]
pub(crate) enum NumberType<'a> {
    Float(FloatType<'a>),
    Integer(IntegerType<'a>),
    Integral(IntegralType<'a>),
    Unknown,
}

#[derive(Debug)]
#[allow(dead_code)]
pub(crate) enum FloatType<'a> {
    F32(&'a f32),
    F64(&'a f64),
}

#[derive(Debug)]
#[allow(dead_code)]
pub(crate) enum IntegerType<'a> {
    I32(&'a i32),
    I64(&'a i64),
}

#[derive(Debug)]
#[allow(dead_code)]
pub(crate) enum IntegralType<'a> {
    U8(&'a u8),
    U16(&'a u16),
    U32(&'a u32),
    U64(&'a u64),
}

#[macro_export]
macro_rules! match_number {
    ($number:expr) => {{
        let any_ref: &dyn std::any::Any = &$number;
        let type_id = any_ref.type_id();

        let number_type = match type_id {
            
            t if t == std::any::TypeId::of::<f32>() => {
                NumberType::Float(FloatType::F32(any_ref.downcast_ref::<f32>().expect("Failed to downcast to f32")))
            }
            t if t == std::any::TypeId::of::<f64>() => {
                NumberType::Float(FloatType::F64(any_ref.downcast_ref::<f64>().expect("Failed to downcast to f64")))
            }

            t if t == std::any::TypeId::of::<i32>() => {
                NumberType::Integer(IntegerType::I32(any_ref.downcast_ref::<i32>().expect("Failed to downcast to i32")))
            }
            t if t == std::any::TypeId::of::<i64>() => {
                NumberType::Integer(IntegerType::I64(any_ref.downcast_ref::<i64>().expect("Failed to downcast to i64")))
            }

            t if t == std::any::TypeId::of::<u8>() => {
                NumberType::Integral(IntegralType::U8(any_ref.downcast_ref::<u8>().expect("Failed to downcast to u8")))
            }
            t if t == std::any::TypeId::of::<u16>() => {
                NumberType::Integral(IntegralType::U16(any_ref.downcast_ref::<u16>().expect("Failed to downcast to u16")))
            }
            t if t == std::any::TypeId::of::<u32>() => {
                NumberType::Integral(IntegralType::U32(any_ref.downcast_ref::<u32>().expect("Failed to downcast to u32")))
            }
            t if t == std::any::TypeId::of::<u64>() => {
                NumberType::Integral(IntegralType::U64(any_ref.downcast_ref::<u64>().expect("Failed to downcast to u64")))
            }
            _ => NumberType::Unknown,
        };

        Some(number_type)
    }};
}