-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add cast
function to primitive integers
#42456
Closed
Closed
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# `num_cast` | ||
|
||
The tracking issue for this feature is: [#FIXME] | ||
|
||
[#FIXME]: https://github.com/rust-lang/rust/issues/FIXME | ||
|
||
------------------------ | ||
|
8 changes: 8 additions & 0 deletions
8
src/doc/unstable-book/src/library-features/num_cast_internals.md
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# `num_cast_internals` | ||
|
||
The tracking issue for this feature is: [#FIXME] | ||
|
||
[#FIXME]: https://github.com/rust-lang/rust/issues/FIXME | ||
|
||
------------------------ | ||
|
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use fmt; | ||
|
||
/// Internal trait for APIs like `i32::cast`. | ||
#[unstable(feature = "num_cast_internals", issue = "0")] | ||
pub trait Cast<T>: Sized { | ||
/// Internal implementation detail of this trait. | ||
fn cast(t: T) -> Result<Self, CastError>; | ||
} | ||
|
||
/// Error type returned from APIs like `i32::cast`, indicates that a cast could | ||
/// not be performed losslessly. | ||
#[unstable(feature = "num_cast", issue = "0")] | ||
#[derive(Debug)] | ||
pub struct CastError(()); | ||
|
||
#[unstable(feature = "num_cast", issue = "0")] | ||
impl fmt::Display for CastError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
"failed to losslessly cast integral types".fmt(f) | ||
} | ||
} | ||
|
||
macro_rules! same_sign_cast_int_impl { | ||
($storage:ty, $target:ty, $($source:ty),*) => {$( | ||
#[unstable(feature = "num_cast", issue = "0")] | ||
impl Cast<$source> for $target { | ||
#[inline] | ||
fn cast(u: $source) -> Result<$target, CastError> { | ||
let min = <$target>::min_value() as $storage; | ||
let max = <$target>::max_value() as $storage; | ||
if u as $storage < min || u as $storage > max { | ||
Err(CastError(())) | ||
} else { | ||
Ok(u as $target) | ||
} | ||
} | ||
} | ||
)*} | ||
} | ||
|
||
same_sign_cast_int_impl!(u128, u8, u8, u16, u32, u64, u128, usize); | ||
same_sign_cast_int_impl!(i128, i8, i8, i16, i32, i64, i128, isize); | ||
same_sign_cast_int_impl!(u128, u16, u8, u16, u32, u64, u128, usize); | ||
same_sign_cast_int_impl!(i128, i16, i8, i16, i32, i64, i128, isize); | ||
same_sign_cast_int_impl!(u128, u32, u8, u16, u32, u64, u128, usize); | ||
same_sign_cast_int_impl!(i128, i32, i8, i16, i32, i64, i128, isize); | ||
same_sign_cast_int_impl!(u128, u64, u8, u16, u32, u64, u128, usize); | ||
same_sign_cast_int_impl!(i128, i64, i8, i16, i32, i64, i128, isize); | ||
same_sign_cast_int_impl!(u128, u128, u8, u16, u32, u64, u128, usize); | ||
same_sign_cast_int_impl!(i128, i128, i8, i16, i32, i64, i128, isize); | ||
same_sign_cast_int_impl!(u128, usize, u8, u16, u32, u64, u128, usize); | ||
same_sign_cast_int_impl!(i128, isize, i8, i16, i32, i64, i128, isize); | ||
|
||
macro_rules! cross_sign_cast_int_impl { | ||
($unsigned:ty, $($signed:ty),*) => {$( | ||
#[unstable(feature = "num_cast", issue = "0")] | ||
impl Cast<$unsigned> for $signed { | ||
#[inline] | ||
fn cast(u: $unsigned) -> Result<$signed, CastError> { | ||
let max = <$signed>::max_value() as u128; | ||
if u as u128 > max { | ||
Err(CastError(())) | ||
} else { | ||
Ok(u as $signed) | ||
} | ||
} | ||
} | ||
|
||
#[unstable(feature = "num_cast", issue = "0")] | ||
impl Cast<$signed> for $unsigned { | ||
#[inline] | ||
fn cast(u: $signed) -> Result<$unsigned, CastError> { | ||
let max = <$unsigned>::max_value() as u128; | ||
if u < 0 || u as u128 > max { | ||
Err(CastError(())) | ||
} else { | ||
Ok(u as $unsigned) | ||
} | ||
} | ||
} | ||
)*} | ||
} | ||
|
||
cross_sign_cast_int_impl!(u8, i8, i16, i32, i64, i128, isize); | ||
cross_sign_cast_int_impl!(u16, i8, i16, i32, i64, i128, isize); | ||
cross_sign_cast_int_impl!(u32, i8, i16, i32, i64, i128, isize); | ||
cross_sign_cast_int_impl!(u64, i8, i16, i32, i64, i128, isize); | ||
cross_sign_cast_int_impl!(u128, i8, i16, i32, i64, i128, isize); | ||
cross_sign_cast_int_impl!(usize, i8, i16, i32, i64, i128, isize); |
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
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.
Is this separate error really needed? Why not an option based API? The ? operator is about to work with options as well, no?
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.
The intention is that you can define
From<CastError> for MyError
and have?
work in result-based functions, whereas in option base dfunctions you can just usecast(..).ok()?
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.
IDK, for me the operation feels similar to
checked_add
and that returns an option as well. Or is the policy to returnResult
everywhere for new APIs?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.
It's pretty difficult to create a blanket policy that works everywhere, but this is intended for use in APIs that are already dealing with calls to libc and such and are almost guaranteed to already be returning
Result
.