-
Notifications
You must be signed in to change notification settings - Fork 8
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
I wonder if all the methods of the crate could be made const
#24
Comments
Hello and thank you for the kind words! Indeed, I plan on making everything |
Closing as this requires some support that only exists in nightly and is unlikely to stabilize soon or even in its current form. This is definitely a highly desirable feature which I will implement when support lands in stable. |
Yay 🌈
Ohh, I didn't know about that crate! Nor did I know about the RFC to include it in My Given how the process is trending towards
Of course ^^ 🫶🏽 |
On a related note. Just today I had to implement |
It is not possible without special-casing the macro as it supports nested types and relies on trait functions to instantiate values. The following solution seems simple enough (though it does pull in another crate): use enum_iterator::Sequence;
use once_cell::sync::Lazy;
#[derive(Debug, PartialEq, Sequence)]
pub enum Foo {
A,
B(bool),
}
pub fn all_foos() -> &'static [Foo] {
static FOOS: Lazy<Vec<Foo>> = Lazy::new(|| enum_iterator::all().collect());
&**FOOS
} |
I see. Thanks for posting the example. I have done similar today with Obviously hardcoding is not difficult, it's just that as the types evolve and grow it becomes harder to keep track of things. So eventually I decided to plug in a |
You're welcome. Actually, I just remembered it's possible to achieve without an additional crate as the needed functionality was merged into use enum_iterator::Sequence;
use std::sync::OnceLock;
#[derive(Debug, PartialEq, Sequence)]
pub enum Foo {
A,
B(bool),
}
pub fn all_foos() -> &'static [Foo] {
static FOOS: OnceLock<Vec<Foo>> = OnceLock::new();
&**FOOS.get_or_init(|| enum_iterator::all().collect())
} If you only need this |
Hellu. First of all, thanks for the crate. It's freaking awesome.
So I was doing some exercises at exercism.org, as practice, and there was an exercise that used this crate. I realized that most of the functions it requires don't (or almost don't) need any runtime information to be computed:
value_to_color_string
can be solved with aconst Hashmap<u32, &str>
colors
is basically aconst Vec<ResistorColor>
So long story short, I realized that I couldn't make these things
const
because, among other things,enum-iterator
itself's methods aren't const. So I came and tried to addconst
ness for the methods, thinking of submitting that as a PR, but I ran into a wall in the language that took me to rust-lang/rust#101900.I think the methods in the crate can't currently be made
const
(since they are Trait methods after all?), but I could be wrong.Anywho. I'm 99% sure you've already delved into this, and that it's in some todo-list of your own. But perhaps it wasn't, and this helps somewhat.
Also this allows me to say thanks for the crate. It's a really cool crate. Thanks :D
The text was updated successfully, but these errors were encountered: