mirror of
https://github.com/kmc7468/cs220.git
synced 2025-12-12 21:08:45 +00:00
Assignment 12 Done (Except card.rs)
This commit is contained in:
@@ -28,11 +28,25 @@ impl<T, F: Fn(&T) -> bool> DemuxSender<T, F> {
|
||||
///
|
||||
/// If `f(&value)` is true, send `value` to `tx_true`. Otherwise, send `value` to `tx_false`.
|
||||
pub fn send(&self, value: T) -> Result<(), SendError<T>> {
|
||||
todo!()
|
||||
if (self.f)(&value) {
|
||||
self.tx_true.send(value)
|
||||
} else {
|
||||
self.tx_false.send(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Demux.
|
||||
pub fn demux<T, F: Fn(&T) -> bool>(f: F) -> (DemuxSender<T, F>, Receiver<T>, Receiver<T>) {
|
||||
todo!()
|
||||
let (tx_true, rx_true) = channel();
|
||||
let (tx_false, rx_false) = channel();
|
||||
(
|
||||
DemuxSender {
|
||||
tx_true,
|
||||
tx_false,
|
||||
f,
|
||||
},
|
||||
rx_true,
|
||||
rx_false,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -19,5 +19,18 @@ where
|
||||
T: Send + 'static,
|
||||
F: Send + Sync + Fn(&T) -> bool + 'static,
|
||||
{
|
||||
todo!()
|
||||
let tx = Arc::new(tx);
|
||||
let f = Arc::new(f);
|
||||
thread::spawn(move || {
|
||||
for rx in rxs {
|
||||
let tx = tx.clone();
|
||||
let f = f.clone();
|
||||
let _unused = thread::spawn(move || loop {
|
||||
let v = rx.recv().unwrap();
|
||||
if f(&v) {
|
||||
tx.send(v).unwrap()
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -13,7 +13,9 @@ use etrace::*;
|
||||
/// Read the `test_ping_pong` function in `small_exercises_grade.rs` to figure out what it should
|
||||
/// do.
|
||||
pub fn pong(rx1: &mut Receiver<u32>, tx2: &mut Sender<u32>) -> bool {
|
||||
todo!()
|
||||
let i = rx1.recv().unwrap();
|
||||
tx2.send(i + 1).unwrap();
|
||||
i != 99
|
||||
}
|
||||
|
||||
/// Executes the given functions (f1, f2) in concurrent and returns the results.
|
||||
@@ -31,5 +33,7 @@ where
|
||||
F1: Send + FnOnce() -> T1 + 'scope,
|
||||
F2: Send + FnOnce() -> T2 + 'scope,
|
||||
{
|
||||
todo!()
|
||||
let (t1, t2) = (s.spawn(f1), s.spawn(f2));
|
||||
let (v1, v2) = (t1.join(), t2.join());
|
||||
(v1.unwrap(), v2.unwrap())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user