I'm writing a plugin for an Elgato streamdeck using this Rust package. It uses Tokio to handle communication to & from the stream deck, with the example here. I based my version nearly directly off of that, with one key difference: I need to send an object and have it live as long as the program.
Here is my code:
fn run(&mut self) {
//error points to this line
tokio::run(
self.connect
.unwrap()
.map_err(|e| error!("connection failed: {:?}", e))
.and_then(|socket| {
socket
.for_each(|message| {
handle_message(message, &mut self.conn_mgr.unwrap());
Ok(())
})
.map_err(|e| error!("read error: {:?}", e))
}),
);
}
handle_message
requires a mutable ConnectionManager
because connections are created or removed based on the messages received.
run
is implemented on this struct:
struct Runner {
//this struct needs to live as long as the program, it manages connections to an API
conn_mgr : Option<connection_manager::ConnectionManager>,
//reg_params is used to create connect
reg_params : Option<RegistrationParams>,
//this is also used in the function above, but hasn't given me issues (yet?)
connect : Option<streamdeck_rs::socket::Connect<GlobalSettings, ActionSettings, MessageFromSd, MessageToSd>>
}
Finally, my error is this:
error[E0277]: `(
dyn futures::future::Future<Item = tokio_tcp::stream::TcpStream, Error = std::io::Error>
+ std::marker::Send + 'static)`
cannot be shared between threads safely
Some of the answers I've seen talk about using a move
here to transfer ownership, but based on the error location (that is, not part of a closure), I'm not sure that's relevant. I get the same error message if I move run
outside the struct and make the parameter &mut Runner
.