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

Use associated constant for EnumCount #96

Closed
Boscop opened this issue May 25, 2020 · 0 comments · Fixed by #99
Closed

Use associated constant for EnumCount #96

Boscop opened this issue May 25, 2020 · 0 comments · Fixed by #99

Comments

@Boscop
Copy link

Boscop commented May 25, 2020

Use an associated constant for EnumCount:

pub trait EnumCount {
    const COUNT: usize;
}

This would replace both the count() method

fn count() -> usize {

and the <enum-name>_COUNT constant
pub const #const_name: usize = #n;

and it works on stable since 1.20.

(There is no reason why this needs to be a method (not even a const-fn method), and it's unelegant to have both a method and a non-associated constant.)

An associated constant is the right way to express this.

Related discussion: #58 #15 (comment)


Btw, this was motivated by my real-world use case that is currently not possible with EnumCount, I need to define the following:

pub trait Param: From<usize> + Into<usize> + Serialize + DeserializeOwned {
	const PARAM_COUNT: usize;
}

impl<T: From<usize> + Into<usize> + EnumCount> Param for T
where
	[T; <T as EnumCount>::count()]: Serialize + DeserializeOwned,
{
	const PARAM_COUNT: usize = T::count();
}

It's not possible because count() is not a constant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant