Reputation: 379
When running the following code:
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()
}
I get the following error:
*arg0
does not live long enough
borrowed value does not live long enoughargument requires that *arg0
is borrowed for 'static
If anyone could let me know why, I would be a huge help.
Upvotes: 3
Views: 541
Reputation: 832
The error message is caused when Rust assumes that the future returned by return_strings
has the same lifetime as _input
even though it doesn't actually borrow from it. Basically, the de-sugared function signature looks like:
pub fn return_strings<'a>(_input: &'a str) -> impl Future<Output = JsValue> + 'a;
The generated code wants a future with 'static
lifetime, but the returned future actually winds up with the lifetime of a temporary string variable.
The developers are aware of this limitation. Probably the easiest solution is to take an owned String
or Box<str>
argument instead of a borrowed &str
.
Upvotes: 2