multithreading - Calling an FnMut callback from another thread -


i writing phoenix client library rust, taking advantage of async websocket client rust-websockets. right having trouble figuring out how pass callback functions in thread handling websocket traffic. have simplified struct:

pub struct socket {     endpoint:               string,     connected:              arc<atomicbool>,     state_change_close:     option<box<fnmut(string)>>, } 

this struct has connect function laid out follows:

pub fn connect(&mut self) -> result<(), string> {     if self.connected.load(ordering::relaxed) {         return ok(())     }      // copy endpoint string, otherwise error on thread::spawn     let connection_string = self.endpoint.clone();     let (usr_msg, stdin_ch) = mpsc::channel(0);      let connection_thread = thread::spawn(move || {         // tokio core running event loop         let mut core = core::new().unwrap();         let runner = clientbuilder::new(&connection_string)             .unwrap()             .add_protocol("rust-websocket")             .async_connect_insecure(&core.handle())             .and_then(|(duplex, _)| {                 let (sink, stream) = duplex.split();                 stream.filter_map(|message| {                     println!("received message: {:?}", message);                     match message {                         ownedmessage::close(e) => {                             // line trying call callback                             if let some(ref mut func) = self.state_change_close {                                 (func)(e.unwrap().reason);                             }                             some(ownedmessage::close(e))                         },                         _ => none,                     }                 })                 .select(stdin_ch.map_err(|_| websocketerror::nodataavailable))                 .forward(sink)             });          // start event loop         core.run(runner).unwrap();     });     self.connected.store(true, ordering::relaxed);     return ok(()) } 

when try compile code following error:

error[e0277]: trait bound `std::ops::fnmut(std::string::string) + 'static: std::marker::send` not satisfied   --> src\socket.rs:99:29    | 99 |     let connection_thread = thread::spawn(move || {    |                             ^^^^^^^^^^^^^ trait `std::marker::send` not implemented `std::ops::fnmut(std::string::string) + 'static`    | 

i have tried changing type of state_change_close mutex<option<...>> avoid thread safety issues, did not problem. i'm trying possible?

after doing more research realized had modify option<box<fnmut(string)>> option<box<fnmut(string) + send>> , copy around code everywhere callback might set. learning more trait objects!


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -