Here is a version of your code that runs on the playground :
pub trait Entity {
fn map(&self, Result<QueryResult>) -> Self;
}
pub struct DbQuery<T> {
pub sql: String,
pub params: Vec<Value>,
pub limit: i32,
pub paged: Option<Pagination>,
pub entity: T,
}
pub struct Settings {
pub name: String,
pub value: Option<String>,
}
impl Entity for Settings {
fn map(&self, result: Result<QueryResult>) -> Settings {
// ...
Settings {
name: "hello".to_string(),
value: None,
}
}
}
impl DbMapper {
// ...
pub fn find<T>(&self, query: DbQuery<T>) -> Option<Vec<T>> {
println!("query find SQL: {}", query.sql);
let mut stmt = &self.pool.prepare(query.sql).unwrap();
let ret = Vec::new();
for row in stmt.execute(query.params).unwrap() {
ret.push(query.entity.map(row.unwrap()));
}
Some(ret)
}
}
하지만 오류가 발생 문제를 복제합니다.
pub struct QueryResult;
pub struct Value;
pub struct Pagination;
pub struct DbMapper;
pub trait Entity {
fn map(&self, Result<QueryResult,()>) -> Self;
}
pub struct DbQuery<T> {
pub sql: String,
pub params: Vec<Value>,
pub limit: i32,
pub paged: Option<Pagination>,
pub entity: T,
}
pub struct Settings {
pub name: String,
pub value: Option<String>,
}
impl Entity for Settings {
fn map(&self, result: Result<QueryResult,()>) -> Settings {
// ...
Settings {
name: "hello".to_string(),
value: None,
}
}
}
impl DbMapper {
// ...
pub fn find<T>(&self, query: DbQuery<T>) -> Option<Vec<T>> {
println!("query find SQL: {}", query.sql);
// ########## attempt to call map()
let _ = query.entity.map(Ok(QueryResult {}));
let ret = Vec::new();
Some(ret)
}
}
fn main() {}
문제는입니다. find
메서드에서 DbQuery<T>
인수의은 T
이 Entity
유형임을 알 수 없습니다. 그래서 우리는 그것을 말할 필요가 있습니다 :
pub fn find<T>(&self, query: DbQuery<T>) -> Option<Vec<T>>
where T: Entity
{
// ... code here ...
}
This now compiles and runs.
이제 컴파일러는 T
이 어떤 설명 인 Entity
이라는 것을 알고 있으며 map
메서드를 호출 할 수 있습니다.
당신의 예제가 최소한의 예제로는별로 보이지 않지만, 나는'D'가'DbQuery' 나'DbMapper ::'에'Entity'를 구현하도록'T'를 제한하지 않았다고 대답 할 것이라고 생각합니다. 찾기 '. –
죄송합니다. 최소 코드를 사용하여 설명했습니다. 불행히도 T 대신 Entity를 이미 시도했지만 동일한 오류가 발생했습니다. – plailopo
안녕하세요 - 다소 적습니다 만 play.rust-lang.org에 넣고 컴파일하여 동일한 오류가 발생하도록 충분히 완벽하지는 않습니다. 그래서 확신하기가 약간 어렵습니다. –