简体   繁体   中英

How can I implement the Deserialize trait on the BigInt struct from the num library?

I'm using toml to parse data, and I have this struct:

use serde_derive::Deserialize;
use toml::value::Datetime;

#[derive(Debug, Deserialize)]
pub struct Trade {
    pub action: Action,
    pub date_time: Datetime,
    pub exchange: Exchange,
    pub fee: i64,
    pub id: Option<String>,
    pub matched: Option<bool>,
    pub price: i64,
    pub quantity: i64,
}

I'd like to replace the integers ( i64 ) with BigInt , a struct from the num library.

Is this possible? Do I have to implement the Deserialize trait myself?

In general, you cannot. See How do I implement a trait I don't own for a type I don't own? for more details

For your specific case, num already has a feature for that:

The serde feature enables serialization for types in num-bigint , num-complex , and num-rational .

so just use it:

[dependencies.num]
version = "0.3"
features = ["serde"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM