Skip to content

Commit 2954cb5

Browse files
committed
Auto merge of #50554 - clarcharr:from_bool, r=TimNN
Add From<bool> for int types Fixes #46109.
2 parents edae1cc + b1797d5 commit 2954cb5

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

src/libcore/num/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -4454,6 +4454,20 @@ macro_rules! impl_from {
44544454
}
44554455
}
44564456

4457+
// Bool -> Any
4458+
impl_from! { bool, u8, #[stable(feature = "from_bool", since = "1.28.0")] }
4459+
impl_from! { bool, u16, #[stable(feature = "from_bool", since = "1.28.0")] }
4460+
impl_from! { bool, u32, #[stable(feature = "from_bool", since = "1.28.0")] }
4461+
impl_from! { bool, u64, #[stable(feature = "from_bool", since = "1.28.0")] }
4462+
impl_from! { bool, u128, #[stable(feature = "from_bool", since = "1.28.0")] }
4463+
impl_from! { bool, usize, #[stable(feature = "from_bool", since = "1.28.0")] }
4464+
impl_from! { bool, i8, #[stable(feature = "from_bool", since = "1.28.0")] }
4465+
impl_from! { bool, i16, #[stable(feature = "from_bool", since = "1.28.0")] }
4466+
impl_from! { bool, i32, #[stable(feature = "from_bool", since = "1.28.0")] }
4467+
impl_from! { bool, i64, #[stable(feature = "from_bool", since = "1.28.0")] }
4468+
impl_from! { bool, i128, #[stable(feature = "from_bool", since = "1.28.0")] }
4469+
impl_from! { bool, isize, #[stable(feature = "from_bool", since = "1.28.0")] }
4470+
44574471
// Unsigned -> Unsigned
44584472
impl_from! { u8, u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
44594473
impl_from! { u8, u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }

src/libcore/tests/num/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ fn test_infallible_try_from_int_error() {
134134
}
135135

136136
macro_rules! test_impl_from {
137+
($fn_name:ident, bool, $target: ty) => {
138+
#[test]
139+
fn $fn_name() {
140+
let one: $target = 1;
141+
let zero: $target = 0;
142+
assert_eq!(one, <$target>::from(true));
143+
assert_eq!(zero, <$target>::from(false));
144+
}
145+
};
137146
($fn_name: ident, $Small: ty, $Large: ty) => {
138147
#[test]
139148
fn $fn_name() {
@@ -173,6 +182,18 @@ test_impl_from! { test_u16i32, u16, i32 }
173182
test_impl_from! { test_u16i64, u16, i64 }
174183
test_impl_from! { test_u32i64, u32, i64 }
175184

185+
// Bool -> Integer
186+
test_impl_from! { test_boolu8, bool, u8 }
187+
test_impl_from! { test_boolu16, bool, u16 }
188+
test_impl_from! { test_boolu32, bool, u32 }
189+
test_impl_from! { test_boolu64, bool, u64 }
190+
test_impl_from! { test_boolu128, bool, u128 }
191+
test_impl_from! { test_booli8, bool, i8 }
192+
test_impl_from! { test_booli16, bool, i16 }
193+
test_impl_from! { test_booli32, bool, i32 }
194+
test_impl_from! { test_booli64, bool, i64 }
195+
test_impl_from! { test_booli128, bool, i128 }
196+
176197
// Signed -> Float
177198
test_impl_from! { test_i8f32, i8, f32 }
178199
test_impl_from! { test_i8f64, i8, f64 }

src/libcore/tests/result.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,15 @@ fn test_try() {
220220
assert_eq!(try_result_none(), None);
221221

222222
fn try_result_ok() -> Result<u8, u8> {
223-
let val = Ok(1)?;
223+
let result: Result<u8, u8> = Ok(1);
224+
let val = result?;
224225
Ok(val)
225226
}
226227
assert_eq!(try_result_ok(), Ok(1));
227228

228229
fn try_result_err() -> Result<u8, u8> {
229-
let val = Err(1)?;
230+
let result: Result<u8, u8> = Err(1);
231+
let val = result?;
230232
Ok(val)
231233
}
232234
assert_eq!(try_result_err(), Err(1));

0 commit comments

Comments
 (0)