mysql :: value :: FromValue在调用mysql :: from_row时没有为大元组实现

时间:2018-03-23 05:42:49

标签: mysql rust

我想转换结构User,将afm以及p的十个字母命名属性添加到#[macro_use] extern crate mysql; use mysql::Pool; #[derive(Debug, Default)] pub struct User { /// The ID number of the user. pub id: u32, /// The name of the user / machine. pub user: String, /// The name of the company the user is attached to. pub company: Option<String>, /// The mass of the weight in the machine. pub mass_of_weight: Option<f64>, /// The total height of the machine. pub total_height: Option<f64>, /// The callibration coefficient for BMWI calculations involving this machine. pub k_coefficient: Option<f64>, /// The EU value for the machine at this point in time. pub energy_utilisation: Option<f64>, pub a: Option<f64>, pub b: Option<f64>, pub c: Option<f64>, pub d: Option<f64>, pub e: Option<f64>, pub f: Option<f64>, pub m: Option<f64>, pub n: Option<f64>, pub o: Option<f64>, pub p: Option<f64>, } impl User { /// Selects a user from the database, given their id. pub fn new(id: u32, pool: &Pool) -> Option<Self> { let ( user, company, mass_of_weight, total_height, k_coefficient, energy_utilisation, a, b, c, d, e, f, m, n, o, p, ) = match pool.prep_exec( include_str!("../../resources/sql/users/select.mysql"), params!{"id" => &id}, ).unwrap() .next() { Some(Ok(row)) => mysql::from_row(row), _ => return None, }; Some(User { id: id, user: user, company: company, mass_of_weight: mass_of_weight, total_height: total_height, k_coefficient: k_coefficient, energy_utilisation: energy_utilisation, a: a, b: b, c: c, d: d, e: e, f: f, m: m, n: n, o: o, p: p, }) } } fn main() {}

cargo build

当我运行error[E0277]: the trait bound `(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _): mysql::prelude::FromValue` is not satisfied --> src/main.rs:62:30 | 62 | Some(Ok(row)) => mysql::from_row(row), | ^^^^^^^^^^^^^^^ the trait `mysql::prelude::FromValue` is not implemented for `(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _)` | = note: required because of the requirements on the impl of `mysql::prelude::FromRow` for `(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _)` = note: required by `mysql::from_row` 时,我收到此错误:

SELECT id, user, company, mass_of_weight, total_height, k_coefficient,
       energy_utilisation, a, b, c, d, e, f, m, n, o, p
       FROM users

SQL是:

cargo clean

我该怎么办这个错误?我试过{{1}};它无济于事。

1 个答案:

答案 0 :(得分:1)

编译器不是骗你的。 FromRow仅针对特定大小的元组实现。 author of the library suggests calling Row::take

pub fn new(id: u32, pool: &Pool) -> Option<Self> {
    let sql = include_str!("../../resources/sql/users/select.mysql");

    let mut row = match pool.prep_exec(sql, params!{"id" => &id}).unwrap().next() {
        Some(Ok(row)) => row,
        _ => return None,
    };

    Some(User {
        id: row.take("id").unwrap(),
        user: row.take("user").unwrap(),
        company: row.take("company").unwrap(),
        mass_of_weight: row.take("mass_of_weight").unwrap(),
        total_height: row.take("total_height").unwrap(),
        k_coefficient: row.take("k_coefficient").unwrap(),
        energy_utilisation: row.take("energy_utilisation").unwrap(),
        a: row.take("a").unwrap(),
        b: row.take("b").unwrap(),
        c: row.take("c").unwrap(),
        d: row.take("d").unwrap(),
        e: row.take("e").unwrap(),
        f: row.take("f").unwrap(),
        m: row.take("m").unwrap(),
        n: row.take("n").unwrap(),
        o: row.take("o").unwrap(),
        p: row.take("p").unwrap(),
    })
}

由于您没有对数据进行任何转换,因此您可能希望了解Diesel是否更适合您的应用。