tor_memquota_cost/if_enabled.rs
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)]
12pub 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
21impl Eq for EnabledToken {}
22
23impl 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 pub const fn new() -> Self {
28 EnabledToken { _hidden: () }
29 }
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 pub const fn new_if_compiled_in() -> Option<Self> {
35 Some(EnabledToken {
36 _hidden: (),
37
38 #[cfg(not(feature = "memquota"))]
39 _forbid: return None,
40 })
41 }
42}