Skip to content
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

From<bool> for integers not implemented? #46109

Closed
therealprof opened this issue Nov 20, 2017 · 4 comments
Closed

From<bool> for integers not implemented? #46109

therealprof opened this issue Nov 20, 2017 · 4 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@therealprof
Copy link
Contributor

I would find it incredibly useful to being able to cast a bool to another type so I can shift it around to generate bitmasks but ALAS this is not possible at the moment due to missing From trait impls:

error[E0277]: the trait bound `u32: core::convert::From<bool>` is not satisfied

Using as instead works fine but is frowned upon by clippy if the type is not a bool but a different type instead:

warning: casting u16 to u32 may become silently lossy if types change

Since the code in question is automatically generated it would be great if there was one common implementation that can (and should) be used for all types.

@TimNN TimNN added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Nov 21, 2017
@kennytm kennytm changed the title From<bool> not implemented? From<bool> for integers not implemented? Nov 21, 2017
@abonander
Copy link
Contributor

abonander commented Nov 22, 2017

IIRC, the integer value of bool is meant to be undefined, I guess to allow possible optimizations with stuff like enum tags.

Addendum: in fact, the compiler is now taking advantage of this undefinedness: #45225

I guess the impl could be as simple as if val { 1 } else { 0 }, but it really depends on your use-case. I believe C booleans are like "false if zero, true otherwise".

@goffrie
Copy link
Contributor

goffrie commented Nov 22, 2017

bools are always 0 or 1 in rust. The change you linked makes use of the other bytes (2 through 255), but has no bearing on valid bools (and it can't since true as u32 is already defined to be 1u32).

But none of this is really related to this issue. IMO clippy is being silly and you should just use as, but it might be good to have the From impl too.

@mtahmed
Copy link

mtahmed commented Dec 6, 2017

Is this issue beginner-friendly? I could work on it with some guidance.

@ithinuel
Copy link
Contributor

ithinuel commented May 14, 2018

In #50597 I propose to use From<bool> for {integer} for the lossless conversion using if val { 1 } else { 0 } instead of as just in case something weird happened and true/false are not 1 and 0.
I also propose TryFrom<{Integer}> for bool to convert from an integer while checking for any data loss.

There is a discussion for this topic on internals.rust-lang.org and a use case here : https://github.com/ithinuel/rusty-printer

You'll need to build rust with the target thumbv7m-none-eabi to see it in action.
If you want to dive in, it is used in the set of macros register! defined in ./silica/src/register.rs and used in ./silica_arm_cortexm4/src/ppb/scb.rs.

bors added a commit that referenced this issue Jun 2, 2018
Add From<bool> for int types

Fixes #46109.
bors added a commit that referenced this issue Jun 2, 2018
Add From<bool> for int types

Fixes #46109.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants