From eee7c8651d46171e185cb481f3483242f78afee3 Mon Sep 17 00:00:00 2001 From: LI Qing Date: Wed, 19 Jun 2024 14:19:29 +0800 Subject: [PATCH] Use `Ord::clamp` to simplify the nice value --- src/libos/src/sched/priority.rs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/libos/src/sched/priority.rs b/src/libos/src/sched/priority.rs index 758a66bf..bc5d9b1a 100644 --- a/src/libos/src/sched/priority.rs +++ b/src/libos/src/sched/priority.rs @@ -39,12 +39,8 @@ impl NiceValue { /// The raw value given beyond the range are automatically adjusted /// to the nearest boundary value. pub fn new(raw: i8) -> Self { - if raw < Self::MIN.value { - Self::MIN - } else if raw > Self::MAX.value { - Self::MAX - } else { - Self { value: raw } + Self { + value: raw.clamp(Self::MIN.value, Self::MAX.value), } } @@ -56,13 +52,7 @@ impl NiceValue { impl From for NiceValue { fn from(raw: i32) -> Self { - let adj_raw = if raw > i8::MAX as i32 { - i8::MAX - } else if raw < i8::MIN as i32 { - i8::MIN - } else { - raw as i8 - }; + let adj_raw = raw.clamp(i8::MIN as i32, i8::MAX as i32) as i8; Self::new(adj_raw) } }