Skip to content

Commit d8e96a3

Browse files
committed
Implement conditionally supported conversions from and to usize/isize
1 parent 0743694 commit d8e96a3

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/libcore/num/mod.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2778,7 +2778,6 @@ pub use num::dec2flt::ParseFloatError;
27782778

27792779
// Conversion traits for primitive integer and float types
27802780
// Conversions T -> T are covered by a blanket impl and therefore excluded
2781-
// Some conversions from and to usize/isize are not implemented due to portability concerns
27822781
macro_rules! impl_from {
27832782
($Small: ty, $Large: ty) => {
27842783
#[stable(feature = "lossless_prim_conv", since = "1.5.0")]
@@ -2791,6 +2790,14 @@ macro_rules! impl_from {
27912790
}
27922791
}
27932792

2793+
// Conditionally supported conversions from and to usize/isize
2794+
macro_rules! impl_from_cond {
2795+
($Small: ty, $Large: ty | $($width: expr),*) => {
2796+
#[cfg(any($(target_pointer_width = $width,)*))]
2797+
impl_from! { $Small, $Large }
2798+
}
2799+
}
2800+
27942801
// Unsigned -> Unsigned
27952802
impl_from! { u8, u16 }
27962803
impl_from! { u8, u32 }
@@ -2799,6 +2806,12 @@ impl_from! { u8, usize }
27992806
impl_from! { u16, u32 }
28002807
impl_from! { u16, u64 }
28012808
impl_from! { u32, u64 }
2809+
impl_from_cond! { u16, usize | "16", "32", "64" }
2810+
impl_from_cond! { u32, usize | "32", "64" }
2811+
impl_from_cond! { u64, usize | "64" }
2812+
impl_from_cond! { usize, u16 | "16" }
2813+
impl_from_cond! { usize, u32 | "16", "32" }
2814+
impl_from_cond! { usize, u64 | "16", "32", "64" }
28022815

28032816
// Signed -> Signed
28042817
impl_from! { i8, i16 }
@@ -2808,6 +2821,12 @@ impl_from! { i8, isize }
28082821
impl_from! { i16, i32 }
28092822
impl_from! { i16, i64 }
28102823
impl_from! { i32, i64 }
2824+
impl_from_cond! { i16, isize | "16", "32", "64" }
2825+
impl_from_cond! { i32, isize | "32", "64" }
2826+
impl_from_cond! { i64, isize | "64" }
2827+
impl_from_cond! { isize, i16 | "16" }
2828+
impl_from_cond! { isize, i32 | "16", "32" }
2829+
impl_from_cond! { isize, i64 | "16", "32", "64" }
28112830

28122831
// Unsigned -> Signed
28132832
impl_from! { u8, i16 }
@@ -2816,6 +2835,11 @@ impl_from! { u8, i64 }
28162835
impl_from! { u16, i32 }
28172836
impl_from! { u16, i64 }
28182837
impl_from! { u32, i64 }
2838+
impl_from_cond! { u8, isize | "16", "32", "64" }
2839+
impl_from_cond! { u16, isize | "32", "64" }
2840+
impl_from_cond! { u32, isize | "64" }
2841+
impl_from_cond! { usize, i32 | "16" }
2842+
impl_from_cond! { usize, i64 | "16", "32" }
28192843

28202844
// Note: integers can only be represented with full precision in a float if
28212845
// they fit in the significand, which is 24 bits in f32 and 53 bits in f64.

src/libcoretest/num/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ macro_rules! test_impl_from {
128128
}
129129
}
130130

131+
macro_rules! test_impl_from_cond {
132+
($fn_name: ident, $Small: ty, $Large: ty | $($width: expr),*) => {
133+
#[cfg(any($(target_pointer_width = $width,)*))]
134+
test_impl_from! { $fn_name, $Small, $Large }
135+
}
136+
}
137+
131138
// Unsigned -> Unsigned
132139
test_impl_from! { test_u8u16, u8, u16 }
133140
test_impl_from! { test_u8u32, u8, u32 }
@@ -136,6 +143,12 @@ test_impl_from! { test_u8usize, u8, usize }
136143
test_impl_from! { test_u16u32, u16, u32 }
137144
test_impl_from! { test_u16u64, u16, u64 }
138145
test_impl_from! { test_u32u64, u32, u64 }
146+
test_impl_from_cond! { test_u16usize, u16, usize | "16", "32", "64" }
147+
test_impl_from_cond! { test_u32usize, u32, usize | "32", "64" }
148+
test_impl_from_cond! { test_u64usize, u64, usize | "64" }
149+
test_impl_from_cond! { test_usizeu16, usize, u16 | "16" }
150+
test_impl_from_cond! { test_usizeu32, usize, u32 | "16", "32" }
151+
test_impl_from_cond! { test_usizeu64, usize, u64 | "16", "32", "64" }
139152

140153
// Signed -> Signed
141154
test_impl_from! { test_i8i16, i8, i16 }
@@ -145,6 +158,12 @@ test_impl_from! { test_i8isize, i8, isize }
145158
test_impl_from! { test_i16i32, i16, i32 }
146159
test_impl_from! { test_i16i64, i16, i64 }
147160
test_impl_from! { test_i32i64, i32, i64 }
161+
test_impl_from_cond! { test_i16isize, i16, isize | "16", "32", "64" }
162+
test_impl_from_cond! { test_i32isize, i32, isize | "32", "64" }
163+
test_impl_from_cond! { test_i64isize, i64, isize | "64" }
164+
test_impl_from_cond! { test_isizei16, isize, i16 | "16" }
165+
test_impl_from_cond! { test_isizei32, isize, i32 | "16", "32" }
166+
test_impl_from_cond! { test_isizei64, isize, i64 | "16", "32", "64" }
148167

149168
// Unsigned -> Signed
150169
test_impl_from! { test_u8i16, u8, i16 }
@@ -153,6 +172,11 @@ test_impl_from! { test_u8i64, u8, i64 }
153172
test_impl_from! { test_u16i32, u16, i32 }
154173
test_impl_from! { test_u16i64, u16, i64 }
155174
test_impl_from! { test_u32i64, u32, i64 }
175+
test_impl_from_cond! { test_u8isize, u8, isize | "16", "32", "64" }
176+
test_impl_from_cond! { test_u16isize, u16, isize | "32", "64" }
177+
test_impl_from_cond! { test_u32isize, u32, isize | "64" }
178+
test_impl_from_cond! { test_isizeu32, usize, i32 | "16" }
179+
test_impl_from_cond! { test_isizeu64, usize, i64 | "16", "32" }
156180

157181
// Signed -> Float
158182
test_impl_from! { test_i8f32, i8, f32 }

0 commit comments

Comments
 (0)