#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Priority {
Idle,
BelowNormal,
Normal,
AboveNormal,
High,
}
#[cfg(unix)]
impl Priority {
pub(crate) fn nice_value(self) -> i32 {
match self {
Priority::Idle => 19,
Priority::BelowNormal => 10,
Priority::Normal => 0,
Priority::AboveNormal => -5,
Priority::High => -10,
}
}
}
#[cfg(windows)]
impl Priority {
pub(crate) fn creation_flag(self) -> u32 {
use windows_sys::Win32::System::Threading::{
ABOVE_NORMAL_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS, HIGH_PRIORITY_CLASS,
IDLE_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS,
};
match self {
Priority::Idle => IDLE_PRIORITY_CLASS,
Priority::BelowNormal => BELOW_NORMAL_PRIORITY_CLASS,
Priority::Normal => NORMAL_PRIORITY_CLASS,
Priority::AboveNormal => ABOVE_NORMAL_PRIORITY_CLASS,
Priority::High => HIGH_PRIORITY_CLASS,
}
}
}
#[cfg(test)]
mod tests {
use super::Priority;
#[cfg(unix)]
#[test]
fn nice_value_maps_every_variant() {
assert_eq!(Priority::Idle.nice_value(), 19);
assert_eq!(Priority::BelowNormal.nice_value(), 10);
assert_eq!(Priority::Normal.nice_value(), 0);
assert_eq!(Priority::AboveNormal.nice_value(), -5);
assert_eq!(Priority::High.nice_value(), -10);
}
#[cfg(windows)]
#[test]
fn creation_flag_maps_every_variant() {
use windows_sys::Win32::System::Threading::{
ABOVE_NORMAL_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS, HIGH_PRIORITY_CLASS,
IDLE_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS,
};
assert_eq!(Priority::Idle.creation_flag(), IDLE_PRIORITY_CLASS);
assert_eq!(
Priority::BelowNormal.creation_flag(),
BELOW_NORMAL_PRIORITY_CLASS
);
assert_eq!(Priority::Normal.creation_flag(), NORMAL_PRIORITY_CLASS);
assert_eq!(
Priority::AboveNormal.creation_flag(),
ABOVE_NORMAL_PRIORITY_CLASS
);
assert_eq!(Priority::High.creation_flag(), HIGH_PRIORITY_CLASS);
}
#[test]
fn priority_is_copy_eq_and_debug() {
let p = Priority::Idle;
let copy = p; assert_eq!(p, copy);
assert_ne!(Priority::Idle, Priority::High);
assert!(format!("{:?}", Priority::Normal).contains("Normal"));
}
}