pub const MAX_DIMS: usize = 8;
#[repr(u8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub enum Dim {
Tokens = 0,
Millis = 1,
Bytes = 2,
Calls = 3,
Memory = 4,
Custom0 = 5,
Custom1 = 6,
Custom2 = 7,
}
impl Dim {
pub const ALL: [Self; MAX_DIMS] = [
Self::Tokens,
Self::Millis,
Self::Bytes,
Self::Calls,
Self::Memory,
Self::Custom0,
Self::Custom1,
Self::Custom2,
];
#[inline]
#[must_use]
pub const fn index(self) -> usize {
self as u8 as usize
}
#[inline]
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Tokens => "tokens",
Self::Millis => "millis",
Self::Bytes => "bytes",
Self::Calls => "calls",
Self::Memory => "memory",
Self::Custom0 => "custom0",
Self::Custom1 => "custom1",
Self::Custom2 => "custom2",
}
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::indexing_slicing)]
use super::*;
#[test]
fn index_matches_discriminant() {
for dim in Dim::ALL {
assert!(dim.index() < MAX_DIMS);
}
}
#[test]
fn indices_are_unique_and_dense() {
let mut seen = [false; MAX_DIMS];
for dim in Dim::ALL {
assert!(!seen[dim.index()], "duplicate index for {dim:?}");
seen[dim.index()] = true;
}
assert!(seen.iter().all(|&b| b), "indices are not dense 0..MAX_DIMS");
}
#[test]
fn all_length_matches_max_dims() {
assert_eq!(Dim::ALL.len(), MAX_DIMS);
}
#[test]
fn names_are_unique() {
let names: [&str; MAX_DIMS] = [
Dim::Tokens.name(),
Dim::Millis.name(),
Dim::Bytes.name(),
Dim::Calls.name(),
Dim::Memory.name(),
Dim::Custom0.name(),
Dim::Custom1.name(),
Dim::Custom2.name(),
];
for i in 0..MAX_DIMS {
for j in (i + 1)..MAX_DIMS {
assert_ne!(names[i], names[j]);
}
}
}
}