Skip to content

Commit b5ff806

Browse files
committed
Add MIN/MAX associated constants to the integer types
1 parent e0bbe79 commit b5ff806

File tree

1 file changed

+47
-23
lines changed

1 file changed

+47
-23
lines changed

src/libcore/num/mod.rs

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -245,42 +245,54 @@ macro_rules! int_impl {
245245
$reversed:expr, $le_bytes:expr, $be_bytes:expr,
246246
$to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr) => {
247247
doc_comment! {
248-
concat!("Returns the smallest value that can be represented by this integer type.
248+
concat!("The smallest value that can be represented by this integer type.
249249
250250
# Examples
251251
252252
Basic usage:
253253
254254
```
255-
", $Feature, "assert_eq!(", stringify!($SelfT), "::min_value(), ", stringify!($Min), ");",
255+
", $Feature, "assert_eq!(", stringify!($SelfT), "::MIN, ", stringify!($Min), ");",
256256
$EndFeature, "
257257
```"),
258-
#[stable(feature = "rust1", since = "1.0.0")]
259-
#[inline(always)]
260-
#[rustc_promotable]
261-
#[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
262-
pub const fn min_value() -> Self {
263-
!0 ^ ((!0 as $UnsignedT) >> 1) as Self
264-
}
258+
#[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")]
259+
pub const MIN: Self = !0 ^ ((!0 as $UnsignedT) >> 1) as Self;
265260
}
266261

267262
doc_comment! {
268-
concat!("Returns the largest value that can be represented by this integer type.
263+
concat!("The largest value that can be represented by this integer type.
269264
270265
# Examples
271266
272267
Basic usage:
273268
274269
```
275-
", $Feature, "assert_eq!(", stringify!($SelfT), "::max_value(), ", stringify!($Max), ");",
270+
", $Feature, "assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($Max), ");",
276271
$EndFeature, "
277272
```"),
273+
#[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")]
274+
pub const MAX: Self = !Self::MIN;
275+
}
276+
277+
doc_comment! {
278+
"Returns the smallest value that can be represented by this integer type.",
279+
#[stable(feature = "rust1", since = "1.0.0")]
280+
#[inline(always)]
281+
#[rustc_promotable]
282+
#[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
283+
pub const fn min_value() -> Self {
284+
Self::MIN
285+
}
286+
}
287+
288+
doc_comment! {
289+
"Returns the largest value that can be represented by this integer type.",
278290
#[stable(feature = "rust1", since = "1.0.0")]
279291
#[inline(always)]
280292
#[rustc_promotable]
281293
#[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
282294
pub const fn max_value() -> Self {
283-
!Self::min_value()
295+
Self::MAX
284296
}
285297
}
286298

@@ -2342,38 +2354,50 @@ macro_rules! uint_impl {
23422354
$reversed:expr, $le_bytes:expr, $be_bytes:expr,
23432355
$to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr) => {
23442356
doc_comment! {
2345-
concat!("Returns the smallest value that can be represented by this integer type.
2357+
concat!("The smallest value that can be represented by this integer type.
23462358
23472359
# Examples
23482360
23492361
Basic usage:
23502362
23512363
```
2352-
", $Feature, "assert_eq!(", stringify!($SelfT), "::min_value(), 0);", $EndFeature, "
2364+
", $Feature, "assert_eq!(", stringify!($SelfT), "::MIN, 0);", $EndFeature, "
23532365
```"),
2354-
#[stable(feature = "rust1", since = "1.0.0")]
2355-
#[rustc_promotable]
2356-
#[inline(always)]
2357-
#[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
2358-
pub const fn min_value() -> Self { 0 }
2366+
#[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")]
2367+
pub const MIN: Self = 0;
23592368
}
23602369

23612370
doc_comment! {
2362-
concat!("Returns the largest value that can be represented by this integer type.
2371+
concat!("The largest value that can be represented by this integer type.
23632372
23642373
# Examples
23652374
23662375
Basic usage:
23672376
23682377
```
2369-
", $Feature, "assert_eq!(", stringify!($SelfT), "::max_value(), ",
2370-
stringify!($MaxV), ");", $EndFeature, "
2378+
", $Feature, "assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($MaxV), ");",
2379+
$EndFeature, "
23712380
```"),
2381+
#[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")]
2382+
pub const MAX: Self = !0;
2383+
}
2384+
2385+
doc_comment! {
2386+
"Returns the smallest value that can be represented by this integer type.",
2387+
#[stable(feature = "rust1", since = "1.0.0")]
2388+
#[rustc_promotable]
2389+
#[inline(always)]
2390+
#[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
2391+
pub const fn min_value() -> Self { Self::MIN }
2392+
}
2393+
2394+
doc_comment! {
2395+
"Returns the largest value that can be represented by this integer type.",
23722396
#[stable(feature = "rust1", since = "1.0.0")]
23732397
#[rustc_promotable]
23742398
#[inline(always)]
23752399
#[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
2376-
pub const fn max_value() -> Self { !0 }
2400+
pub const fn max_value() -> Self { Self::MAX }
23772401
}
23782402

23792403
doc_comment! {

0 commit comments

Comments
 (0)