Skip to content

Commit

Permalink
Constrain cyclic associated types (#2702)
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
BadBastion authored Mar 14, 2024
1 parent 9ba488c commit 0d0dddf
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion sqlx-core/src/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::error::Error;
use std::fmt::Debug;

pub trait Column: 'static + Send + Sync + Debug {
type Database: Database;
type Database: Database<Column = Self>;

/// Gets the column ordinal.
///
Expand Down
4 changes: 2 additions & 2 deletions sqlx-core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use url::Url;

/// Represents a single database connection.
pub trait Connection: Send {
type Database: Database;
type Database: Database<Connection = Self>;

type Options: ConnectOptions<Connection = Self>;

Expand Down Expand Up @@ -184,7 +184,7 @@ impl LogSettings {
}

pub trait ConnectOptions: 'static + Send + Sync + FromStr<Err = Error> + Debug + Clone {
type Connection: Connection + ?Sized;
type Connection: Connection<Options = Self> + ?Sized;

/// Parse the `ConnectOptions` from a URL.
fn from_url(url: &Url) -> Result<Self, Error>;
Expand Down
2 changes: 1 addition & 1 deletion sqlx-core/src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::value::ValueRef;
/// [`FromRow`]: crate::row::FromRow
/// [`Query::fetch`]: crate::query::Query::fetch
pub trait Row: Unpin + Send + Sync + 'static {
type Database: Database;
type Database: Database<Row = Self>;

/// Returns `true` if this row has no columns.
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion sqlx-core/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::borrow::Cow;

/// An owned value from the database.
pub trait Value {
type Database: Database;
type Database: Database<Value = Self>;

/// Get this value as a reference.
fn as_ref(&self) -> <Self::Database as Database>::ValueRef<'_>;
Expand Down

0 comments on commit 0d0dddf

Please sign in to comment.