Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Ids representation from enums to unsigned integer wrapper #92

Open
UltraMachine opened this issue Nov 28, 2023 · 0 comments
Open
Labels
enhancement New feature or request
Milestone

Comments

@UltraMachine
Copy link
Owner

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:

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),
		}
	}
}
@UltraMachine UltraMachine added the enhancement New feature or request label Nov 28, 2023
@UltraMachine UltraMachine added this to the 2.0 milestone Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant