I am struggling with sending a custom struct over channel.
I wrapped the value in an Arc
and Mutex
as described in the tutorial,
but it doesn't compile anyway.
extern crate num;
use num::bigint::BigInt;
use std::io::{self, Write};
use std::sync::mpsc;
use std::thread;
use readline::readline as ask;
use std::sync::{Arc, Mutex};
enum Command {
Quit,
Help,
Factorial { n: BigInt },
Error { msg: String },
}
fn main() {
let (input_tx, input_rx) = mpsc::channel();
let input_thread = thread::spawn(|| {
input_tx.send(Arc::new(Mutex::new(Command::Quit)));
});
}
error: the trait bound `std::sync::mpsc::Sender<std::sync::Arc<std::sync::Mutex<Command>>>: std::marker::Sync` is not satisfied [E0277]
let input_thread = thread::spawn(|| {
^~~~~~~~~~~~~
help: run `rustc --explain E0277` to see a detailed explanation
note: `std::sync::mpsc::Sender<std::sync::Arc<std::sync::Mutex<Command>>>` cannot be shared between threads safely
note: required because of the requirements on the impl of `std::marker::Send` for `&std::sync::mpsc::Sender<std::sync::Arc<std::sync::Mutex<Command>>>`
note: required because it appears within the type `[closure@src/main.rs:25:38: 65:6 input_tx:&std::sync::mpsc::Sender<std::sync::Arc<std::sync::Mutex<Command>>>]`
note: required by `std::thread::spawn`
error: aborting due to previous error
I am using Rust 1.10.0 (cfcb716cf 2016-07-03).