Skip to content

Commit bd899d0

Browse files
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)]`.
1 parent c8865d8 commit bd899d0

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

src/libcore/num/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1993,13 +1993,10 @@ assert_eq!((-10", stringify!($SelfT), ").signum(), -1);",
19931993
$EndFeature, "
19941994
```"),
19951995
#[stable(feature = "rust1", since = "1.0.0")]
1996+
#[rustc_const_unstable(feature = "const_int_sign")]
19961997
#[inline]
1997-
pub fn signum(self) -> Self {
1998-
match self {
1999-
n if n > 0 => 1,
2000-
0 => 0,
2001-
_ => -1,
2002-
}
1998+
pub const fn signum(self) -> Self {
1999+
(self > 0) as Self - (self < 0) as Self
20032000
}
20042001
}
20052002

0 commit comments

Comments
 (0)