-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement RFC 1542 #33426
Merged
Merged
Implement RFC 1542 #33426
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
|
||
use char::CharExt; | ||
use cmp::PartialOrd; | ||
use convert::From; | ||
use convert::{From, TryFrom}; | ||
use fmt; | ||
use intrinsics; | ||
use marker::{Copy, Sized}; | ||
|
@@ -2341,9 +2341,101 @@ macro_rules! from_str_radix_int_impl { | |
} | ||
from_str_radix_int_impl! { isize i8 i16 i32 i64 usize u8 u16 u32 u64 } | ||
|
||
/// The error type returned when a checked integral type conversion fails. | ||
#[unstable(feature = "try_from", issue = "33417")] | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct TryFromIntError(()); | ||
|
||
impl TryFromIntError { | ||
#[unstable(feature = "int_error_internals", | ||
reason = "available through Error trait and this method should \ | ||
not be exposed publicly", | ||
issue = "0")] | ||
#[doc(hidden)] | ||
pub fn __description(&self) -> &str { | ||
"out of range integral type conversion attempted" | ||
} | ||
} | ||
|
||
#[unstable(feature = "try_from", issue = "33417")] | ||
impl fmt::Display for TryFromIntError { | ||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
self.__description().fmt(fmt) | ||
} | ||
} | ||
|
||
macro_rules! same_sign_from_int_impl { | ||
($storage:ty, $target:ty, $($source:ty),*) => {$( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This macro syntax could probably be more clear (given the open-ended arg list, it's hard to see what's going on from a glance at the use-site). But NBD. |
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl TryFrom<$source> for $target { | ||
type Err = TryFromIntError; | ||
|
||
fn try_from(u: $source) -> Result<$target, TryFromIntError> { | ||
let min = <$target as FromStrRadixHelper>::min_value() as $storage; | ||
let max = <$target as FromStrRadixHelper>::max_value() as $storage; | ||
if u as $storage < min || u as $storage > max { | ||
Err(TryFromIntError(())) | ||
} else { | ||
Ok(u as $target) | ||
} | ||
} | ||
} | ||
)*} | ||
} | ||
|
||
same_sign_from_int_impl!(u64, u8, u8, u16, u32, u64, usize); | ||
same_sign_from_int_impl!(i64, i8, i8, i16, i32, i64, isize); | ||
same_sign_from_int_impl!(u64, u16, u8, u16, u32, u64, usize); | ||
same_sign_from_int_impl!(i64, i16, i8, i16, i32, i64, isize); | ||
same_sign_from_int_impl!(u64, u32, u8, u16, u32, u64, usize); | ||
same_sign_from_int_impl!(i64, i32, i8, i16, i32, i64, isize); | ||
same_sign_from_int_impl!(u64, u64, u8, u16, u32, u64, usize); | ||
same_sign_from_int_impl!(i64, i64, i8, i16, i32, i64, isize); | ||
same_sign_from_int_impl!(u64, usize, u8, u16, u32, u64, usize); | ||
same_sign_from_int_impl!(i64, isize, i8, i16, i32, i64, isize); | ||
|
||
macro_rules! cross_sign_from_int_impl { | ||
($unsigned:ty, $($signed:ty),*) => {$( | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl TryFrom<$unsigned> for $signed { | ||
type Err = TryFromIntError; | ||
|
||
fn try_from(u: $unsigned) -> Result<$signed, TryFromIntError> { | ||
let max = <$signed as FromStrRadixHelper>::max_value() as u64; | ||
if u as u64 > max { | ||
Err(TryFromIntError(())) | ||
} else { | ||
Ok(u as $signed) | ||
} | ||
} | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl TryFrom<$signed> for $unsigned { | ||
type Err = TryFromIntError; | ||
|
||
fn try_from(u: $signed) -> Result<$unsigned, TryFromIntError> { | ||
let max = <$unsigned as FromStrRadixHelper>::max_value() as u64; | ||
if u < 0 || u as u64 > max { | ||
Err(TryFromIntError(())) | ||
} else { | ||
Ok(u as $unsigned) | ||
} | ||
} | ||
} | ||
)*} | ||
} | ||
|
||
cross_sign_from_int_impl!(u8, i8, i16, i32, i64, isize); | ||
cross_sign_from_int_impl!(u16, i8, i16, i32, i64, isize); | ||
cross_sign_from_int_impl!(u32, i8, i16, i32, i64, isize); | ||
cross_sign_from_int_impl!(u64, i8, i16, i32, i64, isize); | ||
cross_sign_from_int_impl!(usize, i8, i16, i32, i64, isize); | ||
|
||
#[doc(hidden)] | ||
trait FromStrRadixHelper: PartialOrd + Copy { | ||
fn min_value() -> Self; | ||
fn max_value() -> Self; | ||
fn from_u32(u: u32) -> Self; | ||
fn checked_mul(&self, other: u32) -> Option<Self>; | ||
fn checked_sub(&self, other: u32) -> Option<Self>; | ||
|
@@ -2353,6 +2445,7 @@ trait FromStrRadixHelper: PartialOrd + Copy { | |
macro_rules! doit { | ||
($($t:ty)*) => ($(impl FromStrRadixHelper for $t { | ||
fn min_value() -> Self { Self::min_value() } | ||
fn max_value() -> Self { Self::max_value() } | ||
fn from_u32(u: u32) -> Self { u as Self } | ||
fn checked_mul(&self, other: u32) -> Option<Self> { | ||
Self::checked_mul(*self, other as Self) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yuck! But I guess such is life for private sharing across the facade...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this is what the existing
ParseIntError
did :(