Re: [PATCH v4] rust: adding UniqueRefCounted and UniqueRef types

Alice Ryhl
2025-03-06T12:09:01Z

On Thu, Mar 6, 2025 at 1:03 PM Oliver Mangold <oliver.mangold@pm.me> wrote:
>
> On 250306 1131, Alice Ryhl wrote:
> >
> > How about this:
> >
> > * Rename AlwaysRefCounted to RefCounted.
> > * Introduce a new AlwaysRefCounted trait with no methods and gate
> > `From<&T>` on it. It has RefCounted as a sub-trait.
> > * Introduce an Ownable trait with an Owned type like in [1].
> > * Given an Owned<T> where T:RefCounted you can convert from Owned<T> to
> > ARef<T>.
> >
> > And there needs to be a safety requirement on Ownable or
> > AlwaysRefCounted which requires that a type cannot implement both
> > traits. Alternatively, if a type implements both, it needs to be safe to
> > have both Owned<T> and ARef<T> references at the same time, which could
> > make sense for a type that has one "special" reference and many normal
> > references.
> >
> > If you want conversions ARef<T> to Owned<T>, you should add a new trait
> > TryIntoOwned that's a super-trait of both RefCounted and Owned and has
> > the `try` method for the conversion.
> >
> > Thoughts?
> >
> Yes. Sounds good to me. Basically what I had in mind. Only the naming
> is different.
>
> I will build an implementation like this and post it as v5.
> I won't change the names of UniqueRef and UniqueRefCounted for now,
> but more out of laziness than because of having strong feelings about it.
> I like UniqueRef a bit better as our focus is on pointing out that
> it is unique. But if you or other prefers Owned I can change it.

Advantage of the Owned naming is that it also makes sense for types
that *only* support Owned pointers. The UniqueRef name kind of assumes
that it's refcounted, but the design I proposed does not have that
limitation.

Alice