rust - 将 '&self' 方法中的闭包添加到结构中的属性

标签 rust closures self lifetime json-rpc

考虑以下示例代码:

#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;

extern crate jsonrpc_core as rpc;

#[derive(Serialize, Deserialize)]
struct Test {
    var: u32,
}

struct TestRpc {
    test: Test,
    rpc_io_handler: rpc::IoHandler,
}

impl TestRpc {
    fn new() -> Self {
        let ret = Self {
            test: Test { var: 1 },
            rpc_io_handler: rpc::IoHandler::new(),
        };
        ret.register_rpc_methods();
        ret
    }

    fn register_rpc_methods(&self) {
        let get_var = |_params: rpc::Params| match rpc::to_value(&self.test) {
            Ok(x) => Ok(x),
            Err(_) => Err(rpc::Error::internal_error()),
        };
        self.rpc_io_handler.add_method("get_var", get_var);
    }

    fn get_var_test(&self, msg: &str) -> Option<String> {
        self.rpc_io_handler.handle_request_sync(msg)
    }
}

fn main() {
    let test = TestRpc::new();
    let request = r#"{"jsonrpc": "2.0", "method": "get_var", "id": 1}"#;
    let response = r#"{"jsonrpc":"2.0","result":{"var":1},"id":1}"#;
    assert_eq!(test.get_var_test(request), Some(response.to_owned()));
}

使用以下“rpc::IoHandler::add_method”方法签名

pub fn add_method<F>(&mut self, name: &str, method: F)
where
    F: RpcMethodSimple,

method is from jsonrpc 原样RpcMethodSimple .

当我尝试编译时出现以下错误

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
  --> src/main.rs:26:27
   |
26 |           let mut get_var = |_params: rpc::Params | {
   |  ___________________________^
27 | |             match rpc::to_value(&self.test) {
28 | |                 Ok(x) => Ok(x),
29 | |                 Err(_) => Err(rpc::Error::internal_error())
30 | |             }
31 | |         };
   | |_________^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 25:5...
  --> src/main.rs:25:5
   |
25 | /     fn register_rpc_methods(&self) {
26 | |         let mut get_var = |_params: rpc::Params | {
27 | |             match rpc::to_value(&self.test) {
28 | |                 Ok(x) => Ok(x),
...  |
32 | |         self.rpc_io_handler.add_method("get_var", get_var);
33 | |     }
   | |_____^
   = note: ...so that the types are compatible:
           expected &&TestRpc
              found &&TestRpc
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `[closure@src/main.rs:26:27: 31:10 self:&&TestRpc]` will meet its required lifetime bounds
  --> src/main.rs:32:29
   |
32 |         self.rpc_io_handler.add_method("get_var", get_var);
   |                             ^^^^^^^^^^

是否可以使用此方法(rpc::IoHandler::add_method)而不更改 crate 中的方法?我在 Rust 中苦苦挣扎;有没有一种简单的方法来限制闭包的生命周期?

最佳答案

我对jsonrpc的内部结构不太熟悉,但是jsonrpc库是与Tokio以完全异步的方式实现的。尽管您调用同步请求处理,但在内部它仍然异步执行请求,并且只是阻塞您的线程直到完成。这样做的缺点是 Tokio 无法保证在任务执行器中安排关闭的任何事情。因此,任何此类闭包的生命周期都比任何 self 与执行者的联系更多。 .

在上面的代码中,您捕获了对 self 的引用,但不能保证 self执行闭包时仍然存在。因此你必须move闭包使用的任何数据。此外,关闭必须是 Send与 Tokio 一起使用,因此您不能简单地使用 Rc并将副本移至闭包中。

就您而言,我知道的最简单的方法是更改​​ test输入Arc<Test> 。然后更改闭包定义以将变量的副本移至闭包中。您还遇到了一些可变性问题,这是一个可以编译的完整示例:

#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;

extern crate jsonrpc_core as rpc;

use std::borrow::Borrow;
use std::sync::Arc;

#[derive(Serialize, Deserialize)]
struct Test {
    var: u32,
}

struct TestRpc {
    test: Arc<Test>,
    rpc_io_handler: rpc::IoHandler,
}

impl TestRpc {
    fn new() -> Self {
        let mut ret = Self {
            test: Arc::new(Test { var: 1 }),
            rpc_io_handler: rpc::IoHandler::new(),
        };
        ret.register_rpc_methods();
        ret
    }

    fn register_rpc_methods(&mut self) {
        let test_clone = self.test.clone();
        let get_var = move |_params: rpc::Params| match rpc::to_value(test_clone.borrow() as &Test)
        {
            Ok(x) => Ok(x),
            Err(_) => Err(rpc::Error::internal_error()),
        };
        self.rpc_io_handler.add_method("get_var", get_var);
    }

    fn get_var_test(&self, msg: &str) -> Option<String> {
        self.rpc_io_handler.handle_request_sync(msg)
    }
}

fn main() {
    let test = TestRpc::new();
    let request = r#"{"jsonrpc": "2.0", "method": "get_var", "id": 1}"#;
    let response = r#"{"jsonrpc":"2.0","result":{"var":1},"id":1}"#;
    assert_eq!(test.get_var_test(request), Some(response.to_owned()));
}

关于rust - 将 '&self' 方法中的闭包添加到结构中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53025923/

相关文章:

rust - 为什么我不能直接访问枚举字段?

rust - 如何读取bevy中的鼠标运动?

rust - 为什么 Vec<&str> 在这里缺少生命周期说明符?

rust - 返回特征的生命周期不存在于 tokio_postgres 行上并且不存在 "live long enough"

swift - Swift 中转义闭包的一个很好的例子是什么?

scala - Scala 中的 curry 和闭包

swift - 这段代码会增加引用计数吗?

python - 如何在 Python 中避免显式 'self'?

php - new self(); 是什么意思?在 PHP 中是什么意思?

javascript - Chrome 浏览器忽略 var self = this;