Skip to content

Commit b0edfce

Browse files
committed
Implement TryFrom explicitly for infallible numeric conversions.
See #44174 (comment)
1 parent 80e3f89 commit b0edfce

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/libcore/num/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,10 +2507,12 @@ impl fmt::Display for TryFromIntError {
25072507
macro_rules! try_from_unbounded {
25082508
($source:ty, $($target:ty),*) => {$(
25092509
#[unstable(feature = "try_from", issue = "33417")]
2510-
impl From<$source> for $target {
2510+
impl TryFrom<$source> for $target {
2511+
type Error = Infallible;
2512+
25112513
#[inline]
2512-
fn from(value: $source) -> $target {
2513-
value as $target
2514+
fn try_from(value: $source) -> Result<Self, Self::Error> {
2515+
Ok(value as $target)
25142516
}
25152517
}
25162518
)*}
@@ -2617,7 +2619,7 @@ try_from_lower_bounded!(isize, usize);
26172619
#[cfg(target_pointer_width = "16")]
26182620
mod ptr_try_from_impls {
26192621
use super::TryFromIntError;
2620-
use convert::TryFrom;
2622+
use convert::{Infallible, TryFrom};
26212623

26222624
try_from_upper_bounded!(usize, u8);
26232625
try_from_unbounded!(usize, u16, u32, u64, u128);
@@ -2643,7 +2645,7 @@ mod ptr_try_from_impls {
26432645
#[cfg(target_pointer_width = "32")]
26442646
mod ptr_try_from_impls {
26452647
use super::TryFromIntError;
2646-
use convert::TryFrom;
2648+
use convert::{Infallible, TryFrom};
26472649

26482650
try_from_upper_bounded!(usize, u8, u16);
26492651
try_from_unbounded!(usize, u32, u64, u128);
@@ -2669,7 +2671,7 @@ mod ptr_try_from_impls {
26692671
#[cfg(target_pointer_width = "64")]
26702672
mod ptr_try_from_impls {
26712673
use super::TryFromIntError;
2672-
use convert::TryFrom;
2674+
use convert::{Infallible, TryFrom};
26732675

26742676
try_from_upper_bounded!(usize, u8, u16, u32);
26752677
try_from_unbounded!(usize, u64, u128);

0 commit comments

Comments
 (0)