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

feat(rust): Adds the ability to specify custom derive attributes #678

Merged

Conversation

thomastaylor312
Copy link
Contributor

I also did a little bit of cleanup of clippy warnings while I was in these files.

Closes #554

@thomastaylor312
Copy link
Contributor Author

I am pretty sure the test failure is a flake or not related to my code as I am seeing it in other PRs as well

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Thanks for this! No worries on the failed test, that's known to be flaky and should be resolved on main soon through ignoring it.

At a high level one concern I have about this is that it won't work for Resource types. That won't implement Serialize/Deserialize and doesn't currently implement PartialEq/Hash, although it perhaps could. This means that implementing traits uniformly for all types may not be sufficient and eventually this may need a form of "ignore this type" or "only derive for this type". That all being said I think it's fine to deal with those situations as they come up later, for now this is suitable and works well so I'd say we should land it and then iterate on further syntaxes later if necessary.

@@ -159,7 +160,7 @@ pub trait RustGenerator<'a> {
param_mode: TypeMode,
sig: &FnSig,
) -> Vec<String> {
let params = self.print_docs_and_params(func, param_mode, &sig);
let params = self.print_docs_and_params(func, param_mode, sig);
Copy link
Member

Choose a reason for hiding this comment

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

No worries for this PR, but for future contributions I'd recommend splitting out clippy changes into a separate commit or possibly PR. Makes the "meat" elsewhere a bit easier to review that way.

crates/rust-lib/src/lib.rs Outdated Show resolved Hide resolved
crates/rust-lib/src/lib.rs Outdated Show resolved Hide resolved
) {
let info = self.info(id);
// We use a hash set to make sure we don't have any duplicates
let additional_derives: HashSet<String> = additional_derives.into_iter().collect();
Copy link
Member

Choose a reason for hiding this comment

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

One important caveat to be aware of about HashSet<T> is that its iteration order is not defined and not deterministic between runs of a process. This means, for example, that different code is generated from the same WIT source as derives might be shuffled around.

Mind using a BTreeSet (or IndexSet) here instead to preserve order?

Copy link
Contributor Author

@thomastaylor312 thomastaylor312 Sep 26, 2023

Choose a reason for hiding this comment

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

Oh right, I should have thought about the ordering issue. I'll switch that around

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should be all resolved, but leaving the comment unresolved until you have a chance to take a look at it

crates/rust-lib/src/lib.rs Outdated Show resolved Hide resolved
I also did a little bit of cleanup of clippy warnings while I was in
these files.

Closes bytecodealliance#554

Signed-off-by: Taylor Thomas <taylor@cosmonic.com>
@thomastaylor312
Copy link
Contributor Author

@alexcrichton Should be ready to go for a rereview

In regards to this:

At a high level one concern I have about this is that it won't work for Resource types. That won't implement Serialize/Deserialize and doesn't currently implement PartialEq/Hash, although it perhaps could. This means that implementing traits uniformly for all types may not be sufficient and eventually this may need a form of "ignore this type" or "only derive for this type". That all being said I think it's fine to deal with those situations as they come up later, for now this is suitable and works well so I'd say we should land it and then iterate on further syntaxes later if necessary.

I completely agree with the path forward here. I was curious how this would interact with resources, but it definitely seemed easiest to add this to struct/enum types for now and then we can see if there is a way to make it more specific in the future

@alexcrichton
Copy link
Member

Looks great to me, thanks!

One option is to use the TypeInfo bits to avoid these extra derives for types-with-resources, but that's still a bit specific and may not work out in all cases. In any case this looks good for now 👍

@alexcrichton alexcrichton merged commit 8c2abf4 into bytecodealliance:main Sep 27, 2023
9 checks passed
@thomastaylor312 thomastaylor312 deleted the feat/custom_derive_attr branch September 27, 2023 16:15
@sehz
Copy link

sehz commented Sep 27, 2023

does this work in cargo component ?

@thomastaylor312
Copy link
Contributor Author

@sehz I was actually going to go open an issue over there in cargo component

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.

Add support for Serialize, Deserialize, and Default
3 participants