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

Constrain cyclic associated types to themselves #2702

Merged
merged 1 commit into from
Mar 14, 2024

Conversation

BadBastion
Copy link
Contributor

The Database trait's associated types are constrained using Connection<Database = Self> but this constraint is not specified for the reverse Database<Connection = Self>. This causes issues when wrapping or "extending" sqlx traits since as far as rust's type system is concerned Self::Connection::Database::Connection != Self::Connection.

Consider the following example:

type DatabaseTypeOf<T> = <<T as ConnectOptions>::Connection as Connection>::Database;

pub trait OptionWrapper {
    type Pool: Pool;
    async fn connect(self) -> Self::Pool;
}

impl <T> OptionWrapper for T
where
    T: ConnectOptions,
    for<'a> &'a mut T::Connection: Executor<'a, Database = DatabaseTypeOf<T>>,
{
    type Client = Pool<DatabaseTypeOf<T>>;

    async fn connect(self) -> Self::Pool {
        Pool::connect_with(self).await.unwrap()
        //    ^^^^^^^^^^^^^^^^^^ expected associated type, found type parameter `T`
        // type mismatch resolving `<<<<T as ConnectOptions>::Connection as Connection>::Database as Database>::Connection as Connection>::Options == T`
    }
}

This PR adds bounds such that cyclic associated types are equal to themselves.

<T as Connection>::Database as Database>::Connection == T
<T as ConnectOptions>::Connection as Connection>::Options == T
<T as Row>::Database as Database>::Row == T
<T as Column>::Database as Database>::Column == T
<T as Value>::Database as Database>::Value == T

Add bounds such that cyclic associated types are equal to themselves.
```
<T as Connection>::Database as Database>::Connection == T
<T as ConnectOptions>::Connection as Connection>::Options == T
<T as Row>::Database as Database>::Row == T
<T as Column>::Database as Database>::Column == T
<T as Value>::Database as Database>::Value == T
```
@jplatte
Copy link
Contributor

jplatte commented Aug 22, 2023

I guess technically this is a breaking change, but I agree that it makes a lot of sense. 👍🏼

@abonander abonander merged commit 0d0dddf into launchbadge:main Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants