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

Oliver Mangold
2025-03-06T10:14:14Z

On 250306 0935, Alice Ryhl wrote:
>
> Ultimately, if a struct implements AlwaysRefcounted, then you can always
> increments its refcount.

> If you want a version of the struct where that
> is not the case, then you need a different struct that does *not*
> implement AlwaysRefcounted.
>
I guess so, but it would be possible to make 'From<&T> for ARef<T>' opt-in,
by requiring a separate marker trait.

That you can call 'AlwaysRefCounted::inc_ref()' directly doesn't seem a
problem to me, as it will only leak the object, not create a reference.

A quick grep shows me that there are currently 7 implementers:

unsafe impl AlwaysRefCounted for Credential {
unsafe impl AlwaysRefCounted for File {
unsafe impl AlwaysRefCounted for LocalFile {
unsafe impl<T: Operations> AlwaysRefCounted for Request<T> {
unsafe impl crate::types::AlwaysRefCounted for Device {
unsafe impl crate::types::AlwaysRefCounted for Task {
unsafe impl AlwaysRefCounted for PidNamespace {

So it looks doable to me.

Oliver