I've got code somewhat like the following, which attempts to read from a websocket, parse the JSON result into a struct, and push that struct onto a Vec
buffer. The code however fails to compile, because the struct has a lifetime, and the borrow checker complains that the JSON string does not live long enough.
use serde::{Deserialize, Serialize};
use tungstenite::client::AutoStream;
use tungstenite::protocol::WebSocket;
#[derive(Serialize, Deserialize, Debug, Clone)]
struct MyType<'a> {
id: &'a str,
count: i64,
}
fn example<'a>(
conn: &mut WebSocket<AutoStream>,
buff: &'a mut Vec<MyType<'a>>,
) -> Option<Box<dyn std::error::Error>> {
match conn.read_message() {
Err(err) => Some(err.into()),
Ok(msg) => {
let resp_raw = msg.to_string();
let resp_parsed: Result<MyType<'a>, _> = serde_json::from_str(&resp_raw);
match resp_parsed {
Err(err) => Some(err.into()),
Ok(resp) => {
buff.push(resp.clone());
None
}
}
}
}
}
The exact error is that borrowed value [&resp_raw] does not live long enough
.
I'm wondering how I should restructure this code to satisfy the borrow checker; what is the correct way to push a struct with a lifetime onto the Vec
param?
Or is it the case that the &'a str
parsed into MyType
actually still retains a reference to the original JSON string, so there's no way to safely do this?