I am trying to make an Entity interface for dynamically mapping a database result into a Rust struct:
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)
}
}
But I get an error:
error: no method named
mapfound for typeTin the current scope
ret.push(query.entity.map(row.unwrap())); |
note: the methodmapexists but the following trait bounds were not satisfied:T : std::iter::Iterator= help: items from traits can only be used if the trait is implemented and in scope; the following traits define an itemmap, perhaps you need to implement one of them: = help: candidate #1:models::holders::database::Entity= help: candidate #2:std::iter::Iterator
Tto implementEntityinDbQueryand/orDbMapper::find. - Chris Emerson