Skip to content

Derive Generic FromSql/ToSql #931

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

Merged
merged 2 commits into from
Sep 7, 2022
Merged

Conversation

matt-duch
Copy link
Contributor

Allows for derivation of FromSql and ToSql for generic types. Borrowed types are allowed for ToSql, but not meaningful to support for FromSql (so not supported). (also noted in #570)

Would you be interested in this change? A test was added. Happy to iterate.

Comment on lines 245 to 247
struct InventoryItem<T: FromSqlOwned, U>
where
U: FromSqlOwned,
Copy link
Owner

Choose a reason for hiding this comment

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

Are these bounds actually required?

Copy link
Contributor Author

@matt-duch matt-duch Aug 9, 2022

Choose a reason for hiding this comment

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

FromSqlOwned is required for #[derive(FromSql)] which is needed to run the test. Or were you asking about the derived Debug and PartialEq?

Edit: I also put the bounds in the two different positions to test that bounds and where clauses work as expected.

Copy link
Owner

Choose a reason for hiding this comment

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

Derive logic should normally insert those bounds on the generated impls as necessary - you don't need where T: Clone at the struct definition when using #[derive(Clone)] for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to make sure I understand the change request, the idea is add a FromSql<'a> bound on any generic type in the FromSql derive logic?

Copy link
Owner

Choose a reason for hiding this comment

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

Yep.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I started implementing this, and ran into an ambiguity (where this differs from Clone, which is in the rust prelude). I need to create a path to the FromSql and ToSql traits, however it could be one of a few options:

  1. From the postgres_derive crate (postgres_derive::FromSql<'a, T>, but I think most uses will not import this crate directly)
  2. From the postgres_types crate (postgres_types::FromSql<'a, T>)
  3. Some other crate that re-exports or renames those types (no way to use a path from a crate name)
  4. Assume that it's used somewhere (FromSql<'a, T>)
    Any suggestions on which assumption to use?

Copy link
Owner

Choose a reason for hiding this comment

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

Here's how the path is already produced in the existing logic: https://github.com/sfackler/rust-postgres/blob/master/postgres-derive/src/fromsql.rs#L90

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing that out. Based on that, looks like it was option (2). Branch is updated, and tests pass locally.

@matt-duch
Copy link
Contributor Author

Updated to fix the two issues (lint and failing test).

@AngusWaller
Copy link

AngusWaller commented Sep 1, 2022

Hope this gets in soon, derive is breaking on any struct thats borrowed/lifetimes

@sfackler
Copy link
Owner

sfackler commented Sep 6, 2022

Sorry for the delay - LGTM other than the clippy failures!

@matt-duch
Copy link
Contributor Author

@sfackler Sorry about that. Fixed, clippy runs locally, and pushed.

@sfackler sfackler merged commit 1e944c4 into sfackler:master Sep 7, 2022
@sfackler
Copy link
Owner

sfackler commented Sep 7, 2022

Thanks! I will cut a release tomorrow.

@matt-duch matt-duch deleted the derive-generic branch September 7, 2022 14:05
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