Correct Way to Achieve Query Pipelining with Vec
of Future
s
#212
-
I'm looking to implement an " use diesel_async::AsyncConnection;
use futures::FutureExt;
pub trait Upsert {
type Returns;
async fn upsert(&self, conn: &mut AsyncPgConnection) -> Self::Returns;
}
impl<T: Upsert> Upsert for Vec<T> {
type Returns = Vec<T::Returns>;
async fn upsert(&self, conn: &mut AsyncPgConnection) -> Self::Returns {
let upserts = self.iter().map(|item| item.upsert(conn));
futures::future::join_all(upserts).await
}
} obviously this doesn't compile, as
In this discussion #190, I found the recommendation to use pub trait Upsert2 {
type Returns: Send;
fn upsert(&self, conn: &mut AsyncPgConnection) -> BoxFuture<Self::Returns>;
}
impl<T: Upsert2> Upsert2 for Vec<T> {
type Returns = Vec<T::Returns>;
fn upsert(&self, conn: &mut AsyncPgConnection) -> BoxFuture<Self::Returns> {
let upserts = self.iter().map(|item| item.upsert(conn));
futures::future::join_all(upserts).boxed()
}
} note that I had to add |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I wouldn't call this "workaround" but expected behavior. It's one of rusts core values that you cannot have more than one mutable reference at the same time. You have exactly that by using an
That would be great, especially if you can point out that requirement to return a future that does not borrow the connection (e.g. by using |
Beta Was this translation helpful? Give feedback.
I wouldn't call this "workaround" but expected behavior. It's one of rusts core values that you cannot have more than one mutable reference at the same time. You have exactly that by using an
async
function there as the compiler generated future contains any function argument.That would be great, especially if you can point out that requirement to return a future that does not borrow the connection (e.g. by using
BoxFuture
with a lifetime decoupled from the connection) explicitly.