0

我无法理解为什么会遇到错误 E0277。我有以下代码:

#[derive(Queryable, Identifiable, Serialize, Deserialize)]
pub struct User {
    pub id: i32,
    pub email: String,
    pub password: String,
    pub first_name: String,
    pub last_name: String,
}


struct UserForEmail {
    email: String,
}

impl Message for UserForEmail {
    type Result = Result<Option<User>, Error>;
}

impl Handler<UserForEmail> for DbExecutor {
    type Result = Result<Option<User>, Error>;

    fn handle(&mut self, msg: UserForEmail, _ctx: &mut Self::Context) -> Self::Result {
        let conn = self.connection_pool.get()?;

        let user_res = users.filter(email.eq(&msg.email)).load(&conn)?;

        match user_res.len() {
            0 => Ok(None),
            1 => Ok(user_res[0]),
            _ => Err(format_err!("Found more than one user for email")), // TODO make this an actual error
        }
    }
}

当我尝试编译时,出现以下错误:

error[E0277]: the trait bound `std::option::Option<domain::user::User>: diesel::Queryable<(diesel::sql_types::Integer, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Text), diesel::pg::Pg>` is not satisfied
  --> src/domain/user.rs:60:59
   |
60 |         let user_res = users.filter(email.eq(&msg.email)).load(&conn)?;
   |                                                           ^^^^ the trait `diesel::Queryable<(diesel::sql_types::Integer, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Text), diesel::pg::Pg>` is not implemented for `std::option::Option<domain::user::User>`
   |
   = help: the following implementations were found:
             <std::option::Option<T> as diesel::Queryable<diesel::sql_types::Nullable<ST>, DB>>
   = note: required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>, std::option::Option<domain::user::User>>` for `diesel::query_builder::SelectStatement<schema::users::table, diesel::query_builder::select_clause::DefaultSelectClause, diesel::query_builder::distinct_clause::NoDistinctClause, diesel::query_builder::where_clause::WhereClause<diesel::expression::operators::Eq<schema::users::columns::email, diesel::expression::bound::Bound<diesel::sql_types::Text, &std::string::String>>>>`

我不明白为什么在这种情况下diesel::Queryable需要实施。Option<User>如果我将消息的结果类型切换为,Result<User, Error>那么问题就会消失

4

0 回答 0