Skip to content

Commit 93a56cd

Browse files
committed
impl From<Infallible> for TryFromIntError.
1 parent 36c0ff8 commit 93a56cd

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/libcore/num/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
#![stable(feature = "rust1", since = "1.0.0")]
1414

15-
use convert::TryFrom;
15+
use convert::{Infallible, TryFrom};
1616
use fmt;
1717
use intrinsics;
1818
use str::FromStr;
@@ -2503,6 +2503,13 @@ impl fmt::Display for TryFromIntError {
25032503
}
25042504
}
25052505

2506+
#[unstable(feature = "try_from", issue = "33417")]
2507+
impl From<Infallible> for TryFromIntError {
2508+
fn from(_: Infallible) -> TryFromIntError {
2509+
TryFromIntError(())
2510+
}
2511+
}
2512+
25062513
// no possible bounds violation
25072514
macro_rules! try_from_unbounded {
25082515
($source:ty, $($target:ty),*) => {$(

src/libcore/tests/num/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use core::convert::TryFrom;
11+
use core::convert::{TryFrom, TryInto};
1212
use core::cmp::PartialEq;
1313
use core::fmt::Debug;
1414
use core::marker::Copy;
15+
use core::num::TryFromIntError;
1516
use core::ops::{Add, Sub, Mul, Div, Rem};
1617
use core::option::Option;
1718
use core::option::Option::{Some, None};
@@ -134,6 +135,13 @@ fn test_empty() {
134135
assert_eq!("".parse::<u8>().ok(), None);
135136
}
136137

138+
#[test]
139+
fn test_infallible_try_from_int_error() {
140+
let func = |x: i8| -> Result<i32, TryFromIntError> { Ok(x.try_into()?) };
141+
142+
assert!(func(0).is_ok());
143+
}
144+
137145
macro_rules! test_impl_from {
138146
($fn_name: ident, $Small: ty, $Large: ty) => {
139147
#[test]

0 commit comments

Comments
 (0)