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

Bincode Panic on -0 deserialization #361

Closed
kwuenscher opened this issue Apr 29, 2021 · 2 comments · Fixed by #362
Closed

Bincode Panic on -0 deserialization #361

kwuenscher opened this issue Apr 29, 2021 · 2 comments · Fixed by #362

Comments

@kwuenscher
Copy link

kwuenscher commented Apr 29, 2021

main.rs

use serde::{Serialize, Deserialize};
use rust_decimal::prelude::*;

use std::collections::HashSet;

#[derive(Deserialize, Serialize)]
pub struct Foo {
    value: Decimal,
}

fn main() {
    let s = Foo {
        value: Decimal::new(-1, 3).round_dp(0),
    };

    let _ser = bincode::serialize(&s).unwrap();
    let _des: Foo = bincode::deserialize(&ser).unwrap();
}
[dependencies]
rust_decimal_macros = { version = "1.12.2" }
rust_decimal= { version = "1.12.2", features=["serde-str"] }
bincode = "1.3.1"
serde = { version = "1.0.117", features = [ "derive" ] }

Output

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Custom("invalid value: string \"-\", expected a Decimal type representing a fixed-point number")', src/main.rs:17:47
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I believe any negative decimal < 0 with round_dp(0) panics.

@paupino
Copy link
Owner

paupino commented Apr 29, 2021

Thanks for this. Was perplexing me for a bit but discovered it was primarily to do with numbers being rounded towards 0 whilst retaining negativity. I'll push up a fix.

@kwuenscher
Copy link
Author

Awesome thanks!

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