【发布时间】:2021-08-12 00:55:55
【问题描述】:
我正在尝试为我正在构建的应用程序松散地实现存储库模式,但是我似乎无意中遇到了某种递归类型定义。
关于以下代码:
use diesel::{pg::Pg, Insertable, Queryable, QueryDsl, PgConnection, Identifiable, Table};
use std::marker::PhantomData;
use anyhow::anyhow;
pub struct Repository<Entity, SqlType, Tab> {
_entity_phantom: PhantomData<Entity>,
_type_phantom: PhantomData<SqlType>,
table: Tab,
}
impl <Entity, SqlType, Tab> Repository<Entity, SqlType, Tab>
where
Entity: Queryable<SqlType, Pg> + Insertable<Tab> + Identifiable,
Tab: Table
{
fn find_by_id(&self, id: i64, conn: &PgConnection) -> anyhow::Result<Entity> {
match self.table.find(id).first::<Entity>(conn) {
Ok(entity) => Ok(entity),
Err(e) => Err(anyhow!("{}", e)),
}
}
}
我得到错误:
error[E0275]: overflow evaluating the requirement `_: Sized`
--> support/src/lib.rs:18:26
|
18 | match self.table.find(id).first::<Entity>(conn) {
| ^^^^
|
= help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`support`)
= note: required because of the requirements on the impl of `FilterDsl<_>` for `<Tab as AsQuery>::Query`
我尝试增加 crate 顶部的递归限制,但无济于事。将+ Sized 洒在整个特征范围内,似乎也没有任何作用。
我错过了什么?
【问题讨论】:
-
嗨,您有插入和更新的解决方案吗?柴油::insert_into(self.table) .values(data) .get_result::<:entity>(self.conn) 的特征是什么
标签: rust rust-diesel