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

Improved Conversion traits on Table Primary Keys #21

Open
MinisculeGirraffe opened this issue Jan 14, 2024 · 1 comment
Open

Improved Conversion traits on Table Primary Keys #21

MinisculeGirraffe opened this issue Jan 14, 2024 · 1 comment

Comments

@MinisculeGirraffe
Copy link

MinisculeGirraffe commented Jan 14, 2024

This is a bit of a paper cut I noticed.

When defining an entity in the primary_key function, it performs a conversion from the KeyInput to the associated tables PrimaryKey.

In my code I really wanted to be able to do write a generic function that accepted any type that could be represented as that the primary key for the table associated with an entity.

fn example_signature<T: Entity>(input: impl Into<<<T as Entity>::Table as Table>::PrimaryKey>)

Instead of manually implementing the Into trait for my key inputs, it could just be implemented automatically for every type with the following trait bounds. But it would have to come from this library as the Entity and Table traits are defined here.

type KeyInput<'a, T: Entity> = <T as Entity>::KeyInput<'a>;
type PrimaryKey<T: Entity> = <<T as Entity>::Table as Table>::PrimaryKey;

impl<'a, T: Entity> From<KeyInput<'a, T>> for PrimaryKey<T> {
    fn from(value: <T as Entity>::KeyInput<'a>) -> Self {
        T::primary_key(value)
    }
}

The utility type definitions could also simplify working with that function. I can write them locally, but like. Someone else might have this problem I guess.

fn example_signature<T: Entity>(input: impl Into<PrimaryKey<T>>)

I can put in a PR if you're cool with this.

@neoeinstein
Copy link
Owner

I'm good with a PR for that. Thanks!

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

No branches or pull requests

2 participants