1
//! Helper type for disabling memory tracking when not wanted
2

            
3
/// Token indicating that memory quota tracking is enabled, at both compile and runtime
4
///
5
/// If support is compiled in this is a unit.
6
///
7
/// If the `memquota` cargo feature is not enabled, this type is uninhabited.
8
/// Scattering values of this type around in relevant data structures
9
/// and parameters lists
10
/// allows the compiler to eliminate the unwanted code.
11
#[derive(Clone, Copy, Debug, PartialEq)]
12
pub struct EnabledToken {
13
    /// Make non-exhaustive even within the crate
14
    _hidden: (),
15

            
16
    /// Uninhabited if the feature isn't enabled.
17
    #[cfg(not(feature = "memquota"))]
18
    _forbid: void::Void,
19
}
20

            
21
impl Eq for EnabledToken {}
22

            
23
impl EnabledToken {
24
    /// Obtain an `EnabledToken` (only available if tracking is compiled in)
25
    #[allow(clippy::new_without_default)] // a conditional Default impl would be rather odd
26
    #[cfg(feature = "memquota")]
27
290
    pub const fn new() -> Self {
28
290
        EnabledToken { _hidden: () }
29
290
    }
30

            
31
    /// Obtain an `EnabledToken` if memory-tracking is compiled in, or `None` otherwise
32
    #[allow(clippy::unnecessary_wraps)] // Will be None if compiled out
33
    #[allow(unreachable_code)]
34
1200136
    pub const fn new_if_compiled_in() -> Option<Self> {
35
1200136
        Some(EnabledToken {
36
1200136
            _hidden: (),
37
1200136

            
38
1200136
            #[cfg(not(feature = "memquota"))]
39
1200136
            _forbid: return None,
40
1200136
        })
41
1200136
    }
42
}