我正在尝试使用Rust-web3 API,并将所有与web3相关的功能放在main.rs块之外的单独模块中(例如,ethereum.rs),但是遇到错误,所以我想知道是否是完全有可能。
ethereum.rs
extern crate web3;
use web3::api::Web3;
use web3::Transport;
use web3::error::Error;
use web3::transports::http::Http;
extern crate serde_json;
use serde_json::value::Value;
use web3::futures::Future;
pub fn establish_connection() -> Web3<Transport<Out: Future<Item = Value, Error = Error>>> {
let infura_address = "https://ropsten.infura.io/v3/XXXX";
let (_eloop, http) = web3::transports::Http::new(&infura_address).unwrap();
let web3 = web3::Web3::new(http);
}
错误消息:
error[E0277]: the size for values of type `(dyn web3::Transport<Out = impl web3::futures::Future> + 'static)` cannot be known at compilation time
--> src/ethereum.rs:14:1
|
14 | / pub fn establish_connection() -> Web3 <Transport<Out: Future<Item = Value, Error = Error>>>{
15 | | let infura_address = "https://ropsten.infura.io/v3/3716b01517a742c7b34a70040e3b17a1";
16 | | let (_eloop, http) = web3::transports::Http::new(&infura_address).unwrap();
17 | | let web3 = web3::Web3::new(http);
18 | | }
| |_^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `(dyn web3::Transport<Out = impl web3::futures::Future> + 'static)`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: required by `web3::Web3`
error[E0038]: the trait `web3::Transport` cannot be made into an object
--> src/ethereum.rs:14:1
|
14 | pub fn establish_connection() -> Web3 <Transport<Out: Future<Item = Value, Error = Error>>>{
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `web3::Transport` cannot be made into an object
|
= note: the trait cannot require that `Self : Sized`
edit:将外部包装箱web3添加到上面的代码中。 正如web3 doc
中所述,运输确实是一种特征答案 0 :(得分:0)
除了上面来自Shepmaster的有用评论外,我还收到了来自同事的明确而有益的回答:
因此,web3::Web3::new(http)
创建了一个类型为Web3<T>
的对象,其中T
是实现Transport的对象。在这种情况下,它是Http
,因此返回的类型是Web3<Http>
。您试图返回“任何类型” Web3<T>
,其中T
实现Transport
,这是行不通的,因为编译器需要知道函数返回的内容大小。