Use singular for directory names

This commit is contained in:
Nathan Stocks
2021-06-12 23:36:49 -06:00
parent b492b359b3
commit 9848a889ea
20 changed files with 2 additions and 2 deletions

1
example/cafeteria/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

View File

@@ -0,0 +1,8 @@
[package]
name = "cafeteria"
version = "0.1.0"
authors = ["Nathan Stocks <nathan@agileperception.com>"]
edition = "2018"
[dependencies]
crossbeam = "0.8"

View File

@@ -0,0 +1,57 @@
use crossbeam::channel::{self, Receiver, Sender};
use std::{thread, time::Duration};
#[derive(Debug)]
enum Lunch {
Soup,
Salad,
Sandwich,
HotDog,
}
fn cafeteria_worker(name: &str, orders: Receiver<&str>, lunches: Sender<Lunch>) {
for order in orders {
println!("{} receives an order for {}", name, order);
let lunch = match &order {
x if x.contains("soup") => Lunch::Soup,
x if x.contains("salad") => Lunch::Salad,
x if x.contains("sandwich") => Lunch::Sandwich,
_ => Lunch::HotDog,
};
for _ in 0..order.len() {
thread::sleep(Duration::from_secs_f32(0.1))
}
println!("{} sends a {:?}", name, lunch);
if lunches.send(lunch).is_err() {
break;
}
}
}
fn main() {
let (orders_tx, orders_rx) = channel::unbounded();
let orders_rx2 = orders_rx.clone();
let (lunches_tx, lunches_rx) = channel::unbounded();
let lunches_tx2 = lunches_tx.clone();
let alice_handle = thread::spawn(|| cafeteria_worker("alice", orders_rx2, lunches_tx2));
let zack_handle = thread::spawn(|| cafeteria_worker("zack", orders_rx, lunches_tx));
for order in vec![
"polish dog",
"caesar salad",
"onion soup",
"reuben sandwich",
] {
println!("ORDER: {}", order);
let _ = orders_tx.send(order);
}
drop(orders_tx);
for lunch in lunches_rx {
println!("Order Up! -> {:?}", lunch);
}
let _ = alice_handle.join();
let _ = zack_handle.join();
}