Add TryFrom<{integer}> for bool by ithinuel · Pull Request #50597 · rust-lang/rust (original) (raw)
We have #50554 for the bool -> int direction, so let's focus this PR on the int -> bool direction, i.e. the proposed implementation of impl TryFrom<int> for bool.
The largest concern I see in the discussion above is that different people could have different expectations for the int -> bool conversion.
- Based on the convention
0 = success, * = error, one could argue that0 -> Ok(true), * -> Ok(false). - To be strictly in line with the
bool -> intimplementation from Add From for int types #50554, one could argue that0 -> Ok(false), 1 -> Ok(true), * -> Err(()). - Following the C compiler, one could argue that
0 -> Ok(false), * -> Ok(true).
Note that (1) and (3) could be implemented as From instead of TryFrom.
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 bool should behave as a "tiny int" (i.e. an i1, or rather a u1) or as a completely distinct type.
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 merge / close FCP.