Re: [PATCH v4] rust: adding UniqueRefCounted and UniqueRef types
From: Oliver Mangold
Date: Thu Mar 06 2025 - 07:03:46 EST
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.
>
> [1]: https://lore.kernel.org/rust-for-linux/20250202-rust-page-v1-1-e3170d7fe55e@xxxxxxxxxxxxx/
Oh, really very similar.