Rust for Linux List
 help / color / mirror / Atom feed
From: Younes Akhouayri <git@younes.io>
To: Alexandre Courbot <acourbot@nvidia.com>
Cc: Rust For Linux <rust-for-linux@vger.kernel.org>,
	Yury Norov <yury.norov@gmail.com>, Ojeda <ojeda@kernel.org>,
	Yu Whisper Personal <yu.whisper.personal@gmail.com>
Subject: Re: From<bool> and Bounded's invariant for signed N = 1
Date: Thu, 13 Aug 2026 21:52:34 +0200 (CEST)	[thread overview]
Message-ID: <OzwfgEC--F-9@younes.io> (raw)
In-Reply-To: <DKNU5IBDCA44.28C93KM4IQS44@nvidia.com>

Thank you both for the feedback. Yes, I can send a patch.

Just to confirm the intended scope: excluding signed integers entirely would also remove valid conversions such as Bounded::<i8, 2>::from(true), since signed Bounded values with N >= 2 can represent both 0 and 1.

Would you prefer the simpler unsigned-only restriction, or should the patch preserve signed conversions for N >= 2?


--

Best regards,
Younes



Aug 13, 2026, 15:04 by acourbot@nvidia.com:

> 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?
>

      reply	other threads:[~2026-08-13 19:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-13  9:46 From<bool> and Bounded's invariant for signed N = 1 Younes Akhouayri
2026-08-13 10:16 ` Miguel Ojeda
2026-08-13 13:04 ` Alexandre Courbot
2026-08-13 19:52   ` Younes Akhouayri [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=OzwfgEC--F-9@younes.io \
    --to=git@younes.io \
    --cc=acourbot@nvidia.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=yu.whisper.personal@gmail.com \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox