I’m curious what the difference is between execute(&mut *transaction)
and execute(&mut transaction)
in my example since I didn’t think that transaction
was a pointer to begin with but for some reason execute(&mut *transaction)
satisfies the trait point while execute(&mut transaction)
does not.
Here’s the error I get when using the latter syntax:
<code>rustc: the trait bound `&mut sqlx::Transaction<'_, sqlx::Postgres>: sqlx::Executor<'_>` is not satisfied
the following other types implement trait `sqlx::Executor<'c>`:
<&'c mut sqlx::PgConnection as sqlx::Executor<'c>>
<&'c mut sqlx::sqlx_postgres::PgListener as sqlx::Executor<'c>>
<&'c mut sqlx::AnyConnection as sqlx::Executor<'c>>
<&sqlx::Pool<DB> as sqlx::Executor<'p>> [E0277]
rustc: required by a bound introduced by this call [E0277]
</code>
<code>rustc: the trait bound `&mut sqlx::Transaction<'_, sqlx::Postgres>: sqlx::Executor<'_>` is not satisfied
the following other types implement trait `sqlx::Executor<'c>`:
<&'c mut sqlx::PgConnection as sqlx::Executor<'c>>
<&'c mut sqlx::sqlx_postgres::PgListener as sqlx::Executor<'c>>
<&'c mut sqlx::AnyConnection as sqlx::Executor<'c>>
<&sqlx::Pool<DB> as sqlx::Executor<'p>> [E0277]
rustc: required by a bound introduced by this call [E0277]
</code>
rustc: the trait bound `&mut sqlx::Transaction<'_, sqlx::Postgres>: sqlx::Executor<'_>` is not satisfied
the following other types implement trait `sqlx::Executor<'c>`:
<&'c mut sqlx::PgConnection as sqlx::Executor<'c>>
<&'c mut sqlx::sqlx_postgres::PgListener as sqlx::Executor<'c>>
<&'c mut sqlx::AnyConnection as sqlx::Executor<'c>>
<&sqlx::Pool<DB> as sqlx::Executor<'p>> [E0277]
rustc: required by a bound introduced by this call [E0277]
Does this boil down to doing some sort of type casting? Curious if this would be considered a limitation or if it actually makes sense for the compiler to think these two expressions are different objects.
<code>use sqlx::{PgPool, Transaction, Executor};
use chrono::{DateTime, Utc};
pub struct SimpleDbClient {
db: PgPool,
}
pub trait AddCharacterEntries {
async fn insert_character_entries(&self, timestamp: DateTime<Utc>, char_values: Vec<(char, i32)>) -> sqlx::Result<()>;
}
impl AddCharacterEntries for SimpleDbClient {
async fn insert_character_entries(&self, timestamp: DateTime<Utc>, char_values: Vec<(char, i32)>) -> sqlx::Result<()> {
let mut transaction = self.db.begin().await?;
for (character, ascii_value) in char_values {
sqlx::query!(
r#"
INSERT INTO character_log (timestamp, character, ascii_value)
VALUES ($1, $2, $3)
"#,
timestamp,
character as char,
ascii_value
)
.execute(&mut *transaction)
.await?;
}
transaction.commit().await?;
Ok(())
}
}
</code>
<code>use sqlx::{PgPool, Transaction, Executor};
use chrono::{DateTime, Utc};
pub struct SimpleDbClient {
db: PgPool,
}
pub trait AddCharacterEntries {
async fn insert_character_entries(&self, timestamp: DateTime<Utc>, char_values: Vec<(char, i32)>) -> sqlx::Result<()>;
}
impl AddCharacterEntries for SimpleDbClient {
async fn insert_character_entries(&self, timestamp: DateTime<Utc>, char_values: Vec<(char, i32)>) -> sqlx::Result<()> {
let mut transaction = self.db.begin().await?;
for (character, ascii_value) in char_values {
sqlx::query!(
r#"
INSERT INTO character_log (timestamp, character, ascii_value)
VALUES ($1, $2, $3)
"#,
timestamp,
character as char,
ascii_value
)
.execute(&mut *transaction)
.await?;
}
transaction.commit().await?;
Ok(())
}
}
</code>
use sqlx::{PgPool, Transaction, Executor};
use chrono::{DateTime, Utc};
pub struct SimpleDbClient {
db: PgPool,
}
pub trait AddCharacterEntries {
async fn insert_character_entries(&self, timestamp: DateTime<Utc>, char_values: Vec<(char, i32)>) -> sqlx::Result<()>;
}
impl AddCharacterEntries for SimpleDbClient {
async fn insert_character_entries(&self, timestamp: DateTime<Utc>, char_values: Vec<(char, i32)>) -> sqlx::Result<()> {
let mut transaction = self.db.begin().await?;
for (character, ascii_value) in char_values {
sqlx::query!(
r#"
INSERT INTO character_log (timestamp, character, ascii_value)
VALUES ($1, $2, $3)
"#,
timestamp,
character as char,
ascii_value
)
.execute(&mut *transaction)
.await?;
}
transaction.commit().await?;
Ok(())
}
}