小编spo*_*ack的帖子

如何使用 Warp 异步路由实现共享应用程序状态?

我有一个 Rust 应用程序,使用warp. 它实现了 RESTful CRUD API。我需要每个路由处理程序(即最终由扭曲过滤器调用的函数)能够访问并(在大多数情况下)改变共享应用程序状态。

我可以编译它的唯一方法是Arc<Mutex<State>> 为每个路由克隆一个:

    /* internal_state is loaded from a dump file earlier on and is of type `State` */

    let state: Arc<Mutex<State>> = Arc::new(Mutex::new(internal_state));
    let index_book_state: Arc<Mutex<State>> = state.clone();
    let create_book_state: Arc<Mutex<State>> = state.clone();
    let read_book_state: Arc<Mutex<State>> = state.clone();

    let create_order_state: Arc<Mutex<State>> = state.clone();
    let read_order_state: Arc<Mutex<State>> = state.clone();
    let update_order_state: Arc<Mutex<State>> = state.clone();
    let destroy_order_state: Arc<Mutex<State>> = state.clone();

/* define CRUD routes for order books */
    let book_prefix = …
Run Code Online (Sandbox Code Playgroud)

mutex asynchronous crud rust rust-warp

4
推荐指数
1
解决办法
2640
查看次数

不满足标准库所需类型的“默认”特征绑定

web3我正在使用板条箱中的类型,web3::contract::Contract<web3::transports::Http>. 编译器抱怨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 …
Run Code Online (Sandbox Code Playgroud)

traits type-traits rust rust-web3

1
推荐指数
1
解决办法
600
查看次数

标签 统计

rust ×2

asynchronous ×1

crud ×1

mutex ×1

rust-warp ×1

rust-web3 ×1

traits ×1

type-traits ×1