Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

rust - Unsatisfied `Default` trait bound on type required by standard library

I'm using a type from the web3 crate, web3::contract::Contract<web3::transports::Http>. The compiler is complaining with E0277:

$ cargo run
   Compiling proj v0.1.0 (/home/user/proj)
error[E0277]: the trait bound `web3::contract::Contract<web3::transports::Http>: Default` is not satisfied
  --> src/book.rs:38:5
   |
38 | /     #[serde(skip)]
39 | |     abi: web3::contract::Contract<OMETransport>,
   | |_______________________________________________^ the trait `Default` is not implemented for `web3::contract::Contract<web3::transports::Http>`
   |
   = note: required by `std::default::Default::default`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `proj`

To learn more, run the command again with --verbose.

How can this error originate from std::default::Default::default?

The definition for the relevant type (i.e., the struct containing the web3 type) is:

/// Represents an order book for a particular market
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Book {
    market: Address, /* the address of the market */
    bids: BTreeMap<U256, VecDeque<Order>>, /* buy-side */
    asks: BTreeMap<U256, VecDeque<Order>>, /* sell-side */
    #[serde(serialize_with = "from_hex_se", deserialize_with = "from_hex_de")]
    ltp: U256, /* last traded price */
    depth: (usize, usize), /* depth  */
    crossed: bool,   /* is book crossed? */
    #[serde(serialize_with = "from_hex_se", deserialize_with = "from_hex_de")]
    spread: U256, /* bid-ask spread */
    #[serde(skip)]
    abi: web3::contract::Contract<web3::transports:Http>,
}
question from:https://stackoverflow.com/questions/65930855/unsatisfied-default-trait-bound-on-type-required-by-standard-library

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

the serde documentation for #[serde(skip)] says:

When deserializing, Serde will use Default::default() or the function given by default = "..." to get a default value for this field.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...