I have an issue with my code.
The main code
pub struct PermutationIteratorThread {
}
use std::thread;
#[derive(Debug, PartialEq)]
pub enum PermutationIteratorEvent<T>
{
Data( usize, Vec<T> ),
End,
}
use PermutationIteratorEvent::{Data, End};
impl PermutationIteratorThread
{
pub fn new<T, F>( input : Vec<T>, f : &'static mut F )
where
T: 'static + PartialOrd + Ord + Clone + Send,
F: 'static + FnMut(PermutationIteratorEvent<T>) + Send
{
let iterator = PermutationIterator::new(input);
let computer_handler = thread::spawn( move || {
for (index, permutation) in iterator.enumerate() {
f( Data( index, permutation ) );
}
f( End )
});
computer_handler.join();
}
}
The integration test code
extern crate permutation_way;
use permutation_way::PermutationIteratorThread;
use permutation_way::PermutationIteratorEvent::*;
use std::thread;
use std::sync::mpsc;
use std::time::Duration;
#[test]
fn should_be_coded_with_threads() {
// counter of permutation received
let mut counter = 0;
let (tx, rx) = mpsc::channel();
let input = vec![1, 2, 3];
let receiver = | event | {
println!( "evenement reçus {:?}", event );
match event {
Data( index, permutation) => {
match index {
0 => assert_eq!( vec![1, 2, 3], permutation ),
1 => assert_eq!( vec![1, 3, 2], permutation ),
2 => assert_eq!( vec![3, 1, 2], permutation ),
3 => assert_eq!( vec![3, 2, 1], permutation ),
4 => assert_eq!( vec![2, 3, 1], permutation ),
5 => assert_eq!( vec![2, 1, 3], permutation ),
// 1 -> assert_eq!( None, iterator.next() ),
// 1 -> assert_eq!( false, iterator.has_errors() ),
_ => (),
}
counter = counter + 1
},
End => {
assert_eq!( 6, counter );
tx.send( event ).unwrap()
}
}
};
// call
PermutationIteratorThread::new( input, & mut receiver );
// assertions
// wait receiving the end event
let result = rx.recv_timeout( Duration::from_secs(10) );
assert_eq!( End, result.unwrap() );
}
The error output
error[E0277]: `std::sync::mpsc::Sender<permutation_way::PermutationIteratorEvent<i32>>` cannot be shared between threads safely
--> tests\threads_it.rs:49:5
|
49 | PermutationIteratorThread::new( input, & mut receiver );
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `std::sync::mpsc::Sender<permutation_way::PermutationIteratorEvent<i32>>` cannot be shared between threads safely
|
= help: the trait `std::marker::Sync` is not implemented for `std::sync::mpsc::Sender<permutation_way::PermutationIteratorEvent<i32>>`
= note: required because of the requirements on the impl of `std::marker::Send` for `&std::sync::mpsc::Sender<permutation_way::PermutationIteratorEvent<i32>>`
= note: required because it appears within the type `[closure@tests\threads_it.rs:22:20: 45:6 counter:&mut i32, tx:&std::sync::mpsc::Sender<permutation_way::PermutationIteratorEvent<i32>>]`
= note: required by `permutation_way::PermutationIteratorThread::new`
error: aborting due to previous error
I do not understand where is the issue.
Please advise.