Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd TryFrom<{integer}> for bool #50597
Conversation
rust-highfive
assigned
shepmaster
May 10, 2018
This comment has been minimized.
This comment has been minimized.
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @shepmaster (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
rust-highfive
added
the
S-waiting-on-review
label
May 10, 2018
scottmcm
reviewed
May 10, 2018
| impl_from! { i32, bool, #[unstable(feature = "into_bool", issue = "0")]} | ||
| impl_from! { i64, bool, #[unstable(feature = "into_bool", issue = "0")]} | ||
| impl_from! { i128, bool, #[unstable(feature = "into_bool", issue = "0")]} | ||
| // Boolean -> Integer |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
scottmcm
reviewed
May 10, 2018
| @@ -4580,6 +4602,29 @@ impl_from! { u32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0" | |||
| // Float -> Float | |||
| impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] } | |||
|
|
|||
| // Integer -> Boolean | |||
This comment has been minimized.
This comment has been minimized.
scottmcm
May 10, 2018
Member
This is lossy, so feels questionable whether it should be there in From.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
This fixes : #46109. |
ithinuel
changed the title
Add from and into bool for integer types
Add From<bool> for integers and TryFrom<integers> for bool
May 10, 2018
ithinuel
reviewed
May 11, 2018
| @@ -4259,6 +4259,25 @@ macro_rules! try_from_both_bounded { | |||
| )*} | |||
| } | |||
|
|
|||
| macro_rules! try_bool_from { | |||
| ($($source:ty),*) => {$( | |||
| #[unstable(feature = "try_bool_from", issue = "0")] | |||
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
scottmcm
May 12, 2018
Member
impls are insta-stable, so they should be marked stable with the corresponding release (currently 1.28, I think).
This comment has been minimized.
This comment has been minimized.
ithinuel
May 12, 2018
Author
Ok, Thank you.
So I changed :
- the
impl From<bool> for ..to stable 1.28.0 with the feature namelossless_bool_convto be consistent with other lossless conversions. impl TryFrom<T> for boolto the unstable featuretry_fromlinking #33417 as the issue number.
This comment has been minimized.
This comment has been minimized.
|
A practical use case of this can be found here : https://github.com/ithinuel/rusty-printer cc @TimNN @clarcharr |
ithinuel
referenced this pull request
May 14, 2018
Closed
From<bool> for integers not implemented? #46109
This comment has been minimized.
This comment has been minimized.
|
I don't think that |
This comment has been minimized.
This comment has been minimized.
I am not sure to understand your point. At least in C, C++, JS and python
This demonstrates The implementation proposed here prevents loss by only accepting |
This comment has been minimized.
This comment has been minimized.
|
Err, you're right, I swapped true and false there. |
This comment has been minimized.
This comment has been minimized.
|
@scottmcm @clarcharr @shepmaster |
This comment has been minimized.
This comment has been minimized.
|
@ithinuel I'm torn. On the one hand I think Well, you could remove the |
This comment has been minimized.
This comment has been minimized.
|
Could you elaborate why you find it weird ? here or on internals. I could indeed remove the From part. I implemented it that way because I wasn't sure true is always 1 and false 0 but it is actually stated in the documentation : https://doc.rust-lang.org/std/primitive.bool.html EDIT: I just noticed that the other PR is lacking tests. |
clarfon
added a commit
to clarfon/rust
that referenced
this pull request
May 20, 2018
This comment has been minimized.
This comment has been minimized.
|
@ithinuel Because there's two interpretations. The "bool is i1" interpretation says it should only accept |
This comment has been minimized.
This comment has been minimized.
|
@scottmcm We could argue something equivalent to the conversion from u32 to u8 where the truncation is also well known in a lot of languages. TBH I don't really mind either way, what really matters is to have |
This comment has been minimized.
This comment has been minimized.
|
TBQH I think that |
This comment has been minimized.
This comment has been minimized.
rust-highfive
assigned
TimNN
and unassigned
shepmaster
May 21, 2018
This comment has been minimized.
This comment has been minimized.
|
Ping from triage @TimNN! This PR needs your review. |
This comment has been minimized.
This comment has been minimized.
|
We have #50554 for the The largest concern I see in the discussion above is that different people could have different expectations for the
Note that (1) and (3) could be implemented as The main benefit of this PR is for generic & macro code, as I understand it. To me it seems like the core question is whether Since this will need an FCP anyway, due to insta-stable implementations, I believe the best way forward is to delegate this to @rust-lang/libs, either to discuss how this should be implemented (1) - (3) or to propose a |
TimNN
added
T-libs
S-waiting-on-team
and removed
S-waiting-on-review
labels
May 28, 2018
This comment has been minimized.
This comment has been minimized.
I think this is a reason to not have these conversions at all. There is precedent with |
This comment has been minimized.
This comment has been minimized.
|
@SimonSapin I don't think |
This comment has been minimized.
This comment has been minimized.
|
This was my first attempt but unfortunately it is not possible as
That's why the solution is to actually implement it in the upstream crate |
clarfon
added a commit
to clarfon/rust
that referenced
this pull request
Jun 1, 2018
This comment has been minimized.
This comment has been minimized.
|
|
ithinuel
force-pushed the
ithinuel:add-from-and-into-bool-for-integer-types
branch
from
3f20ecd
to
25a622a
Jun 2, 2018
This comment has been minimized.
This comment has been minimized.
|
Squashed for a clean history & rebased :) |
ithinuel
changed the title
Add From<bool> for integers and TryFrom<integers> for bool
Add TryFrom<{integer}> for bool
Jun 2, 2018
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@rust-lang/libs any update on this PR ? |
This comment has been minimized.
This comment has been minimized.
|
I don't believe our feelings on these impls have changed. If you want specific behavior for your application, you can make a private trait that does that. It just can't have a blanket impl. |
This comment has been minimized.
This comment has been minimized.
|
@sfackler Can you give an example of what you mean ? Using a trait to extend the implementation does not solve the issue as demonstrated here. This example show a working example on my specific application. |
This comment has been minimized.
This comment has been minimized.
|
@ithinuel: So, if you're main use case is macros, I've just thought about another solution: Add your own |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This is what I mean. |
This comment has been minimized.
This comment has been minimized.
This means code duplication or redundant boiler plate. |
This comment has been minimized.
This comment has been minimized.
|
@rust-lang/libs, it looks like the consensus here is to close the PR. Could one of you please do so if that is correct or initiate an |
F001
added a commit
to F001/rust
that referenced
this pull request
Jun 24, 2018
This comment has been minimized.
This comment has been minimized.
I think this is a strong reason. @rfcbot fcp close |
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Jun 27, 2018
•
|
Team member @SimonSapin has proposed to close this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
rfcbot
added
proposed-final-comment-period
disposition-close
labels
Jun 27, 2018
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Jun 27, 2018
|
|
rfcbot
added
final-comment-period
and removed
proposed-final-comment-period
labels
Jun 27, 2018
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Jul 7, 2018
|
The final comment period, with a disposition to close, as per the review above, is now complete. |
rfcbot
added
finished-final-comment-period
and removed
final-comment-period
labels
Jul 7, 2018
This comment has been minimized.
This comment has been minimized.
|
As discussed we’re not taking this impl, but thanks for you work on this anyway @ithinuel. |
ithinuel commentedMay 10, 2018
This adds the conversion from and into boolean for integer types.
This is also discused here : https://internals.rust-lang.org/t/from-bool-for-primitive-integer-types