From b75351e98ee0a33dbd4dfe2b40e54e191d16dde5 Mon Sep 17 00:00:00 2001 From: danflapjax <12000932+danflapjax@users.noreply.github.com> Date: Thu, 10 Aug 2023 22:38:30 -0700 Subject: [PATCH] Optimized implementations of max, min, and clamp for bool --- library/core/src/cmp.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index faf48ae570fdd..3c127efb390ae 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -1406,6 +1406,22 @@ mod impls { _ => unsafe { unreachable_unchecked() }, } } + + #[inline] + fn min(self, other: bool) -> bool { + self & other + } + + #[inline] + fn max(self, other: bool) -> bool { + self | other + } + + #[inline] + fn clamp(self, min: bool, max: bool) -> bool { + assert!(min <= max); + self.max(min).min(max) + } } ord_impl! { char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }