-// Rust-101, Part 12: Concurrency (WIP)
-// =================
+// Rust-101, Part 12: Concurrency, Arc, Send
+// =========================================
use std::io::prelude::*;
use std::{io, fs, thread};
// Before we come to the actual code, we define a data-structure `Options` to store all the information we need
// to complete the job: Which files to work on, which pattern to look for, and how to output. <br/>
-// Besides just printing all the matching lines, we will also offer to count them, or alternatively to sort them.
#[derive(Clone,Copy)]
pub enum OutputMode {
Print,
}
-// The first functions reads the files, and sends every line over the `out_channel`.
+// The first function reads the files, and sends every line over the `out_channel`.
fn read_files(options: Arc<Options>, out_channel: SyncSender<String>) {
for file in options.files.iter() {
// First, we open the file, ignoring any errors.
// The second function filters the lines it receives through `in_channel` with the pattern, and sends
// matches via `out_channel`.
-fn filter_lines(options: Arc<Options>, in_channel: Receiver<String>, out_channel: SyncSender<String>) {
+fn filter_lines(options: Arc<Options>,
+ in_channel: Receiver<String>,
+ out_channel: SyncSender<String>) {
// We can simply iterate over the channel, which will stop when the channel is closed.
for line in in_channel.iter() {
// `contains` works on lots of types of patterns, but in particular, we can use it to test whether
- // one string is contained in another.
+ // one string is contained in another. This is another example of Rust using traits as substitute for overloading.
if line.contains(&options.pattern) {
unimplemented!()
}
// We move the `options` into an `Arc`, as that's what the thread workers expect.
let options = Arc::new(options);
- // Set up the channels. Use `sync_channel` with buffer-size of 16 to avoid needlessly filling RAM.
+ // This sets up the channels. We use a `sync_channel` with buffer-size of 16 to avoid needlessly filling RAM.
let (line_sender, line_receiver) = sync_channel(16);
let (filtered_sender, filtered_receiver) = sync_channel(16);
// Same with the filter thread.
let options2 = options.clone();
- let handle2 = thread::spawn(move || filter_lines(options2, line_receiver, filtered_sender));
+ let handle2 = thread::spawn(move || {
+ filter_lines(options2, line_receiver, filtered_sender)
+ });
// And the output thread.
let options3 = options.clone();
// Now we have all the pieces together for testing our rgrep with some hard-coded options.
pub fn main() {
let options = Options {
- files: vec!["src/part10.rs".to_string(), "src/part11.rs".to_string(), "src/part12.rs".to_string()],
+ files: vec!["src/part10.rs".to_string(),
+ "src/part11.rs".to_string(),
+ "src/part12.rs".to_string()],
pattern: "let".to_string(),
output_mode: Print
};
run(options);
}
-// **Exercise 12.1**: Change rgrep such that it prints now only the matching lines, but also the name of the file
+// **Exercise 12.1**: Change rgrep such that it prints not only the matching lines, but also the name of the file
// and the number of the line in the file. You will have to change the type of the channels from `String` to something
// that records this extra information.