Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot (de-)serialize rational64 with serde #269

Closed
MalteSchledjewski opened this issue Sep 13, 2021 · 4 comments · Fixed by #272
Closed

Cannot (de-)serialize rational64 with serde #269

MalteSchledjewski opened this issue Sep 13, 2021 · 4 comments · Fixed by #272

Comments

@MalteSchledjewski
Copy link

Hello,

I am trying to (de-)serialize a uom::si::rational64::Frequency with serde but I cannot get it working.

Cargo.toml

[package]
name = "uom-serde"
version = "0.1.0"
edition = "2018"

[dependencies]
uom = { version = "0.31.1", default-features = false, features = [ "autoconvert", "rational64", "si", "std", "use_serde"]}
serde = { version = "1.0.130", features = ["derive"] }

lib.rs

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub struct Dummy {
    pub frequency: uom::si::rational64::Frequency,
}

Error:

cargo build
   Compiling uom v0.31.1
   Compiling uom-serde v0.1.0 
error[E0277]: the trait bound `Ratio<i64>: Serialize` is not satisfied
 --> src\lib.rs:5:5
  |
5 |     pub frequency: uom::si::rational64::Frequency,
  |     ^^^ the trait `Serialize` is not implemented for `Ratio<i64>`
  |
  = note: required because of the requirements on the impl of `Serialize` for `Quantity<dyn Dimension<M = Z0, J = Z0, N = Z0, I = Z0, T = NInt<UInt<UTerm, B1>>, L = Z0, Th = Z0, Kind = (dyn Kind + 'static)>, dyn uom::si::Units<Ratio<i64>, length = uom::si::length::meter, 
electric_current = uom::si::electric_current::ampere, luminous_intensity = uom::si::luminous_intensity::candela, thermodynamic_temperature = uom::si::thermodynamic_temperature::kelvin, mass = uom::si::mass::kilogram, time = uom::si::time::second, amount_of_substance = uom::si::amount_of_substance::mole>, Ratio<i64>>`
  = note: required by `_::_serde::ser::SerializeStruct::serialize_field`

error[E0277]: the trait bound `Ratio<i64>: Deserialize<'_>` is not satisfied
 --> src\lib.rs:5:5
  |
5 |     pub frequency: uom::si::rational64::Frequency,
  |     ^^^ the trait `Deserialize<'_>` is not implemented for `Ratio<i64>`
  |
  = note: required because of the requirements on the impl of `Deserialize<'_>` for `Quantity<(dyn Dimension<M = Z0, J = Z0, N = Z0, I = Z0, T = NInt<UInt<UTerm, B1>>, L = Z0, Th = Z0, Kind = (dyn Kind + 'static)> + 'static), (dyn uom::si::Units<Ratio<i64>, length = uom::si::length::meter, electric_current = uom::si::electric_current::ampere, luminous_intensity = uom::si::luminous_intensity::candela, thermodynamic_temperature = uom::si::thermodynamic_temperature::kelvin, mass = uom::si::mass::kilogram, time = uom::si::time::second, amount_of_substance = uom::si::amount_of_substance::mole> + 'static), Ratio<i64>>`
  = note: required by `next_element`

error[E0277]: the trait bound `Ratio<i64>: Deserialize<'_>` is not satisfied
 --> src\lib.rs:5:5
  |
5 |     pub frequency: uom::si::rational64::Frequency,
  |     ^^^ the trait `Deserialize<'_>` is not implemented for `Ratio<i64>`
  |
  = note: required because of the requirements on the impl of `Deserialize<'_>` for `Quantity<(dyn Dimension<M = Z0, J = Z0, N = Z0, I = Z0, T = NInt<UInt<UTerm, B1>>, L = Z0, Th = Z0, Kind = (dyn Kind + 'static)> + 'static), (dyn uom::si::Units<Ratio<i64>, length = uom::si::length::meter, electric_current = uom::si::electric_current::ampere, luminous_intensity = uom::si::luminous_intensity::candela, thermodynamic_temperature = uom::si::thermodynamic_temperature::kelvin, mass = uom::si::mass::kilogram, time = uom::si::time::second, amount_of_substance = uom::si::amount_of_substance::mole> + 'static), Ratio<i64>>`
  = note: required by `next_value`

error: aborting due to 3 previous errors

As far as I can see from the Cargo.lock file, the serde feature of num-rational is not activated.

@adamreichold
Copy link
Contributor

As far as I can see from the Cargo.lock file, the serde feature of num-rational is not activated.

You should be able to work around this by manually enabling the feature by adding

num-rational = { version = "*", features = ["serde"] }

to your Cargo.toml.

I suspect "num-rational/serde" is missing from the feature list at https://github.com/iliekturtles/uom/blob/master/Cargo.toml#L80

@adamreichold
Copy link
Contributor

While I opened #270 I see now that forwarding a feature to an optional dependency will also enable the dependency itself. (The so-called "Weak Dependency Feature" Cargo feature is still unstable.)

I therefore wonder whether this should be considered a usability issue and the documentation in the README be extended instead of the above?

@iliekturtles
Copy link
Owner

I'm leaning towards the second solution so that enabling serde doesn't pull in additional optional dependencies. Of course this makes it more complicated for users who need serde with storage types like rational64. Other thoughts?

@adamreichold
Copy link
Contributor

adamreichold commented Mar 15, 2022

@iliekturtles Friendly ping about considering the README paragraph from #272 for merging. Even if another solution like weak dependency features is chosen eventually, documenting this probably won't hurt until then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants