Skip to content

Commit

Permalink
Implement std::error::Error for thread_priority::Error (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
bronsonp committed Jan 14, 2024
1 parent aa2d197 commit 1a745c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/lib.rs
Expand Up @@ -196,6 +196,21 @@ pub enum Error {
Ffi(&'static str),
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Priority(s) => write!(f, "unable to set priority: {}", s),
Error::PriorityNotInRange(range) => {
write!(f, "priority must be within the range: {:?}", range)
}
Error::OS(i) => write!(f, "the operating system returned error code {}", i),
Error::Ffi(s) => write!(f, "FFI error: {}", s),
}
}
}

impl std::error::Error for Error {}

/// Platform-independent thread priority value.
/// Should be in `[0; 100)` range. The higher the number is - the higher
/// the priority.
Expand Down
8 changes: 4 additions & 4 deletions tests/common.rs
@@ -1,10 +1,10 @@
use rstest::rstest;

#[rstest]
fn should_be_possible_to_reset_the_same_priority() {
let current = thread_priority::get_current_thread_priority().unwrap();
let set_result = thread_priority::set_current_thread_priority(current);
assert_eq!(set_result, Ok(()));
fn should_be_possible_to_reset_the_same_priority() -> Result<(), Box<dyn std::error::Error>> {
let current = thread_priority::get_current_thread_priority()?;
thread_priority::set_current_thread_priority(current)?;
Ok(())
}

#[rstest]
Expand Down

0 comments on commit 1a745c5

Please sign in to comment.