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

^ the trait Default is not implemented for chrono::Duration #1297

Closed
akaNightmare opened this issue Sep 17, 2023 · 1 comment
Closed

^ the trait Default is not implemented for chrono::Duration #1297

akaNightmare opened this issue Sep 17, 2023 · 1 comment

Comments

@akaNightmare
Copy link

Hello, received an error, and I assume the default for chrono::Duration should be Duration::zero(), anyway I tried to wrap Duration into my struct and implement Default trait for that and have no luck.
Thanks

use serde::{Deserialize, Serialize};
use chrono::Duration;

#[derive(Debug, Serialize, Deserialize)]
struct MyStruct {
    #[serde(skip)]
    duration: Duration,
    f: i32,
}

fn main() {
    // Create an instance of MyStruct
    let my_struct = MyStruct {
        duration: Duration::minutes(30),
        f: 12,
    };

    // Serialize it to JSON
    let serialized = serde_json::to_string(&my_struct).unwrap();
    println!("Serialized: {}", serialized);

    // Deserialize from JSON
    let deserialized: MyStruct = serde_json::from_str(&serialized).unwrap();
    println!("Deserialized: {:?}", deserialized);
}

The error:

error[E0277]: the trait bound `chrono::Duration: Default` is not satisfied
 --> src/main.rs:6:5
  |
6 |     #[serde(skip)]
  |     ^ the trait `Default` is not implemented for `chrono::Duration`
@pitdicker
Copy link
Collaborator

pitdicker commented Sep 17, 2023

@akaNightmare For a long time we could not implement Default, because chrono::Duration was (depending on a feature flag) a re-export of time 0.1 time::Duration. A bit more than a week ago we made a release to change this, see https://github.com/chronotope/chrono/releases/tag/v0.4.30. This was a tricky release, but we haven't gotten negative feedback yet.

Hopefully soon #1137 can be merged that adds features to chrono::Duration such as this.

For now closing as a duplicate of #717.

@pitdicker pitdicker closed this as not planned Won't fix, can't repro, duplicate, stale Sep 17, 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