Skip to content

Commit

Permalink
Fix async madness
Browse files Browse the repository at this point in the history
  • Loading branch information
Virgiel committed Jul 28, 2022
1 parent 56ff29a commit cb77797
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 30 deletions.
10 changes: 7 additions & 3 deletions bench/usage/cornucopia_benches/generated_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,16 @@ pub mod queries {
client.execute(stmt, &[name, hair_color]).await
}
}
impl<'a, C: GenericClient>
impl<'a, C: GenericClient + Send + Sync>
cornucopia_client::async_::Params<
'a,
InsertUserParams<'a>,
std::pin::Pin<
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + 'a>,
Box<
dyn futures::Future<Output = Result<u64, tokio_postgres::Error>>
+ Send
+ 'a,
>,
>,
C,
> for InsertUserStmt
Expand All @@ -414,7 +418,7 @@ pub mod queries {
client: &'a C,
params: &'a InsertUserParams<'a>,
) -> std::pin::Pin<
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + 'a>,
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + Send + 'a>,
> {
Box::pin(self.bind(client, &params.name, &params.hair_color))
}
Expand Down
70 changes: 49 additions & 21 deletions codegen_test/src/cornucopia_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,12 +1304,16 @@ pub mod queries {
.await
}
}
impl<'a, C: GenericClient>
impl<'a, C: GenericClient + Send + Sync>
cornucopia_client::async_::Params<
'a,
InsertNightmareDomainParams<'a>,
std::pin::Pin<
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + 'a>,
Box<
dyn futures::Future<Output = Result<u64, tokio_postgres::Error>>
+ Send
+ 'a,
>,
>,
C,
> for InsertNightmareDomainStmt
Expand All @@ -1319,7 +1323,7 @@ pub mod queries {
client: &'a C,
params: &'a InsertNightmareDomainParams<'a>,
) -> std::pin::Pin<
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + 'a>,
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + Send + 'a>,
> {
Box::pin(self.bind(
client,
Expand Down Expand Up @@ -1713,12 +1717,16 @@ pub mod queries {
client.execute(stmt, &[named]).await
}
}
impl<'a, C: GenericClient>
impl<'a, C: GenericClient + Send + Sync>
cornucopia_client::async_::Params<
'a,
NamedComplexParams<'a>,
std::pin::Pin<
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + 'a>,
Box<
dyn futures::Future<Output = Result<u64, tokio_postgres::Error>>
+ Send
+ 'a,
>,
>,
C,
> for NewNamedComplexStmt
Expand All @@ -1728,7 +1736,7 @@ pub mod queries {
client: &'a C,
params: &'a NamedComplexParams<'a>,
) -> std::pin::Pin<
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + 'a>,
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + Send + 'a>,
> {
Box::pin(self.bind(client, &params.named))
}
Expand Down Expand Up @@ -1870,12 +1878,16 @@ pub mod queries {
client.execute(stmt, &[texts, name, composite]).await
}
}
impl<'a, C: GenericClient>
impl<'a, C: GenericClient + Send + Sync>
cornucopia_client::async_::Params<
'a,
NullityParams<'a>,
std::pin::Pin<
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + 'a>,
Box<
dyn futures::Future<Output = Result<u64, tokio_postgres::Error>>
+ Send
+ 'a,
>,
>,
C,
> for NewNullityStmt
Expand All @@ -1885,7 +1897,7 @@ pub mod queries {
client: &'a C,
params: &'a NullityParams<'a>,
) -> std::pin::Pin<
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + 'a>,
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + Send + 'a>,
> {
Box::pin(self.bind(client, &params.texts, &params.name, &params.composite))
}
Expand Down Expand Up @@ -2024,12 +2036,16 @@ pub mod queries {
client.execute(stmt, &[author, name]).await
}
}
impl<'a, C: GenericClient>
impl<'a, C: GenericClient + Send + Sync>
cornucopia_client::async_::Params<
'a,
InsertBookParams<'a>,
std::pin::Pin<
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + 'a>,
Box<
dyn futures::Future<Output = Result<u64, tokio_postgres::Error>>
+ Send
+ 'a,
>,
>,
C,
> for InsertBookStmt
Expand All @@ -2039,7 +2055,7 @@ pub mod queries {
client: &'a C,
params: &'a InsertBookParams<'a>,
) -> std::pin::Pin<
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + 'a>,
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + Send + 'a>,
> {
Box::pin(self.bind(client, &params.author, &params.name))
}
Expand Down Expand Up @@ -2099,12 +2115,16 @@ pub mod queries {
client.execute(stmt, &[c, a]).await
}
}
impl<'a, C: GenericClient>
impl<'a, C: GenericClient + Send + Sync>
cornucopia_client::async_::Params<
'a,
ParamsOrderParams,
std::pin::Pin<
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + 'a>,
Box<
dyn futures::Future<Output = Result<u64, tokio_postgres::Error>>
+ Send
+ 'a,
>,
>,
C,
> for ParamsOrderStmt
Expand All @@ -2114,7 +2134,7 @@ pub mod queries {
client: &'a C,
params: &'a ParamsOrderParams,
) -> std::pin::Pin<
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + 'a>,
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + Send + 'a>,
> {
Box::pin(self.bind(client, &params.c, &params.a))
}
Expand Down Expand Up @@ -3254,12 +3274,16 @@ pub mod queries {
.await
}
}
impl<'a, C: GenericClient>
impl<'a, C: GenericClient + Send + Sync>
cornucopia_client::async_::Params<
'a,
EverythingParams<'a>,
std::pin::Pin<
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + 'a>,
Box<
dyn futures::Future<Output = Result<u64, tokio_postgres::Error>>
+ Send
+ 'a,
>,
>,
C,
> for InsertEverythingStmt
Expand All @@ -3269,7 +3293,7 @@ pub mod queries {
client: &'a C,
params: &'a EverythingParams<'a>,
) -> std::pin::Pin<
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + 'a>,
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + Send + 'a>,
> {
Box::pin(self.bind(
client,
Expand Down Expand Up @@ -3480,12 +3504,16 @@ pub mod queries {
.await
}
}
impl<'a, C: GenericClient>
impl<'a, C: GenericClient + Send + Sync>
cornucopia_client::async_::Params<
'a,
EverythingArrayParams<'a>,
std::pin::Pin<
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + 'a>,
Box<
dyn futures::Future<Output = Result<u64, tokio_postgres::Error>>
+ Send
+ 'a,
>,
>,
C,
> for InsertEverythingArrayStmt
Expand All @@ -3495,7 +3523,7 @@ pub mod queries {
client: &'a C,
params: &'a EverythingArrayParams<'a>,
) -> std::pin::Pin<
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + 'a>,
Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + Send + 'a>,
> {
Box::pin(self.bind(
client,
Expand Down
2 changes: 1 addition & 1 deletion codegen_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::net::{IpAddr, Ipv4Addr};
use time::{OffsetDateTime, PrimitiveDateTime};
use uuid::Uuid;

use cornucopia_client::sync::Params;
use crate::cornucopia_sync::{
queries::{
copy::{insert_clone, insert_copy, select_copy},
Expand Down Expand Up @@ -37,6 +36,7 @@ use crate::cornucopia_sync::{
SpongebobCharacter,
},
};
use cornucopia_client::sync::Params;

pub fn main() {
let client = &mut Config::new()
Expand Down
9 changes: 5 additions & 4 deletions cornucopia/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,19 +491,20 @@ fn gen_query_fn(
}}"
);
} else {
let (pre_ty, post_ty_lf, pre, post) = if is_async {
let (send_sync, pre_ty, post_ty_lf, pre, post) = if is_async {
(
"+ Send + Sync",
"std::pin::Pin<Box<dyn futures::Future<Output = ",
"> + 'a>>",
"> + Send + 'a>>",
"Box::pin(",
")",
)
} else {
("", "", "", "")
("", "", "", "", "")
};
gen!(
w,
"impl <'a, C: GenericClient> cornucopia_client::{mod_name}::Params<'a, {param_name}{lifetime}, {pre_ty}Result<u64, {backend}::Error>{post_ty_lf}, C> for {struct_name}Stmt {{
"impl <'a, C: GenericClient {send_sync}> cornucopia_client::{mod_name}::Params<'a, {param_name}{lifetime}, {pre_ty}Result<u64, {backend}::Error>{post_ty_lf}, C> for {struct_name}Stmt {{
fn params(&'a mut self, client: &'a {client_mut} C, params: &'a {param_name}{lifetime}) -> {pre_ty}Result<u64, {backend}::Error>{post_ty_lf} {{
{pre}self.bind(client, {param_values}){post}
}}
Expand Down
2 changes: 1 addition & 1 deletion cornucopia_client/src/async_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tokio_postgres::{
/// In addition, when the `deadpool` feature is enabled (default), this trait also
/// abstracts over deadpool clients and transactions
#[async_trait]
pub trait GenericClient {
pub trait GenericClient: Send + Sync {
async fn prepare(&self, query: &str) -> Result<Statement, Error>;
async fn execute<T>(
&self,
Expand Down

0 comments on commit cb77797

Please sign in to comment.