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

[usage question] Using DurationSeconds in a hashmap #661

Closed
rollo-b2c2 opened this issue Nov 7, 2023 · 1 comment
Closed

[usage question] Using DurationSeconds in a hashmap #661

rollo-b2c2 opened this issue Nov 7, 2023 · 1 comment

Comments

@rollo-b2c2
Copy link

Hey this is a usage question,

Trying to do this?

use std::collections::HashMap;
use std::hash::Hash;
use serde_with;
use serde;
use serde::{Deserialize};
use serde_with::serde_as;
use serde_with::DurationSeconds;

#[serde_as]
#[derive(Deserialize)]
struct T {
    #[serde_as(as = "HashMap<DurationSeconds, f64>")]
    pub a: HashMap<std::time::Duration, f64>
}

But I'm getting a compiler error.

error[E0277]: the trait bound `HashMap<DurationSeconds, f64>: DeserializeAs<'_, _>` is not satisfied
   --> src/main.rs:10:1
    |
10  |   #[serde_as]
    |   ^----------
    |   |
    |  _in this procedural macro expansion
    | |
11  | | #[derive(Deserialize)]
    | |____________________^ the trait `DeserializeAs<'_, _>` is not implemented for `HashMap<DurationSeconds, f64>`
    |
    = help: the following other types implement trait `DeserializeAs<'de, T>`:
              <HashMap<KAs, VAs> as DeserializeAs<'de, BTreeSet<(K, V)>>>
              <HashMap<KAs, VAs> as DeserializeAs<'de, HashSet<(K, V), S>>>
              <HashMap<KAs, VAs> as DeserializeAs<'de, BinaryHeap<(K, V)>>>
              <HashMap<KAs, VAs> as DeserializeAs<'de, Box<[(K, V)]>>>
              <HashMap<KAs, VAs> as DeserializeAs<'de, LinkedList<(K, V)>>>
              <HashMap<KAs, VAs> as DeserializeAs<'de, Vec<(K, V)>>>
              <HashMap<KAs, VAs> as DeserializeAs<'de, VecDeque<(K, V)>>>
              <HashMap<KAs, VAs> as DeserializeAs<'de, std::option::Option<(K, V)>>>
            and 2 others
note: required by a bound in `serde_with::de::<impl As<T>>::deserialize`
   --> /Users/rollokonig-brock/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde_with-3.4.0/src/de/mod.rs:153:12
    |
151 |     pub fn deserialize<'de, D, I>(deserializer: D) -> Result<I, D::Error>
    |            ----------- required by a bound in this associated function
152 |     where
153 |         T: DeserializeAs<'de, I>,
    |            ^^^^^^^^^^^^^^^^^^^^^ required by this bound in `serde_with::de::<impl As<T>>::deserialize`
    = note: this error originates in the attribute macro `serde_as` (in Nightly builds, run with -Z macro-backtrace for more info)

I'm assuming I'm misunderstanding how to use this. Any pointers?

@rollo-b2c2 rollo-b2c2 changed the title Using DurationSeconds in a hashmap [usage question] Using DurationSeconds in a hashmap Nov 7, 2023
@jonasbb
Copy link
Owner

jonasbb commented Nov 12, 2023

The problem is with the f64 not the DurationSeconds. There is no DeserializeAs implementation for f64 that matches here. That is why you get the error. For the f64 you want to use the default Deserialize implementation. You do this by using _. That is an unfortunate papercut, see #202.

This works:

#[serde_as]
#[derive(Deserialize)]
struct T {
    #[serde_as(as = "HashMap<DurationSeconds, _>")]
    pub a: HashMap<std::time::Duration, f64>
}

@jonasbb jonasbb closed this as completed Nov 12, 2023
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

2 participants