title: Calling an unsafe library object of from a different thread
body: <p>I am using <a href="https://github.com/tikv/raft-rs" rel="nofollow noreferrer">tikv/raft-rs</a> library for implementing a consensus system. This library has a <a href="https://github.com/tikv/raft-rs/blob/master/src/raw_node.rs#L254" rel="nofollow noreferrer">RawNode</a> object which is a thread-unsafe object. We must execute some functions on this object periodically (<a href="https://github.com/tikv/raft-rs/blob/master/examples/five_mem_node/main.rs#L64" rel="nofollow noreferrer">example</a>), hence I use a new thread for executing.</p>
  <p>Here are the constraints:</p>
  <ul>
  <li>I need to have this object on the main-thread doesn't have this object for accessing some its internal states. (e.g.: <code>raw_node.is_leader</code>)</li>
  <li>This object must be accessed on a worker thread.</li>
  </ul>
  <p>Because of these constraints, this seems impossible because:</p>
  <ul>
  <li>If I create this object, and move to the new thread, the main thread cannot call its state.</li>
  <li>If I keep this object on the main thread object, I cannot use <code>Arc&lt;Mutex&lt;&gt;&gt;</code> because this object doesn't implement <code>Copy</code> method.</li>
  </ul>
  <p>Here is my code:</p>
  <pre><code>use std::thread::JoinHandle;
  use std::sync::{Arc, Mutex, mpsc};
  use std::collections::{VecDeque, HashMap};
  use raft::RawNode;
  use std::sync::mpsc::{Receiver, Sender, TryRecvError};
  
  use protobuf::Message as PbMessage;
  use raft::eraftpb::ConfState;
  use raft::storage::MemStorage;
  use raft::{prelude::*, StateRole};
  use regex::Regex;
  use crate::proposal::Proposal;
  use crate::{proposal, batch};
  use std::{str, thread};
  use std::time::{Instant, Duration};
  use crate::batch::Mailbox;
  
  pub struct Node {
      core: Arc&lt;Mutex&lt;CoreNode&gt;&gt;
  }
  
  #[derive(Copy)]
  struct CoreNode {
      raft_group: RawNode&lt;MemStorage&gt;,
  }
  
  
  impl Node {
      pub fn new() -&gt; Self {
          let cfg = Config {
              election_tick: 10,
              heartbeat_tick: 3,
              ..Default::default()
          };
          let storage = MemStorage::new();
  
          let core = Arc::new(Mutex::new(CoreNode {
              raft_group: RawNode::new(&amp;cfg, storage).unwrap()
          }));
  
          thread::spawn(move || {
              core.lock().unwrap().run();
              return;
          });
  
          Node { core: core.clone() }
      }
  
      pub fn is_leader(&amp;self) -&gt; bool {
          return self.core.lock().unwrap().raft_group.raft.state == StateRole::Leader;
  
      }
  }
  
  impl CoreNode {
      pub fn run(mut self) {}
  }
  </code></pre>
  <p>When compiling, here is the error:</p>
  <pre><code>22 | #[derive(Copy)]
     |          ^^^^
  23 | struct CoreNode {
  24 |     raft_group: RawNode&lt;MemStorage&gt;,
     |     ------------------------------- this field does not implement `Copy`
     |
  </code></pre>
  <p>My question is: How can I design around this problem.</p>