Rocket: "FromFromValue is not implemented" ... but should for f64

Created on 11 Dec 2018  路  3Comments  路  Source: SergioBenitez/Rocket

Hi there,

this might be a bug, but I guess I am overseeing something.

I am using Rocket 0.4 on Windows GNU right now. I just upgraded from Rocket 0.3.17 and now I am struggling a bit with some changes introduced in 0.4.

I cannot compile the following code because it says that it would miss the implementation of FromFormValue. However, my Form struct only contains f64:

//....
#[derive(FromForm)]
pub struct ErrorCalcForm {
    pub mz: f64,
    pub error: f64,
}

/// Dalton to ppm
#[get("/datoppm?<data>")]
pub fn errorcalc_datoppm(_user: UserId, data: Form<ErrorCalcForm>, context: State<SafeContext>) -> Template {
    let mut context = context.lock().unwrap();
    context.insert("mz_theo", &Some(format!("{:.4}", data.mz)));
    context.insert("da", &Some(format!("{:.4}", data.error)));
    context.insert(
        "ppm",
        &Some(format!("{:.2}", da_to_ppm(data.mz, data.error))),
    );
    Template::render("errorcalc", &context.clone())
}
//...

The error is:

error[E0277]: the trait bound `rocket::request::Form<errorcalc::ErrorCalcForm>: rocket::request::FromFormValue<'_>` is not satisfied
  --> src\errorcalc.rs:53:41
   |
53 | pub fn errorcalc_datoppm(_user: UserId, data: Form<ErrorCalcForm>, context: State<SafeContext>) -> Template {
   |                                         ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `rocket::request::FromFormValue<'_>` is not implemented for `rocket::request::Form<errorcalc::ErrorCalcForm>`

According to the documentation, FromFormValue is implemented for f64. Why does it not work, then?

Thx for any help!

invalid

All 3 comments

This is related to the query string handling changes (second in the list at https://github.com/SergioBenitez/Rocket/blob/master/CHANGELOG.md#breaking-changes).

The problem here is that ...?<data> now means "one value" (FromFormValue), but Form<T> is "multiple values" and doesn't implement that. You probably want ...?<data..> instead.

Forgot to mention: You could also use "?<mz>&<error>" with mz and error declared in the route instead of a separate form type.

This is the second bullet point of the CHANGELOG.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GoRustafari picture GoRustafari  路  3Comments

paulvt picture paulvt  路  4Comments

lambda-fairy picture lambda-fairy  路  4Comments

loothood picture loothood  路  4Comments

sphinxc0re picture sphinxc0re  路  3Comments