iliekturtles / uom

Units of measurement -- type-safe zero-cost dimensional analysis

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

serde support broken ?

GeorgeKT opened this issue · comments

I enable the use_serde features in my Cargo.toml:

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

[dependencies]
ron = "0.8"
serde = { version = "1", features = ["derive"] }
uom = { version = "0.33.0", features = ["use_serde"] }

The following sample application fails to build:

use serde::{Deserialize, Serialize};
use uom::si::length::meter;

#[derive(Debug, Deserialize, Serialize)]
struct MyStruct {
boolean: bool,
float: f32,
foo: meter,
}

fn main() {
let x: MyStruct = ron::from_str("(boolean: true, float: 1.23)").unwrap();
println!("RON: {}", ron::to_string(&x).unwrap());
}

Output:

error[E0277]: the trait bound uom::si::length::meter: Deserialize<'_> is not satisfied
--> src/main.rs:8:10
|
8 | foo: meter,
| ^^^^^ the trait Deserialize<'_> is not implemented for uom::si::length::meter
|
= help: the following other types implement trait Deserialize<'de>:
&'a Path
&'a [u8]
&'a str
()
(T0, T1)
(T0, T1, T2)
(T0, T1, T2, T3)
(T0, T1, T2, T3, T4)
and 132 others
note: required by a bound in next_element
--> /home/joris/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.152/src/de/mod.rs:1729:12
|
1729 | T: Deserialize<'de>,
| ^^^^^^^^^^^^^^^^ required by this bound in next_element
...

Am I doing something wrong, or is this feature just broken ?

You need to use the quantity Length, meter is just a unit.

Well this to be specific Length, if the underlying storage type is f32.

It seems I should read the documentation more carefully, instead of throwing code at the wall using auto complete.