I am trying to send a message containing Cow<[u8]>
over a channel. However, the lifetime rules do not allow me to pass it over.
use std::borrow::Cow;
use std::sync::mpsc;
#[derive(Debug, Default, PartialEq, Clone)]
pub struct PlayerAction<'a> {
pub data: Cow<'a, [u8]>,
}
#[derive(Debug, Clone)]
pub enum NetworkMessage<'a> {
PlayerActionMessage(PlayerAction<'a>),
}
pub struct ConnectionsManager<'a> {
channel: mpsc::Sender<NetworkMessage<'a>>,
}
pub struct MessageHandler<'a> {
pub connection_manager: ConnectionsManager<'a>,
}
fn read_message<'a>(bytes: &'a Vec<u8>) -> NetworkMessage {
NetworkMessage::PlayerActionMessage(PlayerAction {
data: Cow::Borrowed(&bytes),
})
}
impl<'a> MessageHandler<'a> {
fn on_message(&mut self, msg: Vec<u8>) {
let readm = read_message(&msg);
self.connection_manager.channel.send(readm);
}
}
fn main() {}
error[E0597]: `msg` does not live long enough
--> src/main.rs:30:35
|
30 | let readm = read_message(&msg);
| ^^^ borrowed value does not live long enough
31 | self.connection_manager.channel.send(readm);
32 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the impl at 28:1...
--> src/main.rs:28:1
|
28 | impl<'a> MessageHandler<'a> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
MessageHandler
outlives Vec<u8>
, but I have no idea how to pass it any other way.
Is there any way to pass Vec<u8>
so that it can live longer than the on_message
function?