Hi I am new to Rust and I am currently trying to implement a system where a collection of elements
can produce , filter and consume data from each other in a pipeline. (Think a simple GStreamer)
So each element will have a send and recv channel. So a file source element as shown below will read a file and send the data into the channel in a separate thread. Eventually I will connect another element that will read from this channel in a different thread.
However, I am having trouble sending to the channels in threads.
The errors I am getting are
the trait std::marker::Sync
is not implemented for std::sync::mpsc::Sender<std::string::Stri ng>
I have read some similar threads but am still having trouble.
I am guessing my design is wrong here.
Below I am just showing a simple element sending for simplicity.
Thanks
struct Element {
comm : Arc<(Sender<String>, Receiver<String>)>,
}
impl Element {
pub fn new(name : String) -> Self {
Element{
comm : Arc::new(mpsc::channel()),
}
}
}
struct FileSourceElement {
base : Element,
file: File,
}
impl FileSourceElement {
fn new(name: String, file : File) -> Self {
let mut element = FileSourceElement { base : Element::new(name), file : file };
element
}
fn run(&self) {
let mut local_comm = self.base.comm.clone();
// Spawn off an expensive computation
thread::spawn(move|| {
// Read from file and stream data to the output pad.
let mut reader = BufReader::new(&self.file);
for line in reader.lines() {
let l = line.unwrap();
local_comm.0.send(l).unwrap();
}
});
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
use ::Element;
use ::FileSourceElement;
use std::fs::File;
match File::open("data/test.txt") {
Ok(file) => {
let file_source_element = FileSourceElement::new("My Source".to_string(), file);
file_source_element.run();
},
Err(e) => {
// fallback in case of failure.
// you could log the error, panic, or do anything else.
println!("{}", e);
}
};
}
}