-
Notifications
You must be signed in to change notification settings - Fork 5
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 #40 from oqc-community/jd/cleanup
General cleanups
- Loading branch information
Showing
12 changed files
with
112 additions
and
116 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
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
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,30 +1,27 @@ | ||
use crate::runtime::ActiveTracers; | ||
|
||
pub struct RasqalConfig { | ||
/// How many steps the symbolic executor is allowed to make before failing. | ||
/// | ||
pub step_count_limit: Option<i64>, | ||
pub debug_tracers: ActiveTracers | ||
/// How many steps the symbolic executor is allowed to make before failing. | ||
/// | ||
pub step_count_limit: Option<i64>, | ||
pub debug_tracers: ActiveTracers | ||
} | ||
|
||
impl RasqalConfig { | ||
pub fn step_count_limit(&mut self, count: i64) { | ||
self.step_count_limit = Some(count); | ||
} | ||
|
||
pub fn trace_runtime(&mut self) { self.debug_tracers.insert(ActiveTracers::Runtime); } | ||
pub fn step_count_limit(&mut self, count: i64) { self.step_count_limit = Some(count); } | ||
|
||
pub fn trace_projections(&mut self) { self.debug_tracers.insert(ActiveTracers::Projections); } | ||
pub fn trace_runtime(&mut self) { self.debug_tracers.insert(ActiveTracers::Runtime); } | ||
|
||
pub fn trace_graphs(&mut self) { self.debug_tracers.insert(ActiveTracers::Graphs); } | ||
pub fn trace_projections(&mut self) { self.debug_tracers.insert(ActiveTracers::Projections); } | ||
|
||
pub fn trace_graphs(&mut self) { self.debug_tracers.insert(ActiveTracers::Graphs); } | ||
} | ||
|
||
impl Default for RasqalConfig { | ||
fn default() -> Self { | ||
RasqalConfig { | ||
step_count_limit: None, | ||
debug_tracers: ActiveTracers::empty() | ||
} | ||
fn default() -> Self { | ||
RasqalConfig { | ||
step_count_limit: None, | ||
debug_tracers: ActiveTracers::empty() | ||
} | ||
} | ||
} | ||
} |
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,60 +1,55 @@ | ||
use std::{panic}; | ||
use std::panic::{AssertUnwindSafe}; | ||
use std::panic; | ||
use std::panic::AssertUnwindSafe; | ||
|
||
/// Because we are primarily interfaced with from Python we want to make sure we don't panic | ||
/// and kill the external process. This is also important because we have errors that occur in places | ||
/// that [`Results`] cannot be used, and when we fail other systems should then respond. | ||
/// | ||
/// This just wraps the lambda call in [`panic::catch_unwind`] and folds the panic message into | ||
/// the expected error. | ||
pub fn catch_panics<R, F: FnOnce() -> Result<R, String>>(wrapped: F) -> Result<R, String> { | ||
let result = panic::catch_unwind(AssertUnwindSafe(wrapped)); | ||
match result { | ||
Ok(thread_result) => { | ||
if let Ok(result) = thread_result { | ||
Ok(result) | ||
} else { | ||
Err(thread_result.err().unwrap()) | ||
} | ||
} | ||
Err(panic_value) => { | ||
Err(if let Some(message) = panic_value.downcast_ref::<&str>() { | ||
message.to_string() | ||
} else { | ||
"Unavailable error message.".to_string() | ||
}) | ||
} | ||
pub fn catch_panics<R, F: FnOnce() -> Result<R, String>>(wrapped: F) -> Result<R, String> { | ||
let result = panic::catch_unwind(AssertUnwindSafe(wrapped)); | ||
match result { | ||
Ok(thread_result) => { | ||
if let Ok(result) = thread_result { | ||
Ok(result) | ||
} else { | ||
Err(thread_result.err().unwrap()) | ||
} | ||
} | ||
Err(panic_value) => Err( | ||
if let Some(message) = panic_value.downcast_ref::<String>() { | ||
message.clone() | ||
} else if let Some(message) = panic_value.downcast_ref::<&str>() { | ||
message.to_string() | ||
} else { | ||
"Unavailable error message.".to_string() | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::exceptions::catch_panics; | ||
use crate::exceptions::catch_panics; | ||
|
||
#[test] | ||
fn success() { | ||
let result = catch_panics(|| { | ||
Ok("dave") | ||
}); | ||
#[test] | ||
fn success() { | ||
let result = catch_panics(|| Ok("dave")); | ||
|
||
assert!(result.is_ok() && result.ok().unwrap() == "dave") | ||
} | ||
assert!(result.is_ok() && result.ok().unwrap() == "dave") | ||
} | ||
|
||
#[test] | ||
fn panic() { | ||
let result: Result<(), String> = catch_panics(|| { | ||
panic!("Ahhh?!") | ||
}); | ||
#[test] | ||
fn panic() { | ||
let result: Result<(), String> = catch_panics(|| panic!("Ahhh?!")); | ||
|
||
assert!(result.is_err() && result.err().unwrap().as_str() == "Ahhh?!") | ||
} | ||
assert!(result.is_err() && result.err().unwrap().as_str() == "Ahhh?!") | ||
} | ||
|
||
#[test] | ||
fn error() { | ||
let result: Result<(), String> = catch_panics(|| { | ||
Err("Eh.".into()) | ||
}); | ||
#[test] | ||
fn error() { | ||
let result: Result<(), String> = catch_panics(|| Err("Eh.".into())); | ||
|
||
assert!(result.is_err() && result.err().unwrap().as_str() == "Eh.") | ||
} | ||
assert!(result.is_err() && result.err().unwrap().as_str() == "Eh.") | ||
} | ||
} |
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
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
Oops, something went wrong.