Re: [PATCH] rust: num: reject bool conversion to signed one-bit Bounded

"Gary Guo" <[email protected]>
Newsgroups org.kernel.vger.rust-for-linux,org.kernel.vger.stable
Message-ID <[email protected]>
On Sat Aug 15, 2026 at 9:06 AM BST, Younes Akhouayri via B4 Relay wrote:
> From: Younes Akhouayri <[email protected]>
>
> From<bool> turns true into 1. A signed Bounded with N = 1 can hold
> only -1 and 0. From<bool> therefore creates a value that the type does
> not allow. Deref assumes the value is valid and calls
> unreachable_unchecked() when it is not. Safe Rust can therefore reach
> undefined behavior.
>
> Rust cannot write N >= 2 directly in this From implementation. Add a
> private BoolFits trait. It accepts every valid unsigned width and signed
> widths from 2 through 128. The current Integer types do not use more
> than 128 bits, so this keeps every valid conversion.

Given that the existing `From<Bounded<T, N>> for uXX` implementation all
requires signedness match, I think this is being inconsistent (if we view bool
as u1). I think restricting it to just unsigned integer is better.

Best,
Gary

>
> Fixes: 01e345e82ec3 ("rust: num: add Bounded integer wrapping type")
> Closes: https://lore.kernel.org/rust-for-linux/[email protected]/
> Cc: [email protected]
> Assisted-by: Codex:gpt-5.6-sol
> Signed-off-by: Younes Akhouayri <[email protected]>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.