Re: [PATCH 2/2] rust: alloc: add `Vec::dec_len`

Tamir Duberstein
2025-03-17T17:28:41Z

On Mon, Mar 17, 2025 at 1:25 PM Benno Lossin <benno.lossin@proton.me> wrote:
>
> On Mon Mar 17, 2025 at 4:37 PM CET, Tamir Duberstein wrote:
> > On Mon, Mar 17, 2025 at 10:39 AM Benno Lossin <benno.lossin@proton.me> wrote:
> >> On Mon Mar 17, 2025 at 12:34 PM CET, Tamir Duberstein wrote:
> >> > On Mon, Mar 17, 2025 at 6:04 AM Benno Lossin <benno.lossin@proton.me> wrote:
> >> >> On Sun Mar 16, 2025 at 11:32 PM CET, Tamir Duberstein wrote:
> >> >> > + ///
> >> >> > + /// # Safety
> >> >> > + ///
> >> >> > + /// - `count` must be less than or equal to `self.len`.
> >> >>
> >> >> I also think that we should use saturating_sub instead and then not have
> >> >> to worry about this. (It should still be documented in the function
> >> >> though). That way this can also be a safe function.
> >> >
> >> > This doesn't seem better to me. I'd prefer to have more rather than
> >> > fewer guardrails on such low-level operations.
> >>
> >> Your second sentence seems like an argument for making it safe? I think
> >> it's a lot better as a safe function.
> >
> > The guardrail I was referring to is the requirement that the caller
> > write a safety comment.
>
> But saturating_sub is a better guardrail?

It's a different kind of guardrail; one that attempts to do something
correct in the presence of incorrect code.

Put another way: do we have line of sight on a caller that wants to
use `dec_len` without already knowing the current length of the
vector?