创建要由pgpool”,“ mysqlpool”等实现的特征。
上实现的特征
#[async_trait]
trait MyTrait {
const QUERY: &'static str;
fn fetch(&self) -> Option<Self> {
sqlx::query(Self::QUERY)
.fetch_one(self)
.await
.map(|row| row.get(0))
.ok()
}
}
#[async_trait]
impl MyTrait for sqlx::PgPool {
const QUERY: &'static str = "SELECT value FROM table WHERE id = 10";
}
。
error[E0277]: the trait bound `&Self: sqlx::Executor<'_>` is not satisfied
--> service/src/lib.rs:265:24
|
265 | .fetch_one(self)
| --------- ^^^^ the trait `sqlx::Executor<'_>` is not implemented for `&Self`
| |
| required by a bound introduced by this call
|
note: required by a bound in `sqlx::query::Query::<'q, DB, A>::fetch_one`
我正在使用sqlx来定义要在pgpool
,mysqlpool
等 尝试添加where self:sqlx :: executor&lt;'...,datbase = db&gt;
及其多种变化,但不幸的是没有可用。
我目前正在尝试mytrait&lt;'p,db:sqlx ::数据库&gt; where&amp;'p self:sqlx :: executor&lt;'p,database = db&gt; s self:'p
with inasimenation intermantion
squy&lt;'_,sqlx :: postgres&gt;对于pgpool
,但这给出了错误键入不匹配,以sqlx :: epecutor&lt;'_&gt;&gt; :: database == postgres`
告诉我我需要修改代码以进行编译,我真的很感激,谢谢。
I'm using SQLx to define a trait that is to be implemented on PgPool
, MySqlPool
, etc. and looks something like this:
#[async_trait]
trait MyTrait {
const QUERY: &'static str;
fn fetch(&self) -> Option<Self> {
sqlx::query(Self::QUERY)
.fetch_one(self)
.await
.map(|row| row.get(0))
.ok()
}
}
#[async_trait]
impl MyTrait for sqlx::PgPool {
const QUERY: &'static str = "SELECT value FROM table WHERE id = 10";
}
This does not compile with:
error[E0277]: the trait bound `&Self: sqlx::Executor<'_>` is not satisfied
--> service/src/lib.rs:265:24
|
265 | .fetch_one(self)
| --------- ^^^^ the trait `sqlx::Executor<'_>` is not implemented for `&Self`
| |
| required by a bound introduced by this call
|
note: required by a bound in `sqlx::query::Query::<'q, DB, A>::fetch_one`
I've tried adding where Self: sqlx::Executor<'..., Datbase = DB>
and numerous variations of it but to no avail unfortunately.
I'm currently trying something like MyTrait<'p, DB: sqlx::Database> where &'p Self: sqlx::Executor<'p, Database = DB>, Self: 'p
with implementation impl MyTrait<'_, sqlx::Postgres> for PgPool
but this gives the error type mismatch resolving `<&Self as sqlx::Executor<'_>>::Database == Postgres`
If anybody could tell me how I need to modify my code in order for it to compile I would really appreciate it, thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论