Skip to content

Commit 9ba488c

Browse files
nitn3lavabonander
andauthored
Generic Associated Types in Database, replacing HasValueRef, HasArguments, HasStatement (#2973)
* HasValueRef, HasArguments, HasStatement -> Database GATs replace the associated types from the generic traits `HasValueRef<'r>`, `HasArguments<'q>` and `HasStatement<'q>` with generic associated types in `Database` * fixup after rebase --------- Co-authored-by: Austin Bonander <[email protected]>
1 parent 936744d commit 9ba488c

33 files changed

+171
-282
lines changed

sqlx-core/src/any/database.rs

+7-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::any::{
22
AnyArgumentBuffer, AnyArguments, AnyColumn, AnyConnection, AnyQueryResult, AnyRow,
33
AnyStatement, AnyTransactionManager, AnyTypeInfo, AnyValue, AnyValueRef,
44
};
5-
use crate::database::{Database, HasArguments, HasStatement, HasStatementCache, HasValueRef};
5+
use crate::database::{Database, HasStatementCache};
66

77
/// Opaque database driver. Capable of being used in place of any SQLx database driver. The actual
88
/// driver used will be selected at runtime, from the connection url.
@@ -23,29 +23,16 @@ impl Database for Any {
2323
type TypeInfo = AnyTypeInfo;
2424

2525
type Value = AnyValue;
26-
const NAME: &'static str = "Any";
27-
28-
const URL_SCHEMES: &'static [&'static str] = &[];
29-
}
30-
31-
impl<'r> HasValueRef<'r> for Any {
32-
type Database = Any;
26+
type ValueRef<'r> = AnyValueRef<'r>;
3327

34-
type ValueRef = AnyValueRef<'r>;
35-
}
28+
type Arguments<'q> = AnyArguments<'q>;
29+
type ArgumentBuffer<'q> = AnyArgumentBuffer<'q>;
3630

37-
impl<'q> HasStatement<'q> for Any {
38-
type Database = Any;
31+
type Statement<'q> = AnyStatement<'q>;
3932

40-
type Statement = AnyStatement<'q>;
41-
}
42-
43-
impl<'q> HasArguments<'q> for Any {
44-
type Database = Any;
45-
46-
type Arguments = AnyArguments<'q>;
33+
const NAME: &'static str = "Any";
4734

48-
type ArgumentBuffer = AnyArgumentBuffer<'q>;
35+
const URL_SCHEMES: &'static [&'static str] = &[];
4936
}
5037

5138
// This _may_ be true, depending on the selected database

sqlx-core/src/any/row.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::any::error::mismatched_types;
22
use crate::any::{Any, AnyColumn, AnyTypeInfo, AnyTypeInfoKind, AnyValue, AnyValueKind};
33
use crate::column::{Column, ColumnIndex};
4-
use crate::database::{Database, HasValueRef};
4+
use crate::database::Database;
55
use crate::decode::Decode;
66
use crate::error::Error;
77
use crate::ext::ustr::UStr;
@@ -28,10 +28,7 @@ impl Row for AnyRow {
2828
&self.columns
2929
}
3030

31-
fn try_get_raw<I>(
32-
&self,
33-
index: I,
34-
) -> Result<<Self::Database as HasValueRef<'_>>::ValueRef, Error>
31+
fn try_get_raw<I>(&self, index: I) -> Result<<Self::Database as Database>::ValueRef<'_>, Error>
3532
where
3633
I: ColumnIndex<Self>,
3734
{
@@ -141,7 +138,7 @@ impl AnyRow {
141138
}
142139

143140
fn decode<'r, DB: Database, T: Decode<'r, DB>>(
144-
valueref: <DB as HasValueRef<'r>>::ValueRef,
141+
valueref: <DB as Database>::ValueRef<'r>,
145142
) -> crate::Result<T> {
146143
Decode::decode(valueref).map_err(Error::decode)
147144
}

sqlx-core/src/any/types/blob.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::any::{Any, AnyTypeInfo, AnyTypeInfoKind, AnyValueKind};
2-
use crate::database::{HasArguments, HasValueRef};
2+
use crate::database::Database;
33
use crate::decode::Decode;
44
use crate::encode::{Encode, IsNull};
55
use crate::error::BoxDynError;
@@ -15,14 +15,14 @@ impl Type<Any> for [u8] {
1515
}
1616

1717
impl<'q> Encode<'q, Any> for &'q [u8] {
18-
fn encode_by_ref(&self, buf: &mut <Any as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
18+
fn encode_by_ref(&self, buf: &mut <Any as Database>::ArgumentBuffer<'q>) -> IsNull {
1919
buf.0.push(AnyValueKind::Blob((*self).into()));
2020
IsNull::No
2121
}
2222
}
2323

2424
impl<'r> Decode<'r, Any> for &'r [u8] {
25-
fn decode(value: <Any as HasValueRef<'r>>::ValueRef) -> Result<Self, BoxDynError> {
25+
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
2626
match value.kind {
2727
AnyValueKind::Blob(Cow::Borrowed(blob)) => Ok(blob),
2828
// This shouldn't happen in practice, it means the user got an `AnyValueRef`
@@ -42,14 +42,14 @@ impl Type<Any> for Vec<u8> {
4242
}
4343

4444
impl<'q> Encode<'q, Any> for Vec<u8> {
45-
fn encode_by_ref(&self, buf: &mut <Any as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
45+
fn encode_by_ref(&self, buf: &mut <Any as Database>::ArgumentBuffer<'q>) -> IsNull {
4646
buf.0.push(AnyValueKind::Blob(Cow::Owned(self.clone())));
4747
IsNull::No
4848
}
4949
}
5050

5151
impl<'r> Decode<'r, Any> for Vec<u8> {
52-
fn decode(value: <Any as HasValueRef<'r>>::ValueRef) -> Result<Self, BoxDynError> {
52+
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
5353
match value.kind {
5454
AnyValueKind::Blob(blob) => Ok(blob.into_owned()),
5555
other => other.unexpected(),

sqlx-core/src/any/types/bool.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::any::{Any, AnyTypeInfo, AnyTypeInfoKind, AnyValueKind};
2-
use crate::database::{HasArguments, HasValueRef};
2+
use crate::database::Database;
33
use crate::decode::Decode;
44
use crate::encode::{Encode, IsNull};
55
use crate::error::BoxDynError;
@@ -14,14 +14,14 @@ impl Type<Any> for bool {
1414
}
1515

1616
impl<'q> Encode<'q, Any> for bool {
17-
fn encode_by_ref(&self, buf: &mut <Any as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
17+
fn encode_by_ref(&self, buf: &mut <Any as Database>::ArgumentBuffer<'q>) -> IsNull {
1818
buf.0.push(AnyValueKind::Bool(*self));
1919
IsNull::No
2020
}
2121
}
2222

2323
impl<'r> Decode<'r, Any> for bool {
24-
fn decode(value: <Any as HasValueRef<'r>>::ValueRef) -> Result<Self, BoxDynError> {
24+
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
2525
match value.kind {
2626
AnyValueKind::Bool(b) => Ok(b),
2727
other => other.unexpected(),

sqlx-core/src/any/types/float.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::any::{Any, AnyArgumentBuffer, AnyTypeInfo, AnyTypeInfoKind, AnyValueKind, AnyValueRef};
2-
use crate::database::{HasArguments, HasValueRef};
2+
use crate::database::Database;
33
use crate::decode::Decode;
44
use crate::encode::{Encode, IsNull};
55
use crate::error::BoxDynError;
@@ -38,14 +38,14 @@ impl Type<Any> for f64 {
3838
}
3939

4040
impl<'q> Encode<'q, Any> for f64 {
41-
fn encode_by_ref(&self, buf: &mut <Any as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
41+
fn encode_by_ref(&self, buf: &mut <Any as Database>::ArgumentBuffer<'q>) -> IsNull {
4242
buf.0.push(AnyValueKind::Double(*self));
4343
IsNull::No
4444
}
4545
}
4646

4747
impl<'r> Decode<'r, Any> for f64 {
48-
fn decode(value: <Any as HasValueRef<'r>>::ValueRef) -> Result<Self, BoxDynError> {
48+
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
4949
match value.kind {
5050
// Widening is safe
5151
AnyValueKind::Real(r) => Ok(r as f64),

sqlx-core/src/any/types/int.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::any::{Any, AnyTypeInfo, AnyTypeInfoKind, AnyValueKind};
2-
use crate::database::{HasArguments, HasValueRef};
2+
use crate::database::Database;
33
use crate::decode::Decode;
44
use crate::encode::{Encode, IsNull};
55
use crate::error::BoxDynError;
@@ -18,14 +18,14 @@ impl Type<Any> for i16 {
1818
}
1919

2020
impl<'q> Encode<'q, Any> for i16 {
21-
fn encode_by_ref(&self, buf: &mut <Any as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
21+
fn encode_by_ref(&self, buf: &mut <Any as Database>::ArgumentBuffer<'q>) -> IsNull {
2222
buf.0.push(AnyValueKind::SmallInt(*self));
2323
IsNull::No
2424
}
2525
}
2626

2727
impl<'r> Decode<'r, Any> for i16 {
28-
fn decode(value: <Any as HasValueRef<'r>>::ValueRef) -> Result<Self, BoxDynError> {
28+
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
2929
value.kind.try_integer()
3030
}
3131
}
@@ -43,14 +43,14 @@ impl Type<Any> for i32 {
4343
}
4444

4545
impl<'q> Encode<'q, Any> for i32 {
46-
fn encode_by_ref(&self, buf: &mut <Any as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
46+
fn encode_by_ref(&self, buf: &mut <Any as Database>::ArgumentBuffer<'q>) -> IsNull {
4747
buf.0.push(AnyValueKind::Integer(*self));
4848
IsNull::No
4949
}
5050
}
5151

5252
impl<'r> Decode<'r, Any> for i32 {
53-
fn decode(value: <Any as HasValueRef<'r>>::ValueRef) -> Result<Self, BoxDynError> {
53+
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
5454
value.kind.try_integer()
5555
}
5656
}
@@ -68,14 +68,14 @@ impl Type<Any> for i64 {
6868
}
6969

7070
impl<'q> Encode<'q, Any> for i64 {
71-
fn encode_by_ref(&self, buf: &mut <Any as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
71+
fn encode_by_ref(&self, buf: &mut <Any as Database>::ArgumentBuffer<'q>) -> IsNull {
7272
buf.0.push(AnyValueKind::BigInt(*self));
7373
IsNull::No
7474
}
7575
}
7676

7777
impl<'r> Decode<'r, Any> for i64 {
78-
fn decode(value: <Any as HasValueRef<'r>>::ValueRef) -> Result<Self, BoxDynError> {
78+
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
7979
value.kind.try_integer()
8080
}
8181
}

sqlx-core/src/any/types/str.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::any::types::str;
22
use crate::any::{Any, AnyTypeInfo, AnyTypeInfoKind, AnyValueKind};
3-
use crate::database::{HasArguments, HasValueRef};
3+
use crate::database::Database;
44
use crate::decode::Decode;
55
use crate::encode::{Encode, IsNull};
66
use crate::error::BoxDynError;
@@ -16,21 +16,21 @@ impl Type<Any> for str {
1616
}
1717

1818
impl<'a> Encode<'a, Any> for &'a str {
19-
fn encode(self, buf: &mut <Any as HasArguments<'a>>::ArgumentBuffer) -> IsNull
19+
fn encode(self, buf: &mut <Any as Database>::ArgumentBuffer<'a>) -> IsNull
2020
where
2121
Self: Sized,
2222
{
2323
buf.0.push(AnyValueKind::Text(self.into()));
2424
IsNull::No
2525
}
2626

27-
fn encode_by_ref(&self, buf: &mut <Any as HasArguments<'a>>::ArgumentBuffer) -> IsNull {
27+
fn encode_by_ref(&self, buf: &mut <Any as Database>::ArgumentBuffer<'a>) -> IsNull {
2828
(*self).encode(buf)
2929
}
3030
}
3131

3232
impl<'a> Decode<'a, Any> for &'a str {
33-
fn decode(value: <Any as HasValueRef<'a>>::ValueRef) -> Result<Self, BoxDynError> {
33+
fn decode(value: <Any as Database>::ValueRef<'a>) -> Result<Self, BoxDynError> {
3434
match value.kind {
3535
AnyValueKind::Text(Cow::Borrowed(text)) => Ok(text),
3636
// This shouldn't happen in practice, it means the user got an `AnyValueRef`
@@ -50,14 +50,14 @@ impl Type<Any> for String {
5050
}
5151

5252
impl<'q> Encode<'q, Any> for String {
53-
fn encode_by_ref(&self, buf: &mut <Any as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
53+
fn encode_by_ref(&self, buf: &mut <Any as Database>::ArgumentBuffer<'q>) -> IsNull {
5454
buf.0.push(AnyValueKind::Text(Cow::Owned(self.clone())));
5555
IsNull::No
5656
}
5757
}
5858

5959
impl<'r> Decode<'r, Any> for String {
60-
fn decode(value: <Any as HasValueRef<'r>>::ValueRef) -> Result<Self, BoxDynError> {
60+
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
6161
match value.kind {
6262
AnyValueKind::Text(text) => Ok(text.into_owned()),
6363
other => other.unexpected(),

sqlx-core/src/any/value.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::borrow::Cow;
22

33
use crate::any::{Any, AnyTypeInfo, AnyTypeInfoKind};
4-
use crate::database::{Database, HasValueRef};
4+
use crate::database::Database;
55
use crate::error::BoxDynError;
66
use crate::types::Type;
77
use crate::value::{Value, ValueRef};
@@ -71,7 +71,7 @@ pub struct AnyValueRef<'a> {
7171
impl Value for AnyValue {
7272
type Database = Any;
7373

74-
fn as_ref(&self) -> <Self::Database as HasValueRef<'_>>::ValueRef {
74+
fn as_ref(&self) -> <Self::Database as Database>::ValueRef<'_> {
7575
AnyValueRef {
7676
kind: match &self.kind {
7777
AnyValueKind::Null => AnyValueKind::Null,

sqlx-core/src/arguments.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Types and traits for passing arguments to SQL queries.
22
3-
use crate::database::{Database, HasArguments};
3+
use crate::database::Database;
44
use crate::encode::Encode;
55
use crate::types::Type;
66
use std::fmt::{self, Write};
@@ -23,8 +23,8 @@ pub trait Arguments<'q>: Send + Sized + Default {
2323
}
2424
}
2525

26-
pub trait IntoArguments<'q, DB: HasArguments<'q>>: Sized + Send {
27-
fn into_arguments(self) -> <DB as HasArguments<'q>>::Arguments;
26+
pub trait IntoArguments<'q, DB: Database>: Sized + Send {
27+
fn into_arguments(self) -> <DB as Database>::Arguments<'q>;
2828
}
2929

3030
// NOTE: required due to lack of lazy normalization
@@ -45,10 +45,10 @@ macro_rules! impl_into_arguments_for_arguments {
4545
}
4646

4747
/// used by the query macros to prevent supernumerary `.bind()` calls
48-
pub struct ImmutableArguments<'q, DB: HasArguments<'q>>(pub <DB as HasArguments<'q>>::Arguments);
48+
pub struct ImmutableArguments<'q, DB: Database>(pub <DB as Database>::Arguments<'q>);
4949

50-
impl<'q, DB: HasArguments<'q>> IntoArguments<'q, DB> for ImmutableArguments<'q, DB> {
51-
fn into_arguments(self) -> <DB as HasArguments<'q>>::Arguments {
50+
impl<'q, DB: Database> IntoArguments<'q, DB> for ImmutableArguments<'q, DB> {
51+
fn into_arguments(self) -> <DB as Database>::Arguments<'q> {
5252
self.0
5353
}
5454
}

0 commit comments

Comments
 (0)