`*arg0` 寿命不够长 - wasm_bindgen
`*arg0` does not live long enough - wasm_bindgen
当运行如下代码:
Cargo.toml
[lib]
crate-type = ["cdylib"]
[dependencies]
serde = { version = "1.0", features = ["derive"] }
wasm-bindgen = {version = "0.2.67", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4.17"
lib.rs
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
#[derive(Serialize, Deserialize)]
struct Output {
strings: Vec<String>,
}
#[wasm_bindgen] //error occuring here
pub async fn return_strings(_input: &str) -> JsValue {
//example function that returns a js value
let strings: Vec<String> = Default::default();
let output = Output { strings };
JsValue::from_serde(&output).unwrap()
}
我收到以下错误:
*arg0
活得不够长
借用的价值没有足够长的时间争论要求 *arg0
被借用 'static
如果有人能告诉我原因,我会帮上大忙。
当 Rust 假设 return_strings
返回的 future 与 _input
具有相同的生命周期时,即使它实际上并没有从中借用,也会导致错误消息。基本上,de-sugared 函数签名如下所示:
pub fn return_strings<'a>(_input: &'a str) -> impl Future<Output = JsValue> + 'a;
生成的代码想要一个具有 'static
生命周期的未来,但返回的未来实际上以一个临时字符串变量的生命周期结束。
The developers are aware of this limitation。可能最简单的解决方案是采用自有的 String
或 Box<str>
参数而不是借用的 &str
.
当运行如下代码:
Cargo.toml
[lib]
crate-type = ["cdylib"]
[dependencies]
serde = { version = "1.0", features = ["derive"] }
wasm-bindgen = {version = "0.2.67", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4.17"
lib.rs
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
#[derive(Serialize, Deserialize)]
struct Output {
strings: Vec<String>,
}
#[wasm_bindgen] //error occuring here
pub async fn return_strings(_input: &str) -> JsValue {
//example function that returns a js value
let strings: Vec<String> = Default::default();
let output = Output { strings };
JsValue::from_serde(&output).unwrap()
}
我收到以下错误:
*arg0
活得不够长
借用的价值没有足够长的时间争论要求 *arg0
被借用 'static
如果有人能告诉我原因,我会帮上大忙。
当 Rust 假设 return_strings
返回的 future 与 _input
具有相同的生命周期时,即使它实际上并没有从中借用,也会导致错误消息。基本上,de-sugared 函数签名如下所示:
pub fn return_strings<'a>(_input: &'a str) -> impl Future<Output = JsValue> + 'a;
生成的代码想要一个具有 'static
生命周期的未来,但返回的未来实际上以一个临时字符串变量的生命周期结束。
The developers are aware of this limitation。可能最简单的解决方案是采用自有的 String
或 Box<str>
参数而不是借用的 &str
.