-
Notifications
You must be signed in to change notification settings - Fork 20
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(schema): support generic bounds in structs & enums #490
base: main
Are you sure you want to change the base?
Conversation
b1aa6d0
to
df3e274
Compare
Sorry I don't have time to do a proper review, I just want to throw a warning in here because I've messed this up when dealing with generics in the has schema derive: be very careful to make sure that each different instantiation of the generic type's schema returns it's own unique schema. There was a really tricky to debug issue once where generic enums like Sorry that is really hard to say in a way that makes sense. This is the PR that fixed the error I'm talking about: #456 Anyway, warning aside, big thanks for this! |
@zicklag thanks for weighing in. I think I ran into that issue once already 😆 My first push had some failing tests and it looked like it was because of the new shared That's the only thing I'm not 100% on, since the rest of the changes are essentially just fixing syntax errors in the derive macro code gen. |
@@ -366,38 +373,43 @@ pub fn derive_has_schema(input: TokenStream) -> TokenStream { | |||
}; | |||
|
|||
if let Some(generic_params) = input.generic_params() { | |||
let mut sync_send_generic_params = generic_params.clone(); | |||
for (param, _) in sync_send_generic_params.params.iter_mut() { | |||
let clone_bound = if !no_clone { quote!(+ Clone) } else { quote!() }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lmk if I'm missing anything - but I think we may have dropped the #[schema(no_clone]
(i.e. no_clone bool) support here, if this attribute is present, we should not be requiring clone. If reading this right - a no_clone type with generic params will now require clone on its generic types, which previously did not - might not compile with the right instantiation. (we may not have any test coverage or usage that would break in code I'm guessing).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really good catch. Fixed, and added a test that will fail to compile if the code gen is broken again.
Changes
HasSchema
derive macrostatic S: OnceLock<parking_lot::RwLock<HashMap<TypeId, &'static Schema>>>
for generics intobones_schema
so that user crates don't have to take a dependency onparking_lot
Given the following rust code:
The
HasSchema
derive macro used to generate:With errors:
Now it generates: