Open UltraMachine opened 9 months ago
Allows handling unknown ids in non-panicking way, while not breaking existing code, since associated constants can be referenced the same way as enum variants: SomeId::VariantX
SomeId::VariantX
Example code:
use std::fmt; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[repr(transparent)] pub struct SomeId(u32); #[allow(non_upper_case_globals)] impl SomeId { pub const Variant1: Self = Self(1); pub const Variant2: Self = Self(2); pub const Variant3: Self = Self(3); pub fn other(id: u32) -> Self { Self(id) } } impl fmt::Display for SomeId { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { Self::Variant1 => write!(f, "Variant1"), Self::Variant2 => write!(f, "Variant2"), Self::Variant3 => write!(f, "Variant3"), _ => write!(f, "Other({})", self.0), } } }
Allows handling unknown ids in non-panicking way, while not breaking existing code, since associated constants can be referenced the same way as enum variants:
SomeId::VariantX
Example code: