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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/// Label for resources. Optimized out in release builds.
#[derive(Clone, Default, Hash, PartialEq, Eq)]
pub struct DebugLabel {
    #[cfg(debug_assertions)]
    label: String,
}

impl std::fmt::Debug for DebugLabel {
    #[cfg(debug_assertions)]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.label.fmt(f)
    }

    #[cfg(not(debug_assertions))]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DebugLabel").finish_non_exhaustive()
    }
}

impl std::fmt::Display for DebugLabel {
    #[cfg(debug_assertions)]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.label.fmt(f)
    }

    #[cfg(not(debug_assertions))]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DebugLabel").finish_non_exhaustive()
    }
}

impl DebugLabel {
    #[allow(clippy::unnecessary_wraps)]
    #[inline]
    pub fn get(&self) -> Option<&str> {
        #[cfg(debug_assertions)]
        {
            Some(&self.label)
        }
        #[cfg(not(debug_assertions))]
        {
            None
        }
    }
}

impl From<&str> for DebugLabel {
    #[inline]
    fn from(str: &str) -> Self {
        #[cfg(not(debug_assertions))]
        {
            _ = str;
        }

        Self {
            #[cfg(debug_assertions)]
            label: str.to_owned(),
        }
    }
}

impl From<String> for DebugLabel {
    #[inline]
    fn from(str: String) -> Self {
        #[cfg(not(debug_assertions))]
        {
            _ = str;
        }

        Self {
            #[cfg(debug_assertions)]
            label: str,
        }
    }
}

impl From<Option<&str>> for DebugLabel {
    #[inline]
    fn from(str: Option<&str>) -> Self {
        #[cfg(not(debug_assertions))]
        {
            _ = str;
        }

        Self {
            #[cfg(debug_assertions)]
            label: str.unwrap_or("").to_owned(),
        }
    }
}