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
/// Return a long version of the function name.
#[doc(hidden)]
#[macro_export]
macro_rules! function {
    () => {{
        fn _f() {}
        fn _type_name_of<T>(_: T) -> &'static str {
            core::any::type_name::<T>()
        }
        let name = _type_name_of(_f);
        &name[..name.len() - 3]
    }};
}

/// Return a shortened version of the function name.
#[doc(hidden)]
#[macro_export]
macro_rules! short_function {
    () => {{
        fn f() {}
        fn type_name_of<T>(_: T) -> &'static str {
            core::any::type_name::<T>()
        }
        let name = type_name_of(f);

        // Find and cut the rest of the path
        match &name[..name.len() - 3].rfind(':') {
            Some(pos) => &name[pos + 1..name.len() - 3],
            None => &name[..name.len() - 3],
        }
    }};
}

/// Return a shortened version of the function name outside the closure.
#[doc(hidden)]
#[macro_export]
macro_rules! containing_function {
    () => {{
        fn f() {}
        fn type_name_of<T>(_: T) -> &'static str {
            core::any::type_name::<T>()
        }
        let name = type_name_of(f);

        // Find and cut the rest of the path
        match &name[..name.len() - 3].strip_suffix("::{{closure}}") {
            Some(stripped) => match &stripped.rfind(':') {
                Some(pos) => &stripped[pos + 1..stripped.len()],
                None => &stripped,
            },
            None => &name[..name.len() - 3],
        }
    }};
}

/// Log bytes
#[doc(hidden)]
#[macro_export]
macro_rules! log_bytes {
    ($obj: expr) => {
        crate::util::macro_logger::DebugBytes(&$obj)
    };
}