summaryrefslogtreecommitdiffstats
path: root/src/io
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2020-03-16 16:14:17 -0400
committerJiayi Zhao <jeff.no.zhao@gmail.com>2020-03-16 16:14:17 -0400
commitc6edd4e5d5cbcbc16063a10181aee9626822d273 (patch)
tree5524d52e5fb6f288be943c4882973dd1839a95f3 /src/io
parent50f67156b783c3f533214ddfa8e79bd979d76278 (diff)
better error handling for threads
- code cleanup
Diffstat (limited to 'src/io')
-rw-r--r--src/io/io_worker.rs25
1 files changed, 14 insertions, 11 deletions
diff --git a/src/io/io_worker.rs b/src/io/io_worker.rs
index 382d6aa..a9d2726 100644
--- a/src/io/io_worker.rs
+++ b/src/io/io_worker.rs
@@ -32,18 +32,14 @@ impl IOWorkerObserver {
let handle = thread::spawn(move || {
worker.start();
- while let Ok(evt) = worker.recv() {
- let _ = event_tx.send(evt);
+ while let Ok(copied) = worker.recv() {
+ let _ = event_tx.send(Event::IOWorkerProgress(copied));
}
- worker.handle.join();
- let _ = event_tx.send(Event::IOWorkerResult);
+ let res = worker.join();
+ let _ = event_tx.send(Event::IOWorkerResult(res));
});
- Self {
- src,
- dest,
- handle,
- }
+ Self { src, dest, handle }
}
pub fn join(self) {
@@ -56,7 +52,7 @@ pub struct IOWorkerThread {
pub dest: path::PathBuf,
pub handle: thread::JoinHandle<std::io::Result<u64>>,
pub tx_start: mpsc::Sender<()>,
- pub rx: mpsc::Receiver<Event>,
+ pub rx: mpsc::Receiver<u64>,
}
impl IOWorkerThread {
@@ -64,7 +60,14 @@ impl IOWorkerThread {
self.tx_start.send(());
}
- pub fn recv(&self) -> Result<Event, mpsc::RecvError> {
+ pub fn recv(&self) -> Result<u64, mpsc::RecvError> {
self.rx.recv()
}
+
+ pub fn join(self) -> std::io::Result<u64> {
+ match self.handle.join() {
+ Ok(s) => s,
+ Err(_) => Ok(0),
+ }
+ }
}