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

Error with Deserialize trait bound with the serde crate #124576

Closed
brunbjerg opened this issue May 1, 2024 · 1 comment
Closed

Error with Deserialize trait bound with the serde crate #124576

brunbjerg opened this issue May 1, 2024 · 1 comment

Comments

@brunbjerg
Copy link

I tried this code:

#[derive(Deserialize, Debug)]
pub struct WeightParams {
    order_type_weights: HashMap<String, u32>,
    status_weights: HashMap<String, u32>,
    vis_priority_map: HashMap<String, u32>,
    wdf_priority_map: HashMap<String, u32>,
    wgn_priority_map: HashMap<String, u32>,
    wpm_priority_map: HashMap<String, u32>,
}

impl WeightParams {
    pub fn read_config() -> Result<Self, Box<dyn std::error::Error>> {
        let default_path = "scheduling_system/parameters/work_order_weight_parameters.json";
        let config_path = env::var("CONFIG_PATH").unwrap_or_else(|_| default_path.to_string());
        let config_contents = fs::read_to_string(config_path).expect("Could not read config file");

        let config: WeightParams = serde_json::from_str(&config_contents)?;

        Ok(config)
    }
}

I expected to see this happen: The code compiling correctly, and initializing the configuration struct WeightParams from the JSON string.

Instead, this happened: It says that the trait bound (Deserialize) is not satisfied when it clearly.

Meta

rustc --version --verbose:

rustc 1.77.2 (25ef9e3d8 2024-04-09)
binary: rustc
commit-hash: 25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04
commit-date: 2024-04-09
host: x86_64-unknown-linux-gnu
release: 1.77.2
LLVM version: 17.0.6
Backtrace

error[E0277]: the trait bound `WeightParams: serde::de::Deserialize` is not satisfied
    --> shared_messages/src/models/work_order/mod.rs:222:36
     |
222  | ...Params = serde_json::from_str(&config_contents)?;
     |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `serde::de::Deserialize` is not implemented for `WeightParams`
     |
     = help: the following other types implement trait `serde::de::Deserialize`:
               bool
               char
               isize
               i8
               i16
               i32
               i64
               usize
             and 95 others
note: required by a bound in `serde_json::from_str`
    --> /home/scipo/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde_json-0.9.10/src/de.rs:1088:14
     |
1087 | pub fn from_str<T>(s: &str) -> Result<T>
     |        -------- required by a bound in this function
1088 |     where T: de::Deserialize,
     |              ^^^^^^^^^^^^^^^ required by this bound in `from_str`

@brunbjerg brunbjerg added the C-bug Category: This is a bug. label May 1, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 1, 2024
@ChayimFriedman2
Copy link
Contributor

Can't reproduce.

@saethlin saethlin removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants