-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from zeskeertwee/threads
Implement threads for proving, working on #18
- Loading branch information
Showing
3 changed files
with
134 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::thread::{self, JoinHandle}; | ||
|
||
pub enum AsyncResource<T> { | ||
Pending(Option<JoinHandle<T>>), | ||
Finished(T), | ||
Error(String) | ||
} | ||
|
||
impl<T> AsyncResource<T> { | ||
pub fn new<F: FnOnce() -> T>(f: F) -> Self { | ||
Self::Pending(Some(thread::spawn(f))) | ||
} | ||
|
||
pub fn poll(&mut self) -> Self { | ||
match self { | ||
AsyncResource::Pending(t) => { | ||
if t.is_finished() { | ||
match t.take().unwrap().join() { | ||
Ok(v) => *self = AsyncResource::Finished(v), | ||
Err(e) => *self = AsyncResource::Error(format!("{:?}", e)), | ||
} | ||
} | ||
}, | ||
_ => (), | ||
} | ||
} | ||
|
||
pub fn is_pending(&self) -> bool { | ||
match self { | ||
AsyncResource::Pending(_) => true, | ||
_ => false, | ||
} | ||
} | ||
|
||
pub fn is_finished(&self) -> bool { | ||
match self { | ||
AsyncResource::Finished(_) => true, | ||
AsyncResource::Error(_) => true, | ||
} | ||
} | ||
|
||
pub fn get_result(&self) -> Option<&T> { | ||
match self { | ||
AsyncResource::Finished(t) => Some(t), | ||
_ => None | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
mod gui; | ||
mod async_resource; | ||
|
||
use std::{fs::File, sync::Arc}; | ||
|
||
use eframe::NativeOptions; | ||
|