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

execution-plan: Macro for implementing Value on structs #40

Merged
merged 2 commits into from
Dec 20, 2023

Conversation

adamchalmers
Copy link
Collaborator

@adamchalmers adamchalmers commented Dec 19, 2023

Background

The Value trait lets a Rust type be written to KittyCAD execution plan (KCEP) memory, and read back out of memory. The trait has two methods, into_parts() (convert your type into a vec of KCEP Primitive) and from_parts() (given a vec of KCEP Primitive, reconstruct your type).

Problem

You have to be careful that your into_parts() and from_parts() methods use the same order for the fields of your Rust struct. E.g. if into_parts() outputs a vec with self.length and self.name, then from_parts() must assume its input struct is self.length then self.name.

This is easy, but it's a manual process and a bit error-prone. Additionally, the implementation is pretty boilerplate -- the implementation of Value can easily be recursive if all your fields also impl Value.

Also, if I'm being honest, handwriting implementations of these traits for every single type in our Modeling API is going to be a pretty big timesink, and it's likely that at some point I lose focus and make a mistake, like putting the fields in the wrong order.

Solution

Write a macro which generates an impl of Value for a given type. If the human programmer supplies a list of all fields, we can ensure they're being used in the same order in both from_parts() and into_parts(). And this is actually type-checked -- Rust won't compile the code if you supply an invalid field!

TODO

  • This macro only works for structs. Make a similar macro for enums.

Copy link

codecov bot commented Dec 19, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

❗ No coverage uploaded for pull request base (main@3e218fa). Click here to learn what that means.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #40   +/-   ##
=======================================
  Coverage        ?   44.33%           
=======================================
  Files           ?       30           
  Lines           ?     1845           
  Branches        ?        0           
=======================================
  Hits            ?      818           
  Misses          ?     1027           
  Partials        ?        0           
Flag Coverage Δ
unittests 44.33% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@adamchalmers adamchalmers merged commit b34a5db into main Dec 20, 2023
8 checks passed
@adamchalmers adamchalmers deleted the achalmers/more-value-impls branch December 20, 2023 02:14
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.

1 participant