Skip to content

Commit 680f7b8

Browse files
committed
Start prepping for futures closing over parameters
Change the slice-consuming methods to requiring &(dyn ToSql + Sync), which makes the overall value Send. If you have non-Sync values for whatever reason, you can still use the iterator-based methods.
1 parent 4deea3b commit 680f7b8

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

tokio-postgres/src/client.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ impl Client {
197197
pub fn query(
198198
&mut self,
199199
statement: &Statement,
200-
params: &[&dyn ToSql],
200+
params: &[&(dyn ToSql + Sync)],
201201
) -> impl Stream<Item = Result<Row, Error>> {
202-
let buf = query::encode(statement, params.iter().cloned());
202+
let buf = query::encode(statement, params.iter().map(|s| *s as _));
203203
query::query(self.inner(), statement.clone(), buf)
204204
}
205205

@@ -229,9 +229,9 @@ impl Client {
229229
pub fn execute(
230230
&mut self,
231231
statement: &Statement,
232-
params: &[&dyn ToSql],
232+
params: &[&(dyn ToSql + Sync)],
233233
) -> impl Future<Output = Result<u64, Error>> {
234-
let buf = query::encode(statement, params.iter().cloned());
234+
let buf = query::encode(statement, params.iter().map(|s| *s as _));
235235
query::execute(self.inner(), buf)
236236
}
237237

@@ -262,7 +262,7 @@ impl Client {
262262
pub fn copy_in<S>(
263263
&mut self,
264264
statement: &Statement,
265-
params: &[&dyn ToSql],
265+
params: &[&(dyn ToSql + Sync)],
266266
stream: S,
267267
) -> impl Future<Output = Result<u64, Error>>
268268
where
@@ -271,7 +271,7 @@ impl Client {
271271
<S::Ok as IntoBuf>::Buf: 'static + Send,
272272
S::Error: Into<Box<dyn error::Error + Sync + Send>>,
273273
{
274-
let buf = query::encode(statement, params.iter().cloned());
274+
let buf = query::encode(statement, params.iter().map(|s| *s as _));
275275
copy_in::copy_in(self.inner(), buf, stream)
276276
}
277277

@@ -283,9 +283,9 @@ impl Client {
283283
pub fn copy_out(
284284
&mut self,
285285
statement: &Statement,
286-
params: &[&dyn ToSql],
286+
params: &[&(dyn ToSql + Sync)],
287287
) -> impl Stream<Item = Result<Bytes, Error>> {
288-
let buf = query::encode(statement, params.iter().cloned());
288+
let buf = query::encode(statement, params.iter().map(|s| *s as _));
289289
copy_out::copy_out(self.inner(), buf)
290290
}
291291

tokio-postgres/src/transaction.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'a> Transaction<'a> {
9595
pub fn query(
9696
&mut self,
9797
statement: &Statement,
98-
params: &[&dyn ToSql],
98+
params: &[&(dyn ToSql + Sync)],
9999
) -> impl Stream<Item = Result<Row, Error>> {
100100
self.client.query(statement, params)
101101
}
@@ -119,7 +119,7 @@ impl<'a> Transaction<'a> {
119119
pub fn execute(
120120
&mut self,
121121
statement: &Statement,
122-
params: &[&dyn ToSql],
122+
params: &[&(dyn ToSql + Sync)],
123123
) -> impl Future<Output = Result<u64, Error>> {
124124
self.client.execute(statement, params)
125125
}
@@ -150,10 +150,10 @@ impl<'a> Transaction<'a> {
150150
pub fn bind(
151151
&mut self,
152152
statement: &Statement,
153-
params: &[&dyn ToSql],
153+
params: &[&(dyn ToSql + Sync)],
154154
) -> impl Future<Output = Result<Portal, Error>> {
155155
// https://github.com/rust-lang/rust/issues/63032
156-
let buf = bind::encode(statement, params.iter().cloned());
156+
let buf = bind::encode(statement, params.iter().map(|s| *s as _));
157157
bind::bind(self.client.inner(), statement.clone(), buf)
158158
}
159159

@@ -189,7 +189,7 @@ impl<'a> Transaction<'a> {
189189
pub fn copy_in<S>(
190190
&mut self,
191191
statement: &Statement,
192-
params: &[&dyn ToSql],
192+
params: &[&(dyn ToSql + Sync)],
193193
stream: S,
194194
) -> impl Future<Output = Result<u64, Error>>
195195
where
@@ -205,7 +205,7 @@ impl<'a> Transaction<'a> {
205205
pub fn copy_out(
206206
&mut self,
207207
statement: &Statement,
208-
params: &[&dyn ToSql],
208+
params: &[&(dyn ToSql + Sync)],
209209
) -> impl Stream<Item = Result<Bytes, Error>> {
210210
self.client.copy_out(statement, params)
211211
}

tokio-postgres/tests/test/types/mod.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ mod uuid_07;
2727

2828
async fn test_type<T, S>(sql_type: &str, checks: &[(T, S)])
2929
where
30-
T: PartialEq + for<'a> FromSqlOwned + ToSql,
30+
T: PartialEq + for<'a> FromSqlOwned + ToSql + Sync,
3131
S: fmt::Display,
3232
{
3333
let mut client = connect("user=postgres").await;
@@ -656,3 +656,30 @@ async fn inet() {
656656
)
657657
.await;
658658
}
659+
660+
#[tokio::test]
661+
async fn check_send() {
662+
fn is_send<T: Send>(_: &T) {}
663+
664+
let mut client = connect("user=postgres").await;
665+
666+
let f = client.prepare("SELECT $1::TEXT");
667+
is_send(&f);
668+
let stmt = f.await.unwrap();
669+
670+
let f = client.query(&stmt, &[&"hello"]);
671+
is_send(&f);
672+
673+
let f = client.execute(&stmt, &[&"hello"]);
674+
is_send(&f);
675+
676+
let f = client.transaction();
677+
is_send(&f);
678+
let mut trans = f.await.unwrap();
679+
680+
let f = trans.query(&stmt, &[&"hello"]);
681+
is_send(&f);
682+
683+
let f = trans.execute(&stmt, &[&"hello"]);
684+
is_send(&f);
685+
}

0 commit comments

Comments
 (0)