Skip to content

Commit a6d6da9

Browse files
committed
Fix the issue #38.
1. Adds the proper From<u8> implementation for ThreadPriorityValue. 2. Makes the MIN and MAX types of type Self.
1 parent 375fbf6 commit a6d6da9

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

src/lib.rs

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
target_arch = "wasm32",
159159
))]
160160
pub mod unix;
161+
use std::ops::Deref;
161162
#[cfg(any(target_os = "linux", target_os = "android"))]
162163
use std::time::Duration;
163164

@@ -243,12 +244,50 @@ impl std::error::Error for Error {}
243244
/// assert_eq!(raw_value, 0);
244245
/// ```
245246
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
246-
pub struct ThreadPriorityValue(u8);
247+
pub struct ThreadPriorityValue(pub(crate) u8);
247248
impl ThreadPriorityValue {
248249
/// The maximum value for a thread priority.
249-
pub const MAX: u8 = if cfg!(target_os = "vxworks") { 255 } else { 99 };
250+
pub const MAX: Self = Self(if cfg!(target_os = "vxworks") { 255 } else { 99 });
250251
/// The minimum value for a thread priority.
251-
pub const MIN: u8 = 0;
252+
pub const MIN: Self = Self(0);
253+
}
254+
255+
impl Deref for ThreadPriorityValue {
256+
type Target = u8;
257+
258+
fn deref(&self) -> &Self::Target {
259+
&self.0
260+
}
261+
}
262+
263+
impl PartialOrd<u8> for ThreadPriorityValue {
264+
fn partial_cmp(&self, other: &u8) -> Option<std::cmp::Ordering> {
265+
self.0.partial_cmp(other)
266+
}
267+
}
268+
269+
impl PartialOrd<ThreadPriorityValue> for u8 {
270+
fn partial_cmp(&self, other: &ThreadPriorityValue) -> Option<std::cmp::Ordering> {
271+
self.partial_cmp(&other.0)
272+
}
273+
}
274+
275+
impl PartialEq<u8> for ThreadPriorityValue {
276+
fn eq(&self, other: &u8) -> bool {
277+
self.0 == *other
278+
}
279+
}
280+
281+
impl PartialEq<ThreadPriorityValue> for u8 {
282+
fn eq(&self, other: &ThreadPriorityValue) -> bool {
283+
*self == other.0
284+
}
285+
}
286+
287+
impl std::fmt::Display for ThreadPriorityValue {
288+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
289+
write!(f, "{}", self.0)
290+
}
252291
}
253292

254293
impl std::convert::TryFrom<u8> for ThreadPriorityValue {
@@ -267,12 +306,9 @@ impl std::convert::TryFrom<u8> for ThreadPriorityValue {
267306
}
268307
}
269308

270-
// The From<u8> is unsafe, so there is a TryFrom instead.
271-
// For this reason we silent the warning from clippy.
272-
#[allow(clippy::from_over_into)]
273-
impl std::convert::Into<u8> for ThreadPriorityValue {
274-
fn into(self) -> u8 {
275-
self.0
309+
impl From<ThreadPriorityValue> for u8 {
310+
fn from(value: ThreadPriorityValue) -> Self {
311+
value.0
276312
}
277313
}
278314

src/unix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ impl ThreadPriority {
456456
// Mapping a [0..100] priority into niceness [-20..20] needs reversing the ratio,
457457
// as the lowest nice is actually the highest priority.
458458
let niceness_values = NICENESS_MAX.abs() + NICENESS_MIN.abs();
459-
let ratio = 1f32 - (p as f32 / ThreadPriorityValue::MAX as f32);
459+
let ratio = 1f32 - (p as f32 / ThreadPriorityValue::MAX.0 as f32);
460460
let niceness = ((niceness_values as f32 * ratio) as i8 + NICENESS_MAX) as i32;
461461
Self::to_allowed_value_for_policy(niceness, policy).map(|v| v as u32)
462462
}

0 commit comments

Comments
 (0)