Skip to content

Commit fb4029f

Browse files
committed
rollup merge of #23947: aturon/revise-num
Recent numerics stabilization removed the inherent `min_value` and `max_value` methods from integer types, assuming that the module-level constants would suffice. However, that failed to account for the use case in FFI code when dealing with integer type aliases. This commit reintroduces the methods as `#[stable]`, since this is essential functionality for 1.0. It's unfortunate to freeze these as methods, but when we can provide inherent associated constants these methods can be deprecated. r? @sfackler cc @alexcrichton
2 parents d55ffa9 + c0f86a9 commit fb4029f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/libcore/num/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,18 @@ macro_rules! int_impl {
819819
$add_with_overflow:path,
820820
$sub_with_overflow:path,
821821
$mul_with_overflow:path) => {
822+
/// Returns the smallest value that can be represented by this integer type.
823+
#[stable(feature = "rust1", since = "1.0.0")]
824+
pub fn min_value() -> $T {
825+
(-1 as $T) << ($BITS - 1)
826+
}
827+
828+
/// Returns the largest value that can be represented by this integer type.
829+
#[stable(feature = "rust1", since = "1.0.0")]
830+
pub fn max_value() -> $T {
831+
let min: $T = Int::min_value(); !min
832+
}
833+
822834
/// Convert a string slice in a given base to an integer.
823835
///
824836
/// Leading and trailing whitespace represent an error.
@@ -1329,6 +1341,14 @@ macro_rules! uint_impl {
13291341
$add_with_overflow:path,
13301342
$sub_with_overflow:path,
13311343
$mul_with_overflow:path) => {
1344+
/// Returns the smallest value that can be represented by this integer type.
1345+
#[stable(feature = "rust1", since = "1.0.0")]
1346+
pub fn min_value() -> $T { 0 }
1347+
1348+
/// Returns the largest value that can be represented by this integer type.
1349+
#[stable(feature = "rust1", since = "1.0.0")]
1350+
pub fn max_value() -> $T { -1 }
1351+
13321352
/// Convert a string slice in a given base to an integer.
13331353
///
13341354
/// Leading and trailing whitespace represent an error.

0 commit comments

Comments
 (0)