-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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 TryFrom<{integer}> for bool #50597
Add TryFrom<{integer}> for bool #50597
Conversation
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. |
src/libcore/num/mod.rs
Outdated
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 |
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.
There's a PR open for this part at #50554
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.
😮 I'm few hours late
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.
I updated the issue number to #46109
src/libcore/num/mod.rs
Outdated
@@ -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 |
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.
This is lossy, so feels questionable whether it should be there in From
.
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.
I use from here but I can change to TryFrom
if required.
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.
Updated to use TryFrom instead.
This fixes : #46109. |
src/libcore/num/mod.rs
Outdated
@@ -4259,6 +4259,25 @@ macro_rules! try_from_both_bounded { | |||
)*} | |||
} | |||
|
|||
macro_rules! try_bool_from { | |||
($($source:ty),*) => {$( | |||
#[unstable(feature = "try_bool_from", issue = "0")] |
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.
Should I create an issue for this or is the PR number ok here ?
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.
I am not even sure this should be tagged as unstable.
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.
impl
s are insta-stable, so they should be marked stable with the corresponding release (currently 1.28, I think).
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.
Ok, Thank you.
So I changed :
- the
impl From<bool> for ..
to stable 1.28.0 with the feature namelossless_bool_conv
to be consistent with other lossless conversions. impl TryFrom<T> for bool
to the unstable featuretry_from
linking Tracking issue for TryFrom/TryInto traits #33417 as the issue number.
A practical use case of this can be found here : https://github.com/ithinuel/rusty-printer cc @TimNN @clarcharr |
I don't think that |
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 |
Err, you're right, I swapped true and false there. |
@scottmcm @clarcharr @shepmaster |
@ithinuel I'm torn. On the one hand I think Well, you could remove the |
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. |
@ithinuel Because there's two interpretations. The "bool is i1" interpretation says it should only accept |
@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 |
TBQH I think that |
Ping from triage @TimNN! This PR needs your review. |
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 |
I think this is a reason to not have these conversions at all. There is precedent with |
I think @sfackler may have been suggesting to use a private trait. Something like: trait TryConvert<T> {
fn try_convert(self) -> Result<T, ()>;
}
impl<T, U> TryConvert<T> for U where U: TryInto<T> {
fn try_convert(self) -> Result<T, ()> { self.try_into().map_err(|_| ()) }
}
impl TryConvert<bool> for u32 {
// ...
} |
@SimonSapin I don't think |
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 😄 |
☔ The latest upstream changes (presumably #50554) made this pull request unmergeable. Please resolve the merge conflicts. |
3f20ecd
to
25a622a
Compare
Squashed for a clean history & rebased :) |
@rust-lang/libs any update on this PR ? |
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. |
@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. |
@ithinuel: So, if you're main use case is macros, I've just thought about another solution: Add your own |
This is what I mean. |
This means code duplication or redundant boiler plate. 😐 This is not something we want to inflict to users. |
@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 |
I think this is a strong reason. @rfcbot fcp close |
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. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to close, as per the review above, is now complete. |
As discussed we’re not taking this impl, but thanks for you work on this anyway @ithinuel. |
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