Skip to content

refactor(core): Remove lifetime parameter from Arguments trait #3960

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

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ disallowed_methods = 'deny'
level = 'warn'
check-cfg = [
'cfg(mariadb, values(any()))',
'cfg(postgres_12)',
'cfg(postgres_13)',
'cfg(postgres_14)',
'cfg(postgres_15)',
'cfg(sqlite_ipaddr)',
'cfg(sqlite_test_sqlcipher)',
]
Expand Down
40 changes: 18 additions & 22 deletions sqlx-core/src/any/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ use crate::arguments::Arguments;
use crate::encode::{Encode, IsNull};
use crate::error::BoxDynError;
use crate::types::Type;
use std::sync::Arc;

pub struct AnyArguments<'q> {
#[derive(Default)]
pub struct AnyArguments {
#[doc(hidden)]
pub values: AnyArgumentBuffer<'q>,
pub values: AnyArgumentBuffer,
}

impl<'q> Arguments<'q> for AnyArguments<'q> {
impl Arguments for AnyArguments {
type Database = Any;

fn reserve(&mut self, additional: usize, _size: usize) {
self.values.0.reserve(additional);
}

fn add<T>(&mut self, value: T) -> Result<(), BoxDynError>
fn add<'t, T>(&mut self, value: T) -> Result<(), BoxDynError>
where
T: 'q + Encode<'q, Self::Database> + Type<Self::Database>,
T: Encode<'t, Self::Database> + Type<Self::Database>,
{
let _: IsNull = value.encode(&mut self.values)?;
Ok(())
Expand All @@ -30,21 +32,13 @@ impl<'q> Arguments<'q> for AnyArguments<'q> {
}
}

pub struct AnyArgumentBuffer<'q>(#[doc(hidden)] pub Vec<AnyValueKind<'q>>);
#[derive(Default)]
pub struct AnyArgumentBuffer(#[doc(hidden)] pub Vec<AnyValueKind>);

impl Default for AnyArguments<'_> {
fn default() -> Self {
AnyArguments {
values: AnyArgumentBuffer(vec![]),
}
}
}

impl<'q> AnyArguments<'q> {
impl AnyArguments {
#[doc(hidden)]
pub fn convert_to<'a, A: Arguments<'a>>(&'a self) -> Result<A, BoxDynError>
pub fn convert_into<'a, A: Arguments>(self) -> Result<A, BoxDynError>
where
'q: 'a,
Option<i32>: Type<A::Database> + Encode<'a, A::Database>,
Option<bool>: Type<A::Database> + Encode<'a, A::Database>,
Option<i16>: Type<A::Database> + Encode<'a, A::Database>,
Expand All @@ -60,12 +54,13 @@ impl<'q> AnyArguments<'q> {
i64: Type<A::Database> + Encode<'a, A::Database>,
f32: Type<A::Database> + Encode<'a, A::Database>,
f64: Type<A::Database> + Encode<'a, A::Database>,
&'a str: Type<A::Database> + Encode<'a, A::Database>,
&'a [u8]: Type<A::Database> + Encode<'a, A::Database>,
Arc<String>: Type<A::Database> + Encode<'a, A::Database>,
Arc<str>: Type<A::Database> + Encode<'a, A::Database>,
Arc<Vec<u8>>: Type<A::Database> + Encode<'a, A::Database>,
{
let mut out = A::default();

for arg in &self.values.0 {
for arg in self.values.0 {
match arg {
AnyValueKind::Null(AnyTypeInfoKind::Null) => out.add(Option::<i32>::None),
AnyValueKind::Null(AnyTypeInfoKind::Bool) => out.add(Option::<bool>::None),
Expand All @@ -82,8 +77,9 @@ impl<'q> AnyArguments<'q> {
AnyValueKind::BigInt(i) => out.add(i),
AnyValueKind::Real(r) => out.add(r),
AnyValueKind::Double(d) => out.add(d),
AnyValueKind::Text(t) => out.add(&**t),
AnyValueKind::Blob(b) => out.add(&**b),
AnyValueKind::Text(t) => out.add(t),
AnyValueKind::TextSlice(t) => out.add(t),
AnyValueKind::Blob(b) => out.add(b),
}?
}
Ok(out)
Expand Down
16 changes: 8 additions & 8 deletions sqlx-core/src/any/connection/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,19 @@ pub trait AnyConnectionBackend: std::any::Any + Debug + Send + 'static {
))
}

fn fetch_many<'q>(
&'q mut self,
fn fetch_many(
&mut self,
query: SqlStr,
persistent: bool,
arguments: Option<AnyArguments<'q>>,
) -> BoxStream<'q, crate::Result<Either<AnyQueryResult, AnyRow>>>;
arguments: Option<AnyArguments>,
) -> BoxStream<'_, crate::Result<Either<AnyQueryResult, AnyRow>>>;

fn fetch_optional<'q>(
&'q mut self,
fn fetch_optional(
&mut self,
query: SqlStr,
persistent: bool,
arguments: Option<AnyArguments<'q>>,
) -> BoxFuture<'q, crate::Result<Option<AnyRow>>>;
arguments: Option<AnyArguments>,
) -> BoxFuture<'_, crate::Result<Option<AnyRow>>>;

fn prepare_with<'c, 'q: 'c>(
&'c mut self,
Expand Down
4 changes: 2 additions & 2 deletions sqlx-core/src/any/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ impl Database for Any {
type Value = AnyValue;
type ValueRef<'r> = AnyValueRef<'r>;

type Arguments<'q> = AnyArguments<'q>;
type ArgumentBuffer<'q> = AnyArgumentBuffer<'q>;
type Arguments = AnyArguments;
type ArgumentBuffer = AnyArgumentBuffer;

type Statement = AnyStatement;

Expand Down
4 changes: 2 additions & 2 deletions sqlx-core/src/any/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub trait AnyExecutor<'c>: Executor<'c, Database = Any> {}
impl<'c, T: Executor<'c, Database = Any>> AnyExecutor<'c> for T {}

// NOTE: required due to the lack of lazy normalization
impl_into_arguments_for_arguments!(AnyArguments<'q>);
impl_into_arguments_for_arguments!(AnyArguments);
// impl_executor_for_pool_connection!(Any, AnyConnection, AnyRow);
// impl_executor_for_transaction!(Any, AnyRow);
impl_acquire!(Any, AnyConnection);
Expand All @@ -71,7 +71,7 @@ where
{
fn encode_by_ref(
&self,
buf: &mut AnyArgumentBuffer<'q>,
buf: &mut AnyArgumentBuffer,
) -> Result<crate::encode::IsNull, crate::error::BoxDynError> {
if let Some(value) = self {
value.encode_by_ref(buf)
Expand Down
2 changes: 1 addition & 1 deletion sqlx-core/src/any/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Statement for AnyStatement {
&self.columns
}

impl_statement_query!(AnyArguments<'_>);
impl_statement_query!(AnyArguments);
}

impl ColumnIndex<AnyStatement> for &'_ str {
Expand Down
21 changes: 8 additions & 13 deletions sqlx-core/src/any/types/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::decode::Decode;
use crate::encode::{Encode, IsNull};
use crate::error::BoxDynError;
use crate::types::Type;
use std::borrow::Cow;
use std::sync::Arc;

impl Type<Any> for [u8] {
fn type_info() -> AnyTypeInfo {
Expand All @@ -17,22 +17,17 @@ impl Type<Any> for [u8] {
impl<'q> Encode<'q, Any> for &'q [u8] {
fn encode_by_ref(
&self,
buf: &mut <Any as Database>::ArgumentBuffer<'q>,
buf: &mut <Any as Database>::ArgumentBuffer,
) -> Result<IsNull, BoxDynError> {
buf.0.push(AnyValueKind::Blob((*self).into()));
buf.0.push(AnyValueKind::Blob(Arc::new(self.to_vec())));
Ok(IsNull::No)
}
}

impl<'r> Decode<'r, Any> for &'r [u8] {
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
match value.kind {
AnyValueKind::Blob(Cow::Borrowed(blob)) => Ok(blob),
// This shouldn't happen in practice, it means the user got an `AnyValueRef`
// constructed from an owned `Vec<u8>` which shouldn't be allowed by the API.
AnyValueKind::Blob(Cow::Owned(_text)) => {
panic!("attempting to return a borrow that outlives its buffer")
}
AnyValueKind::Blob(blob) => Ok(blob.as_slice()),
other => other.unexpected(),
}
}
Expand All @@ -44,20 +39,20 @@ impl Type<Any> for Vec<u8> {
}
}

impl<'q> Encode<'q, Any> for Vec<u8> {
impl Encode<'_, Any> for Vec<u8> {
fn encode_by_ref(
&self,
buf: &mut <Any as Database>::ArgumentBuffer<'q>,
buf: &mut <Any as Database>::ArgumentBuffer,
) -> Result<IsNull, BoxDynError> {
buf.0.push(AnyValueKind::Blob(Cow::Owned(self.clone())));
buf.0.push(AnyValueKind::Blob(Arc::new(self.clone())));
Ok(IsNull::No)
}
}

impl<'r> Decode<'r, Any> for Vec<u8> {
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
match value.kind {
AnyValueKind::Blob(blob) => Ok(blob.into_owned()),
AnyValueKind::Blob(blob) => Ok(blob.as_ref().clone()),
other => other.unexpected(),
}
}
Expand Down
6 changes: 3 additions & 3 deletions sqlx-core/src/any/types/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ impl Type<Any> for bool {
}
}

impl<'q> Encode<'q, Any> for bool {
impl Encode<'_, Any> for bool {
fn encode_by_ref(
&self,
buf: &mut <Any as Database>::ArgumentBuffer<'q>,
buf: &mut <Any as Database>::ArgumentBuffer,
) -> Result<IsNull, BoxDynError> {
buf.0.push(AnyValueKind::Bool(*self));
Ok(IsNull::No)
Expand All @@ -26,7 +26,7 @@ impl<'q> Encode<'q, Any> for bool {
impl<'r> Decode<'r, Any> for bool {
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
match value.kind {
AnyValueKind::Bool(b) => Ok(b),
AnyValueKind::Bool(b) => Ok(*b),
other => other.unexpected(),
}
}
Expand Down
14 changes: 7 additions & 7 deletions sqlx-core/src/any/types/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ impl Type<Any> for f32 {
}
}

impl<'q> Encode<'q, Any> for f32 {
fn encode_by_ref(&self, buf: &mut AnyArgumentBuffer<'q>) -> Result<IsNull, BoxDynError> {
impl Encode<'_, Any> for f32 {
fn encode_by_ref(&self, buf: &mut AnyArgumentBuffer) -> Result<IsNull, BoxDynError> {
buf.0.push(AnyValueKind::Real(*self));
Ok(IsNull::No)
}
Expand All @@ -23,7 +23,7 @@ impl<'q> Encode<'q, Any> for f32 {
impl<'r> Decode<'r, Any> for f32 {
fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError> {
match value.kind {
AnyValueKind::Real(r) => Ok(r),
AnyValueKind::Real(r) => Ok(*r),
other => other.unexpected(),
}
}
Expand All @@ -37,10 +37,10 @@ impl Type<Any> for f64 {
}
}

impl<'q> Encode<'q, Any> for f64 {
impl Encode<'_, Any> for f64 {
fn encode_by_ref(
&self,
buf: &mut <Any as Database>::ArgumentBuffer<'q>,
buf: &mut <Any as Database>::ArgumentBuffer,
) -> Result<IsNull, BoxDynError> {
buf.0.push(AnyValueKind::Double(*self));
Ok(IsNull::No)
Expand All @@ -51,8 +51,8 @@ impl<'r> Decode<'r, Any> for f64 {
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
match value.kind {
// Widening is safe
AnyValueKind::Real(r) => Ok(r as f64),
AnyValueKind::Double(d) => Ok(d),
AnyValueKind::Real(r) => Ok(*r as f64),
AnyValueKind::Double(d) => Ok(*d),
other => other.unexpected(),
}
}
Expand Down
12 changes: 6 additions & 6 deletions sqlx-core/src/any/types/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ impl Type<Any> for i16 {
}
}

impl<'q> Encode<'q, Any> for i16 {
impl Encode<'_, Any> for i16 {
fn encode_by_ref(
&self,
buf: &mut <Any as Database>::ArgumentBuffer<'q>,
buf: &mut <Any as Database>::ArgumentBuffer,
) -> Result<IsNull, BoxDynError> {
buf.0.push(AnyValueKind::SmallInt(*self));
Ok(IsNull::No)
Expand All @@ -45,10 +45,10 @@ impl Type<Any> for i32 {
}
}

impl<'q> Encode<'q, Any> for i32 {
impl Encode<'_, Any> for i32 {
fn encode_by_ref(
&self,
buf: &mut <Any as Database>::ArgumentBuffer<'q>,
buf: &mut <Any as Database>::ArgumentBuffer,
) -> Result<IsNull, BoxDynError> {
buf.0.push(AnyValueKind::Integer(*self));
Ok(IsNull::No)
Expand All @@ -73,10 +73,10 @@ impl Type<Any> for i64 {
}
}

impl<'q> Encode<'q, Any> for i64 {
impl Encode<'_, Any> for i64 {
fn encode_by_ref(
&self,
buf: &mut <Any as Database>::ArgumentBuffer<'q>,
buf: &mut <Any as Database>::ArgumentBuffer,
) -> Result<IsNull, BoxDynError> {
buf.0.push(AnyValueKind::BigInt(*self));
Ok(IsNull::No)
Expand Down
Loading
Loading