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

Derive TryIntoJs for structs #148

Merged
merged 38 commits into from
May 3, 2021
Merged

Derive TryIntoJs for structs #148

merged 38 commits into from
May 3, 2021

Conversation

mkawalec
Copy link
Contributor

@mkawalec mkawalec commented Apr 19, 2021

This implements #147, without the bonus points.

I am fairly unhappy about the fact that we demand fields to be Clone. Because try_into_js takes ownership of Self, I am unsure how to run it for each of the struct's fields without cloning. Please do let me know if there is a way of making the borrow checker happy without cloning, it feels like there should be one.

Added a TryIntoJs instance for usize by casting it to u64. I don't think we expect this code to run on architectures wider than 64 bits, so the conversion should be a safe one.

@nicholastmosher
Copy link
Contributor

Hi @mkawalec, thanks for the PR! I'll try to read through it and have some comments by the end of the day :)

@sehz
Copy link
Collaborator

sehz commented Apr 19, 2021

let me see if we can change try_to_js to use &self rather than self

Copy link
Collaborator

@sehz sehz left a comment

Choose a reason for hiding this comment

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

Very impressive work. Just one comments for now. I will have more feedback after digesting more.


fn main() {

}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Parser doesn't support Enum and Union. That should be include in fail cases

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea

@mkawalec
Copy link
Contributor Author

mkawalec commented Apr 19, 2021

@sehz Thank you. I didn't look into using &self in try_to_js too hard, but if there is no explicit reason why it must take ownership, I can move it to a reference in the second half of the week.

@nicholastmosher
Copy link
Contributor

I took a first-pass read through this today and everything looks really good, I think you did a great job. I wanted to play around with cargo-expand on some of the examples but for some reason it is not working for me, so I wasn't able to dig around with that like I was planning to. Tomorrow I'll take another deeper look and try to test the limits of this a bit more than the one test. Thanks again for putting in such great work!

@sehz
Copy link
Collaborator

sehz commented Apr 26, 2021

Any update? Just need add fail test case

@mkawalec
Copy link
Contributor Author

@sehz I haven't had any free time in the meantime, with a bit of luck this will happen this week. I want to get rid of the clone call on object fields before we merge this in.

@mkawalec mkawalec marked this pull request as draft April 27, 2021 08:29
@mkawalec mkawalec marked this pull request as ready for review April 27, 2021 20:16
@mkawalec
Copy link
Contributor Author

I think I've got it - now only references need to be Clone, with no cloning happening for simple values.

@nicholastmosher
Copy link
Contributor

@mkawalec I've just finished reading through this again and I think I'm happy with just about everything in here. The one question I have is why you decided to add support for unit structs? It seems to me that there isn't much use to converting those at all. It also took me awhile to figure out that they turned into empty objects. I was wondering if you could just talk me through why you decided to do it that way?

@mkawalec
Copy link
Contributor Author

My reasoning was purely consistency based - if structs translate to objects, a unit struct would translate to an object with no keys. I was mostly thinking about allowing complex structs with many levels of structs embedded inside of them to convert automagically into Node.

That being said I am in no way set on that conversion - I can definitely see a merit of using a null to represent unit structs, or even a string equal to that struct's name. Do you have a more appropriate conversion in mind?

@nicholastmosher
Copy link
Contributor

I suppose my initial reaction is that any of those options could make sense, but we currently have not seen a use-case that has demanded any approach over the others. With that being the case, I think I'd be more inclined to omit this functionality for now and wait to see whether anybody comes along who has a specific need for this and see what their requirements are. Would you mind removing the Unit struct support and perhaps we can open an issue to talk about its use case in the future?

@mkawalec
Copy link
Contributor Author

Sure, that's also a valid approach. I'll remove the unit struct support today and replace it with an error message telling the user to reach out if they need it.

@nicholastmosher
Copy link
Contributor

That sounds awesome, thanks!

Copy link
Contributor

@nicholastmosher nicholastmosher 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 making those changes, I think everything looks good now! @sehz what do you think?

Copy link
Collaborator

@sehz sehz left a comment

Choose a reason for hiding this comment

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

LGTM. Couple of minor housekeeping:

  • Bump up versions for effected crates
  • Add to changelog
  • CI failures

@@ -34,12 +34,19 @@ impl TryIntoJs for i32 {
js_env.create_int32(self)
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you bump up version for nj-core?

@sehz
Copy link
Collaborator

sehz commented Apr 30, 2021

rebase as well

@mkawalec
Copy link
Contributor Author

So the rebase went weirdly... It looks like it still does what it is supposed to do though, I highly recommend squash merge over rebasing for that exact reason.

CHANGELOG.md Show resolved Hide resolved
@sehz
Copy link
Collaborator

sehz commented May 3, 2021

Great work. Thanks for contribution.

@sehz sehz merged commit 2802b84 into infinyon:master May 3, 2021
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