Skip to content

Commit 3b41447

Browse files
authored
Rollup merge of #86840 - usbalbin:const_from, r=oli-obk
Constify implementations of `(Try)From` for int types I believe this to be one of the (many?) things blocking const (Range) iterators. ~~If this is to be merged maybe that should wait until `#![feature(const_trait_impl)]` no longer needs `#![allow(incomplete_features)]`?~~ - Done
2 parents ae90dcf + c8bf5ed commit 3b41447

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

library/core/src/convert/num.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ impl_float_to_int!(f64 => u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);
4545
macro_rules! impl_from {
4646
($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
4747
#[$attr]
48-
impl From<$Small> for $Large {
48+
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
49+
impl const From<$Small> for $Large {
4950
// Rustdocs on the impl block show a "[+] show undocumented items" toggle.
5051
// Rustdocs on functions do not.
5152
#[doc = $doc]
@@ -172,7 +173,8 @@ impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0"
172173
macro_rules! try_from_unbounded {
173174
($source:ty, $($target:ty),*) => {$(
174175
#[stable(feature = "try_from", since = "1.34.0")]
175-
impl TryFrom<$source> for $target {
176+
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
177+
impl const TryFrom<$source> for $target {
176178
type Error = TryFromIntError;
177179

178180
/// Try to create the target number type from a source
@@ -190,7 +192,8 @@ macro_rules! try_from_unbounded {
190192
macro_rules! try_from_lower_bounded {
191193
($source:ty, $($target:ty),*) => {$(
192194
#[stable(feature = "try_from", since = "1.34.0")]
193-
impl TryFrom<$source> for $target {
195+
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
196+
impl const TryFrom<$source> for $target {
194197
type Error = TryFromIntError;
195198

196199
/// Try to create the target number type from a source
@@ -212,7 +215,8 @@ macro_rules! try_from_lower_bounded {
212215
macro_rules! try_from_upper_bounded {
213216
($source:ty, $($target:ty),*) => {$(
214217
#[stable(feature = "try_from", since = "1.34.0")]
215-
impl TryFrom<$source> for $target {
218+
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
219+
impl const TryFrom<$source> for $target {
216220
type Error = TryFromIntError;
217221

218222
/// Try to create the target number type from a source
@@ -234,7 +238,8 @@ macro_rules! try_from_upper_bounded {
234238
macro_rules! try_from_both_bounded {
235239
($source:ty, $($target:ty),*) => {$(
236240
#[stable(feature = "try_from", since = "1.34.0")]
237-
impl TryFrom<$source> for $target {
241+
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
242+
impl const TryFrom<$source> for $target {
238243
type Error = TryFromIntError;
239244

240245
/// Try to create the target number type from a source

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
#![feature(const_slice_from_raw_parts)]
100100
#![feature(const_slice_ptr_len)]
101101
#![feature(const_swap)]
102+
#![feature(const_trait_impl)]
102103
#![feature(const_type_id)]
103104
#![feature(const_type_name)]
104105
#![feature(const_unreachable_unchecked)]

library/core/tests/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#![feature(const_ptr_read)]
1414
#![feature(const_ptr_write)]
1515
#![feature(const_ptr_offset)]
16+
#![feature(const_trait_impl)]
17+
#![feature(const_num_from_num)]
1618
#![feature(core_intrinsics)]
1719
#![feature(core_private_bignum)]
1820
#![feature(core_private_diy_float)]

library/core/tests/num/const_from.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#[test]
2+
fn from() {
3+
use core::convert::TryFrom;
4+
use core::num::TryFromIntError;
5+
6+
// From
7+
const FROM: i64 = i64::from(1i32);
8+
assert_eq!(FROM, 1i64);
9+
10+
// From int to float
11+
const FROM_F64: f64 = f64::from(42u8);
12+
assert_eq!(FROM_F64, 42f64);
13+
14+
// Upper bounded
15+
const U8_FROM_U16: Result<u8, TryFromIntError> = u8::try_from(1u16);
16+
assert_eq!(U8_FROM_U16, Ok(1u8));
17+
18+
// Both bounded
19+
const I8_FROM_I16: Result<i8, TryFromIntError> = i8::try_from(1i16);
20+
assert_eq!(I8_FROM_I16, Ok(1i8));
21+
22+
// Lower bounded
23+
const I16_FROM_U16: Result<i16, TryFromIntError> = i16::try_from(1u16);
24+
assert_eq!(I16_FROM_U16, Ok(1i16));
25+
}

library/core/tests/num/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ mod u64;
2727
mod u8;
2828

2929
mod bignum;
30+
31+
mod const_from;
3032
mod dec2flt;
3133
mod flt2dec;
3234
mod int_log;

0 commit comments

Comments
 (0)