如何在 num 库的 BigInt 结构上实现 Deserialize 特征?

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

我正在使用 toml 来解析数据,我有这个结构:

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,
}

我想用 BigInt, a struct from the num 库替换整数 (i64)。

这可能吗?我必须自己实现 Deserialize 特性吗?

一般来说,你不能。有关详细信息,请参阅 How do I implement a trait I don't own for a type I don't own?

对于您的具体情况,num 已经有一个 feature

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

所以就用它吧:

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