Skip to content

Commit c0f86a9

Browse files
committed
Re-add min_value, max_value methods
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.
1 parent d528aa9 commit c0f86a9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/libcore/num/mod.rs

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

0 commit comments

Comments
 (0)