英文:
How to implement a trait on any type that implements PgExecutor?
问题
我有自己的特性来扩展`PgExecutor`特性提供的功能。
我想要在任何实现了`PgExecutor`的东西上实现这些特性,但老实说,我遇到了一些困难。
```rust
#[async_trait]
impl<'a, T> PgExecutorExt for T
where T: PgExecutor<'a> + Sync
{
async fn my_read_method(&self) -> anyhow::Result<()> {
sqlx::query_as!(...)
.fetch_one(self)
.await?;
Ok(())
}
}
但我得到了以下错误:
未满足`&T: Executor<'_>`的特性约束
`Executor<'_>`特性未为`&T`实现
在.fetch_one(self)
调用上。
有没有人知道发生了什么?谢谢!
<details>
<summary>英文:</summary>
I have my own trait to extend the functionality that the `PgExecutor` trait provides.
I want to implement these trait on anything that implements `PgExecutor` but I'm honestly having a hard time.
```rust
#[async_trait]
impl<'a, T> PgExecutorExt for T
where T: PgExecutor<'a> + Sync
{
async fn my_read_method(&self) -> anyhow::Result<()> {
sqlx::query_as!(...)
.fetch_one(self)
.await?;
Ok(())
}
}
But I'm getting the following error:
the trait bound `&T: Executor<'_>` is not satisfied
the trait `Executor<'_>` is not implemented for `&T`
On the .fetch_one(self)
call
anyone has any clue on what is going on??
Thank you!
答案1
得分: 1
Executor::fetch_one()
函数接受 self
,而不是 &self
。您需要在您的特性定义和此实现中将 &self
更改为 self
,这应该会解决问题。
英文:
The Executor::fetch_one()
function takes self
, not &self
. You need to change &self
to self
in your trait definition and this implementation, and that should fix the issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论