From bd899d02e9530577e2d12f80b8defe3ef40144cf Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Fri, 7 Jun 2019 14:12:54 -0700 Subject: [PATCH] Make `i*::signum` a `const fn`. This uses a well-known branchless implementation of `signum`. Its `const`-ness is unstable and requires `#![feature(const_int_sign)]`. --- src/libcore/num/mod.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index dd7090623f5..304b2fc9ebb 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -1993,13 +1993,10 @@ pub fn abs(self) -> Self { $EndFeature, " ```"), #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_int_sign")] #[inline] - pub fn signum(self) -> Self { - match self { - n if n > 0 => 1, - 0 => 0, - _ => -1, - } + pub const fn signum(self) -> Self { + (self > 0) as Self - (self < 0) as Self } } -- 2.44.0