-
Notifications
You must be signed in to change notification settings - Fork 150
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
Enum Map derive macro #272
Comments
I also think this would be really useful. One could use a hashmap but then you'd have to I created an initial implementation of a derive macro for this on my own repo here. If you have the following code: #[derive(EnumMap)]
enum Size {
Small,
Medium,
Large,
} it expands to: struct SizeMap<T> {
small: T,
medium: T,
large: T,
}
impl<T> SizeMap<T> {
fn new(small: T, medium: T, large: T) -> SizeMap<T> {
SizeMap { small, medium, large }
}
fn get(&self, variant: Size) -> &T {
match variant {
Size::Small => &self.small,
Size::Medium => &self.medium,
Size::Large => &self.large,
}
}
fn set(&mut self, variant: Size, new_value: T) {
match variant {
Size::Small => self.small = new_value,
Size::Medium => self.medium = new_value,
Size::Large => self.large = new_value,
}
}
} I'd be happy to turn this into a PR if the owners of this repo would be interesting in having this macro. I also feel that there are questions to be discussed before releasing something like this, e.g.:
If any maintainers would like to help us move forward with getting this merged in by reviewing a PR or answering these questions with their thoughts, that would be really appreciated! |
That looks great! Would you like access to my version so we can start merging our implementations?
Should we do a blanket fn iter(&self) -> Iterator<Item = T> {
Enum::iter().map(|k| (k, self.get(k))
} |
Oh my god, I feel a bit foolish now - when I pulled up your version last time, github showed me no changes from its parent repo for some reason and I just assumed you hadn't committed your work yet (and thus went on to do a lot of extraneous work on my own repo). I didn't consider just implementing |
lol I thought yours seemed more complete. I also don't really know what I'm doing with all these procedural macros. A lot of my stuff is probably cargo cult-ish, since I copied a bunch from EnumIter. I think we should also change it to the named struct fields rather than sized array |
Hey folks, this sounds like a really useful macro, but I'm worried there's a number of situational concerns that will make it difficult to generalize across a range of needs. Some thoughts:
If we can make some of these trade-offs more configurable, I'd consider including it in strum because it is a cool idea, and it sounds like there's a need :) If not though, it might be better off as its own crate so that people have some flexibility in choosing the implementation they want for this feature. How does that sound? |
As for your first two points, that sounds like you'd just have a wrapper around HashMap or BTreeMap. The main point for my application of this one is that there's always a value, so you can safely index into it without thinking about unwrapping. It might make more sense to call it something other than EnumMap, since it's basically a named array and not a true map. Having said that, it is starting to sound like it doesn't quite fit into strum. On the other hand, it seems way too small to be its own crate. |
I've updated my repo with some of June's code. Here's a list of important features I have working:
|
I think it would be useful to have a map that you can index with an enum. I had the idea while working on a game inventory system where items are enums. I've started my own fork to start the macro. I'm struggling with the dependency on EnumIter and possible support for variants with attached data.
I still have a long way to go before it's PR-quality. I'm posting here to gauge whether my idea seems worthwhile and to ask for help. I'm very new to open source so I'm not sure if I'm doing all this right.
The text was updated successfully, but these errors were encountered: