Skip to main content

maybe_fatal/code/
discriminant.rs

1use core::fmt::Display;
2
3use sealed::sealed;
4
5/// The default type of a [`DiagnosticCode`](super::DiagnosticCode)'s discriminant.
6pub type DefaultDiscriminant = u8;
7
8/// A discriminant for a [`DiagnosticCode`](super::DiagnosticCode).
9///
10/// # Note to Consumers
11///
12/// This trait is **sealed**; it cannot be implemented outside the [`maybe_fatal`](crate) crate. It
13/// is currently implemented for the following types:
14///
15/// - [`u8`]
16/// - [`u16`]
17/// - [`u32`]
18/// - [`u64`]
19/// - [`u128`]
20#[sealed]
21pub trait Discriminant: Display {
22    /// The max number of digits in the discriminant.
23    const MAX_N_DIGITS: usize;
24}
25
26macro_rules! impl_Discriminant {
27    [$($ty:ty),* $(,)?] => {
28        $(
29            #[sealed]
30            impl Discriminant for $ty {
31                const MAX_N_DIGITS: usize = 1 + Self::MAX.ilog10() as usize;
32            }
33        )*
34    };
35}
36
37impl_Discriminant![u8, u16, u32, u64, u128];
38
39// Asserts that `DefaultDiscriminant` implements `Discriminant`.
40const _: () = {
41    let _ = <DefaultDiscriminant as Discriminant>::MAX_N_DIGITS;
42};