Skip to content

Commit babd18d

Browse files
committed
refactor(any): Remove lifetime parameter from AnyArguments
Signed-off-by: Joshua Potts <[email protected]>
1 parent 8bbbf1c commit babd18d

File tree

13 files changed

+88
-114
lines changed

13 files changed

+88
-114
lines changed

sqlx-core/src/any/arguments.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ use crate::arguments::Arguments;
44
use crate::encode::{Encode, IsNull};
55
use crate::error::BoxDynError;
66
use crate::types::Type;
7+
use std::sync::Arc;
78

8-
pub struct AnyArguments<'q> {
9+
#[derive(Default)]
10+
pub struct AnyArguments {
911
#[doc(hidden)]
10-
pub values: AnyArgumentBuffer<'q>,
12+
pub values: AnyArgumentBuffer,
1113
}
1214

13-
impl<'q> Arguments<'q> for AnyArguments<'q> {
15+
impl<'q> Arguments<'q> for AnyArguments {
1416
type Database = Any;
1517

1618
fn reserve(&mut self, additional: usize, _size: usize) {
@@ -30,21 +32,13 @@ impl<'q> Arguments<'q> for AnyArguments<'q> {
3032
}
3133
}
3234

33-
pub struct AnyArgumentBuffer<'q>(#[doc(hidden)] pub Vec<AnyValueKind<'q>>);
35+
#[derive(Default)]
36+
pub struct AnyArgumentBuffer(#[doc(hidden)] pub Vec<AnyValueKind>);
3437

35-
impl Default for AnyArguments<'_> {
36-
fn default() -> Self {
37-
AnyArguments {
38-
values: AnyArgumentBuffer(vec![]),
39-
}
40-
}
41-
}
42-
43-
impl<'q> AnyArguments<'q> {
38+
impl AnyArguments {
4439
#[doc(hidden)]
4540
pub fn convert_into<'a, A: Arguments<'a>>(self) -> Result<A, BoxDynError>
4641
where
47-
'q: 'a,
4842
Option<i32>: Type<A::Database> + Encode<'a, A::Database>,
4943
Option<bool>: Type<A::Database> + Encode<'a, A::Database>,
5044
Option<i16>: Type<A::Database> + Encode<'a, A::Database>,
@@ -60,8 +54,9 @@ impl<'q> AnyArguments<'q> {
6054
i64: Type<A::Database> + Encode<'a, A::Database>,
6155
f32: Type<A::Database> + Encode<'a, A::Database>,
6256
f64: Type<A::Database> + Encode<'a, A::Database>,
63-
String: Type<A::Database> + Encode<'a, A::Database>,
64-
Vec<u8>: Type<A::Database> + Encode<'a, A::Database>,
57+
Arc<String>: Type<A::Database> + Encode<'a, A::Database>,
58+
Arc<str>: Type<A::Database> + Encode<'a, A::Database>,
59+
Arc<Vec<u8>>: Type<A::Database> + Encode<'a, A::Database>,
6560
{
6661
let mut out = A::default();
6762

@@ -82,8 +77,9 @@ impl<'q> AnyArguments<'q> {
8277
AnyValueKind::BigInt(i) => out.add(i),
8378
AnyValueKind::Real(r) => out.add(r),
8479
AnyValueKind::Double(d) => out.add(d),
85-
AnyValueKind::Text(t) => out.add(String::from(t)),
86-
AnyValueKind::Blob(b) => out.add(Vec::from(b)),
80+
AnyValueKind::Text(t) => out.add(t),
81+
AnyValueKind::TextSlice(t) => out.add(t),
82+
AnyValueKind::Blob(b) => out.add(b),
8783
}?
8884
}
8985
Ok(out)

sqlx-core/src/any/connection/backend.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,19 @@ pub trait AnyConnectionBackend: std::any::Any + Debug + Send + 'static {
9494
))
9595
}
9696

97-
fn fetch_many<'q>(
98-
&'q mut self,
97+
fn fetch_many(
98+
&mut self,
9999
query: SqlStr,
100100
persistent: bool,
101-
arguments: Option<AnyArguments<'q>>,
102-
) -> BoxStream<'q, crate::Result<Either<AnyQueryResult, AnyRow>>>;
101+
arguments: Option<AnyArguments>,
102+
) -> BoxStream<'_, crate::Result<Either<AnyQueryResult, AnyRow>>>;
103103

104-
fn fetch_optional<'q>(
105-
&'q mut self,
104+
fn fetch_optional(
105+
&mut self,
106106
query: SqlStr,
107107
persistent: bool,
108-
arguments: Option<AnyArguments<'q>>,
109-
) -> BoxFuture<'q, crate::Result<Option<AnyRow>>>;
108+
arguments: Option<AnyArguments>,
109+
) -> BoxFuture<'_, crate::Result<Option<AnyRow>>>;
110110

111111
fn prepare_with<'c, 'q: 'c>(
112112
&'c mut self,

sqlx-core/src/any/database.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ impl Database for Any {
2525
type Value = AnyValue;
2626
type ValueRef<'r> = AnyValueRef<'r>;
2727

28-
type Arguments<'q> = AnyArguments<'q>;
29-
type ArgumentBuffer<'q> = AnyArgumentBuffer<'q>;
28+
type Arguments<'q> = AnyArguments;
29+
type ArgumentBuffer<'q> = AnyArgumentBuffer;
3030

3131
type Statement = AnyStatement;
3232

sqlx-core/src/any/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub trait AnyExecutor<'c>: Executor<'c, Database = Any> {}
5656
impl<'c, T: Executor<'c, Database = Any>> AnyExecutor<'c> for T {}
5757

5858
// NOTE: required due to the lack of lazy normalization
59-
impl_into_arguments_for_arguments!(AnyArguments<'q>);
59+
impl_into_arguments_for_arguments!(AnyArguments);
6060
// impl_executor_for_pool_connection!(Any, AnyConnection, AnyRow);
6161
// impl_executor_for_transaction!(Any, AnyRow);
6262
impl_acquire!(Any, AnyConnection);
@@ -71,7 +71,7 @@ where
7171
{
7272
fn encode_by_ref(
7373
&self,
74-
buf: &mut AnyArgumentBuffer<'q>,
74+
buf: &mut AnyArgumentBuffer,
7575
) -> Result<crate::encode::IsNull, crate::error::BoxDynError> {
7676
if let Some(value) = self {
7777
value.encode_by_ref(buf)

sqlx-core/src/any/statement.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl Statement for AnyStatement {
4444
&self.columns
4545
}
4646

47-
impl_statement_query!(AnyArguments<'_>);
47+
impl_statement_query!(AnyArguments);
4848
}
4949

5050
impl ColumnIndex<AnyStatement> for &'_ str {

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::decode::Decode;
44
use crate::encode::{Encode, IsNull};
55
use crate::error::BoxDynError;
66
use crate::types::Type;
7-
use std::borrow::Cow;
7+
use std::sync::Arc;
88

99
impl Type<Any> for [u8] {
1010
fn type_info() -> AnyTypeInfo {
@@ -19,20 +19,15 @@ impl<'q> Encode<'q, Any> for &'q [u8] {
1919
&self,
2020
buf: &mut <Any as Database>::ArgumentBuffer<'q>,
2121
) -> Result<IsNull, BoxDynError> {
22-
buf.0.push(AnyValueKind::Blob((*self).into()));
22+
buf.0.push(AnyValueKind::Blob(Arc::new(self.to_vec())));
2323
Ok(IsNull::No)
2424
}
2525
}
2626

2727
impl<'r> Decode<'r, Any> for &'r [u8] {
2828
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
2929
match value.kind {
30-
AnyValueKind::Blob(Cow::Borrowed(blob)) => Ok(blob),
31-
// This shouldn't happen in practice, it means the user got an `AnyValueRef`
32-
// constructed from an owned `Vec<u8>` which shouldn't be allowed by the API.
33-
AnyValueKind::Blob(Cow::Owned(_text)) => {
34-
panic!("attempting to return a borrow that outlives its buffer")
35-
}
30+
AnyValueKind::Blob(blob) => Ok(blob.as_slice()),
3631
other => other.unexpected(),
3732
}
3833
}
@@ -49,15 +44,15 @@ impl<'q> Encode<'q, Any> for Vec<u8> {
4944
&self,
5045
buf: &mut <Any as Database>::ArgumentBuffer<'q>,
5146
) -> Result<IsNull, BoxDynError> {
52-
buf.0.push(AnyValueKind::Blob(Cow::Owned(self.clone())));
47+
buf.0.push(AnyValueKind::Blob(Arc::new(self.clone())));
5348
Ok(IsNull::No)
5449
}
5550
}
5651

5752
impl<'r> Decode<'r, Any> for Vec<u8> {
5853
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
5954
match value.kind {
60-
AnyValueKind::Blob(blob) => Ok(blob.into_owned()),
55+
AnyValueKind::Blob(blob) => Ok(blob.as_ref().clone()),
6156
other => other.unexpected(),
6257
}
6358
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl<'q> Encode<'q, Any> for bool {
2626
impl<'r> Decode<'r, Any> for bool {
2727
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
2828
match value.kind {
29-
AnyValueKind::Bool(b) => Ok(b),
29+
AnyValueKind::Bool(b) => Ok(*b),
3030
other => other.unexpected(),
3131
}
3232
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ impl Type<Any> for f32 {
1313
}
1414
}
1515

16-
impl<'q> Encode<'q, Any> for f32 {
17-
fn encode_by_ref(&self, buf: &mut AnyArgumentBuffer<'q>) -> Result<IsNull, BoxDynError> {
16+
impl Encode<'_, Any> for f32 {
17+
fn encode_by_ref(&self, buf: &mut AnyArgumentBuffer) -> Result<IsNull, BoxDynError> {
1818
buf.0.push(AnyValueKind::Real(*self));
1919
Ok(IsNull::No)
2020
}
@@ -23,7 +23,7 @@ impl<'q> Encode<'q, Any> for f32 {
2323
impl<'r> Decode<'r, Any> for f32 {
2424
fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError> {
2525
match value.kind {
26-
AnyValueKind::Real(r) => Ok(r),
26+
AnyValueKind::Real(r) => Ok(*r),
2727
other => other.unexpected(),
2828
}
2929
}
@@ -51,8 +51,8 @@ impl<'r> Decode<'r, Any> for f64 {
5151
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
5252
match value.kind {
5353
// Widening is safe
54-
AnyValueKind::Real(r) => Ok(r as f64),
55-
AnyValueKind::Double(d) => Ok(d),
54+
AnyValueKind::Real(r) => Ok(*r as f64),
55+
AnyValueKind::Double(d) => Ok(*d),
5656
other => other.unexpected(),
5757
}
5858
}

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::decode::Decode;
55
use crate::encode::{Encode, IsNull};
66
use crate::error::BoxDynError;
77
use crate::types::Type;
8-
use std::borrow::Cow;
8+
use std::sync::Arc;
99

1010
impl Type<Any> for str {
1111
fn type_info() -> AnyTypeInfo {
@@ -20,7 +20,7 @@ impl<'a> Encode<'a, Any> for &'a str {
2020
where
2121
Self: Sized,
2222
{
23-
buf.0.push(AnyValueKind::Text(self.into()));
23+
buf.0.push(AnyValueKind::Text(Arc::new(self.into())));
2424
Ok(IsNull::No)
2525
}
2626

@@ -35,12 +35,7 @@ impl<'a> Encode<'a, Any> for &'a str {
3535
impl<'a> Decode<'a, Any> for &'a str {
3636
fn decode(value: <Any as Database>::ValueRef<'a>) -> Result<Self, BoxDynError> {
3737
match value.kind {
38-
AnyValueKind::Text(Cow::Borrowed(text)) => Ok(text),
39-
// This shouldn't happen in practice, it means the user got an `AnyValueRef`
40-
// constructed from an owned `String` which shouldn't be allowed by the API.
41-
AnyValueKind::Text(Cow::Owned(_text)) => {
42-
panic!("attempting to return a borrow that outlives its buffer")
43-
}
38+
AnyValueKind::Text(text) => Ok(text.as_str()),
4439
other => other.unexpected(),
4540
}
4641
}
@@ -53,19 +48,27 @@ impl Type<Any> for String {
5348
}
5449

5550
impl<'q> Encode<'q, Any> for String {
51+
fn encode(
52+
self,
53+
buf: &mut <Any as Database>::ArgumentBuffer<'q>,
54+
) -> Result<IsNull, BoxDynError> {
55+
buf.0.push(AnyValueKind::Text(Arc::new(self)));
56+
Ok(IsNull::No)
57+
}
58+
5659
fn encode_by_ref(
5760
&self,
5861
buf: &mut <Any as Database>::ArgumentBuffer<'q>,
5962
) -> Result<IsNull, BoxDynError> {
60-
buf.0.push(AnyValueKind::Text(Cow::Owned(self.clone())));
63+
buf.0.push(AnyValueKind::Text(Arc::new(self.clone())));
6164
Ok(IsNull::No)
6265
}
6366
}
6467

6568
impl<'r> Decode<'r, Any> for String {
6669
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
6770
match value.kind {
68-
AnyValueKind::Text(text) => Ok(text.into_owned()),
71+
AnyValueKind::Text(text) => Ok(text.to_string()),
6972
other => other.unexpected(),
7073
}
7174
}

sqlx-core/src/any/value.rs

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
use std::borrow::Cow;
2-
31
use crate::any::{Any, AnyTypeInfo, AnyTypeInfoKind};
42
use crate::database::Database;
53
use crate::error::BoxDynError;
64
use crate::types::Type;
75
use crate::value::{Value, ValueRef};
6+
use std::borrow::Cow;
7+
use std::sync::Arc;
88

99
#[derive(Clone, Debug)]
1010
#[non_exhaustive]
11-
pub enum AnyValueKind<'a> {
11+
pub enum AnyValueKind {
1212
Null(AnyTypeInfoKind),
1313
Bool(bool),
1414
SmallInt(i16),
1515
Integer(i32),
1616
BigInt(i64),
1717
Real(f32),
1818
Double(f64),
19-
Text(Cow<'a, str>),
20-
Blob(Cow<'a, [u8]>),
19+
Text(Arc<String>),
20+
TextSlice(Arc<str>),
21+
Blob(Arc<Vec<u8>>),
2122
}
2223

23-
impl AnyValueKind<'_> {
24+
impl AnyValueKind {
2425
fn type_info(&self) -> AnyTypeInfo {
2526
AnyTypeInfo {
2627
kind: match self {
@@ -32,6 +33,7 @@ impl AnyValueKind<'_> {
3233
AnyValueKind::Real(_) => AnyTypeInfoKind::Real,
3334
AnyValueKind::Double(_) => AnyTypeInfoKind::Double,
3435
AnyValueKind::Text(_) => AnyTypeInfoKind::Text,
36+
AnyValueKind::TextSlice(_) => AnyTypeInfoKind::Text,
3537
AnyValueKind::Blob(_) => AnyTypeInfoKind::Blob,
3638
},
3739
}
@@ -60,31 +62,19 @@ impl AnyValueKind<'_> {
6062
#[derive(Clone, Debug)]
6163
pub struct AnyValue {
6264
#[doc(hidden)]
63-
pub kind: AnyValueKind<'static>,
65+
pub kind: AnyValueKind,
6466
}
6567

6668
#[derive(Clone, Debug)]
6769
pub struct AnyValueRef<'a> {
68-
pub(crate) kind: AnyValueKind<'a>,
70+
pub(crate) kind: &'a AnyValueKind,
6971
}
7072

7173
impl Value for AnyValue {
7274
type Database = Any;
7375

7476
fn as_ref(&self) -> <Self::Database as Database>::ValueRef<'_> {
75-
AnyValueRef {
76-
kind: match &self.kind {
77-
AnyValueKind::Null(k) => AnyValueKind::Null(*k),
78-
AnyValueKind::Bool(b) => AnyValueKind::Bool(*b),
79-
AnyValueKind::SmallInt(i) => AnyValueKind::SmallInt(*i),
80-
AnyValueKind::Integer(i) => AnyValueKind::Integer(*i),
81-
AnyValueKind::BigInt(i) => AnyValueKind::BigInt(*i),
82-
AnyValueKind::Real(r) => AnyValueKind::Real(*r),
83-
AnyValueKind::Double(d) => AnyValueKind::Double(*d),
84-
AnyValueKind::Text(t) => AnyValueKind::Text(Cow::Borrowed(t)),
85-
AnyValueKind::Blob(b) => AnyValueKind::Blob(Cow::Borrowed(b)),
86-
},
87-
}
77+
AnyValueRef { kind: &self.kind }
8878
}
8979

9080
fn type_info(&self) -> Cow<'_, <Self::Database as Database>::TypeInfo> {
@@ -101,17 +91,7 @@ impl<'a> ValueRef<'a> for AnyValueRef<'a> {
10191

10292
fn to_owned(&self) -> <Self::Database as Database>::Value {
10393
AnyValue {
104-
kind: match &self.kind {
105-
AnyValueKind::Null(k) => AnyValueKind::Null(*k),
106-
AnyValueKind::Bool(b) => AnyValueKind::Bool(*b),
107-
AnyValueKind::SmallInt(i) => AnyValueKind::SmallInt(*i),
108-
AnyValueKind::Integer(i) => AnyValueKind::Integer(*i),
109-
AnyValueKind::BigInt(i) => AnyValueKind::BigInt(*i),
110-
AnyValueKind::Real(r) => AnyValueKind::Real(*r),
111-
AnyValueKind::Double(d) => AnyValueKind::Double(*d),
112-
AnyValueKind::Text(t) => AnyValueKind::Text(Cow::Owned(t.to_string())),
113-
AnyValueKind::Blob(b) => AnyValueKind::Blob(Cow::Owned(b.to_vec())),
114-
},
94+
kind: self.kind.clone(),
11595
}
11696
}
11797

0 commit comments

Comments
 (0)