Hi, I'm writing CRUD API for my service using monomorphization - created generic functions which will used in future by other functions with generics.
In this particular case I need to get the info from table and convert it into struct, which implementing FromRow macro trait
#[derive(Clone, Debug, Serialize, FromRow, Fields)]
pub struct Note {
id: i64,
title: Option<String>,
}
Here's my function:
pub async fn get<MC, E>(ctx: &Context, model_manager: &ModelManager, id: i64) -> Result<E>
where
MC: DbBMC,
E: HasSeaFields + Send,
E: for<'r> FromRow<'r, PgRow>
{
let db = model_manager.db();
// SELECT FROM MC::table_ref() WHERE id = id
let mut query = Query::select();
query
.from(MC::table_ref())
.and_where(Expr::col(CommonIdentifier::Id).eq(id));
let (sql, values) = query.build_sqlx(PostgresQueryBuilder);
// <_, E, _> - E - это реализация трейта FromRow
let sqlx_query = sqlx::query_as_with::<_, E, _>(&sql, values);
let entity = db
.fetch_optional(sqlx_query)
.await?
.ok_or(EntityNotFound {
entity: MC::TABLE,
id,
})?;
Ok(entity)
}
Running Cargo Check I got an error, which told me that FromRow wasn't implemented for E:
error[E0308]: mismatched types
--> src\model\base\crud_fns.rs:80:8
|
53 | pub async fn get<'q, MC, E>(ctx: &Context, model_manager: &ModelManager, id: i64) -> Result<E>
| - expected this type parameter
...
80 | Ok(entity)
| -- ^^^^^^ expected type parameter `E`, found `PgRow`
| |
| arguments to this enum variant are incorrect
|
= note: expected type parameter `E`
found struct `PgRow`
help: the type constructed contains `PgRow` due to the type of the argument passed
--> src\model\base\crud_fns.rs:80:5
|
80 | Ok(entity)
| ^^^------^
| |
| this argument influences the type of `Ok`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `Timeline` (bin "Timeline") due to 1 previous error
Process finished with exit code 101
But it was! E: for<'r> FromRow<'r, PgRow>
!
I cannot understand this problem, help me pls...