我正在尝试测试我设置的用于 Dart、JS 和 Python UI 框架的 JSON 桥。它对那些工作正常,但是当我尝试使用 tui-rs 在 Rust 程序中进行相同的 UI/逻辑拆分时,在尝试反序列化 UI 线程上的逻辑线程结果时出现生命周期错误。
我知道使用 JSON 在两个用 Rust 编写的层之间进行通信并不是理想的做事方式,但考虑到我的目标,我希望它是可以理解的。
我尝试过克隆,它适用于将事物序列化并从 UI 发送到逻辑,但这不适用于从逻辑反序列化到 UI
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
use serde::{Deserialize, Serialize};
#[macro_export]
macro_rules! BridgeResult {
($result:expr, $data:expr) => {
BridgeResult {
result: $result,
data: vec![$data.to_string()],
}
};
}
#[derive(Serialize, Deserialize)]
struct BridgeResult {
result: &'static str,
data: Vec<String>,
}
#[derive(Serialize, Deserialize)]
struct App {
state: i64,
}
impl Default for App {
fn default() -> App {
App { state: 0 }
}
}
fn main() {
let (to_logic, from_ui) = mpsc::channel();
let (to_ui, from_logic) = mpsc::channel();
ui(to_logic, from_logic);
logic(to_ui, from_ui);
}
fn ui(tx: Sender<(String, String)>, rx: Receiver<(String)>) {
let app = App::default();
let app_string = serde_json::to_string(&app)
.expect("failed to encode app struct for sending to logic heard");
tx.send(("binary_switch".to_string(), app_string))
.expect("failed to send binary_switch call and data to logic thread");
let output_string = rx
.recv()
.expect("failed to get a result from logic's initialize");
let output: BridgeResult = serde_json::from_str(&output_string)
.expect("failed to decode result from logic's binary_switch");
if output.result != "Ok()" {
panic!("init failed due to: \n {:?}", output.data);
} else {
println!("{:?}", output.data);
}
}
fn logic(tx: Sender<(String)>, rx: Receiver<(String, String)>) {
loop {
let (function, arguments) = rx
.recv()
.expect("failed to receive function and arguments from ui thread");
let result = match function.as_str() {
"binary_switch" => binary_switch(&arguments),
_ => {
BridgeResult! {"Err()", format!("cannot find rust function branch matching {}", function)}
}
};
let output = match serde_json::to_string(&result) {
Ok(output) => output,
Err(_) => "{'result' : 'Err()', 'data': 'failed exit encoding!!!'}".to_string(),
};
tx.send(output)
.expect("failed to send the output back to the ui thread");
}
}
fn binary_switch(data: &String) -> BridgeResult {
#[derive(Deserialize)]
struct Arguments {
state: i64,
}
let mut arguments: Arguments = match serde_json::from_str(&data) {
Ok(data) => data,
Err(err) => return BridgeResult! {"Err()", format!("failed to parse arguments\n, {}", err)},
};
if arguments.state == 0 {
arguments.state += 1;
} else {
arguments.state -= 1;
}
BridgeResult! {"Ok()", arguments.state}
}
我希望这能够反序列化BridgeResult
类型并使用其中应该包含字符串 1 的数据字段。实际上我得到:
error[E0597]: `output_string` does not live long enough
--> src/main.rs:55:53
|
55 | let output: BridgeResult = serde_json::from_str(&output_string)
| ---------------------^^^^^^^^^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `output_string` is borrowed for `'static`
...
63 | }
| - `output_string` dropped here while still borrowed