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

Support generic structures in FromQueryResult #1463

Closed
leviska opened this issue Feb 8, 2023 · 3 comments · Fixed by #1464
Closed

Support generic structures in FromQueryResult #1463

leviska opened this issue Feb 8, 2023 · 3 comments · Fixed by #1464

Comments

@leviska
Copy link
Contributor

leviska commented Feb 8, 2023

Motivation

It seems that there should be no problem to write something like

#[derive(FromQueryResult)]
struct DateValue<T: TryGetable> {
  date: NaiveDate,
  value: T,
}

And then use it something like this

DateValue::<i32>::find_by_statement(..);
DateValue::<String>::find_by_statement(..);

I've tried doing that, and it seems like macro does not support it right now

error[E0107]: missing generics for struct `DateValue`
 --> stats/src/charts/insert.rs:6:12
  |
6 | pub struct DateValue<T: TryGetable> {
  |            ^^^^^^^^^ expected 1 generic argument
  |
note: struct defined here, with 1 generic parameter: `T`
 --> stats/src/charts/insert.rs:6:12
  |
6 | pub struct DateValue<T: TryGetable> {
  |            ^^^^^^^^^ -
help: add missing generic argument
  |
6 | pub struct DateValue<T><T: TryGetable> {
  |            ~~~~~~~~~~~~

Proposed Solutions

Add support to the macro

Even better would be is to add T: TryGetable in the impl<T: TryGetable> ... block, like rust does with Clone, Debug and etc, so it would require the type to implement trait only if somebody actually uses fromqueryresult functions

@tyt2y3
Copy link
Member

tyt2y3 commented Feb 23, 2023

It's a very interesting use case

@leviska
Copy link
Contributor Author

leviska commented Feb 23, 2023

Our use case is that we calculating different statistical values, and they could have different types (int, float or decimal).
The result type has the same structure - date and value.
So currently we have three structs, and I've came to idea to make it generic

@billy1624
Copy link
Member

Released in 0.11.3

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 a pull request may close this issue.

3 participants