【问题标题】:`*arg0` does not live long enough - wasm_bindgen`*arg0` 寿命不够长 - wasm_bindgen
【发布时间】:2020-12-17 14:44:25
【问题描述】:

运行以下代码时:

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 wasm-bindgen


    【解决方案1】:

    当 Rust 假设 return_strings 返回的未来与 _input 具有相同的生命周期时,即使它实际上并没有从中借用,也会导致错误消息。基本上,去糖函数签名看起来像:

    pub fn return_strings<'a>(_input: &'a str) -> impl Future<Output = JsValue> + 'a;
    

    生成的代码想要一个具有'static 生命周期的future,但返回的future 实际上以一个临时字符串变量的生命周期结束。

    The developers are aware of this limitation。可能最简单的解决方案是使用拥有的 StringBox&lt;str&gt; 参数,而不是借用的 &amp;str

    【讨论】:

      猜你喜欢
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 2017-03-09
      相关资源
      最近更新 更多