Re: [PATCH v3 1/2] rust: non_empty: add NonEmpty
From: Gary Guo
Date: Wed Oct 14 2026 - 10:26:02 EST
On Wed Oct 14, 2026 at 12:55 PM BST, Miguel Ojeda wrote:
> On Wed, Oct 14, 2026 at 10:30 AM Lorenzo Delgado <lnsdev@xxxxxxxxx> wrote:
>>
>> +/// A container that can report whether it holds any element.
>> +///
>> +/// The result must not change while the container is only reachable through a shared reference.
>> +///
>> +/// Unsafe code may only rely on the result for a container type whose implementation it trusts,
>> +/// such as the ones in this module, since a safe implementation can return anything.
>> +pub trait IsEmpty {
>> + /// Returns `true` if the container holds no element.
>> + fn is_empty(&self) -> bool;
>> +}
>
> Do we need this to be a safe trait? If it is unsafe, then we can
> simplify the invariant and more easily think about the type, like
> other `Non*`s, no?
I think in this case the meaning of "empty" depends on the container itself.
Unsafe code that relies on "is_empty" would be the `NonEmpty<Container>` itself,
so it just needs to trust itself (as boundary of unsafe is module level).
I think of almost all of the text above can be removed. The invariant of
`NonEmpty` can just be "`IsEmpty::is_empty` has returned true" and how it is
interpreted is up to the container type.
Best,
Gary