Skip to content

Commit 1a745c5

Browse files
authored
Implement std::error::Error for thread_priority::Error (#35)
1 parent aa2d197 commit 1a745c5

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,21 @@ pub enum Error {
196196
Ffi(&'static str),
197197
}
198198

199+
impl std::fmt::Display for Error {
200+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
201+
match self {
202+
Error::Priority(s) => write!(f, "unable to set priority: {}", s),
203+
Error::PriorityNotInRange(range) => {
204+
write!(f, "priority must be within the range: {:?}", range)
205+
}
206+
Error::OS(i) => write!(f, "the operating system returned error code {}", i),
207+
Error::Ffi(s) => write!(f, "FFI error: {}", s),
208+
}
209+
}
210+
}
211+
212+
impl std::error::Error for Error {}
213+
199214
/// Platform-independent thread priority value.
200215
/// Should be in `[0; 100)` range. The higher the number is - the higher
201216
/// the priority.

tests/common.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use rstest::rstest;
22

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

1010
#[rstest]

0 commit comments

Comments
 (0)