Re: From<bool> and Bounded's invariant for signed N = 1

"Alexandre Courbot" <[email protected]>
Newsgroups org.kernel.vger.rust-for-linux
Message-ID <[email protected]>
On Thu Aug 13, 2026 at 6:46 PM JST, Younes Akhouayri wrote:
> Hi,
>
> There seems to be an invariant violation in the generic From<bool>
> implementation for Bounded.
>
> As I understand it, Bounded<i8, 1> can represent only -1 and 0, while
> i8::from(true) produces 1. However, this safe code is accepted:
>
> let value: Bounded<i8, 1> = true.into();
> let _raw: i8 = *value;
>
> Bounded::<i8, 1>::try_new(1) rejects the same value, but From<bool>
> passes it directly to the unsafe __new() constructor. The resulting
> Bounded value violates the invariant relied upon by Deref, whose
> failure branch calls unreachable_unchecked().
>
> Am I missing an intended constraint on this conversion? If not, it
> seems the signed N = 1 case needs to be excluded or handled
> differently.
>
> Any thoughts?

I think you are correct. There is a `Signedness` associated type on the
`Integer` trait, so it should be trivial to exclude signed integers - do
you want to send a patch for that?
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.