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

Add FromStr impl for operands #171

Merged
merged 3 commits into from
Nov 13, 2020
Merged

Add FromStr impl for operands #171

merged 3 commits into from
Nov 13, 2020

Conversation

khyperia
Copy link
Collaborator

This would be a pain to implement all the boilerplate for outside of rspirv, so throwing it in here for our use case in rust-gpu.

Note that this does not implement FromStr for bitflags! operands, but implementing FromStr for those becomes a bit opinionated - do you create a list of values? how do you separate each individual value? so it's not really possible to impl it in an unopinionated way (and besides, there's not many bitflags, it's easy enough for users to implement their own FromStr).

let number = e.value;
seen_discriminator.insert(e.value, name.clone());
enumerants.push(quote! { #name = #number });
from_prim_list.push(quote! { #number => #kind::#name });
from_str_impl.push(quote! { #name_str => Ok(#kind::#name), });
Copy link
Collaborator

@MarijnS95 MarijnS95 Nov 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be

Suggested change
from_str_impl.push(quote! { #name_str => Ok(#kind::#name), });
from_str_impl.push(quote! { #name_str => Ok(Self::#name), });

To save quite a bunch of characters on every line? That fits with the Result<Self, ...> return type as well.

EDIT: I guess the same cleanup applies to from_prim_list?

Copy link
Collaborator

@MarijnS95 MarijnS95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Approval from me, if counts at all 😅

It's sad there's no #[derive(FromStr)] though, these are about ~800 extra lines for strings that appear identical to their enum variant yet could be generated at compile-time instead of checked in. The perks of auto-generated files 😁

let number = e.value;
seen_discriminator.insert(e.value, name.clone());
enumerants.push(quote! { #name = #number });
from_prim_list.push(quote! { #number => #kind::#name });
from_prim_list.push(quote! { #number => Self::#name });
from_str_impl.push(quote! { #name_str => Ok(Self::#name), });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the aliases should probably also appear. Right now ClosestHitKHR isn't recognised but ClosestHitNV is for example, however the new way forward it through the KHR extension.

@khyperia khyperia merged commit 01ca0d2 into master Nov 13, 2020
@khyperia khyperia deleted the FromStr-for-operands branch November 13, 2020 12:13
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 this pull request may close these issues.

3 participants