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

From: Andreas Hindborg
Date: Mon Mar 03 2025 - 09:23:30 EST


"Oliver Mangold" <oliver.mangold@xxxxx> writes:

> From: Oliver Mangold <oliver.mangold@xxxxx>
> Date: Fri, 21 Feb 2025 08:36:46 +0100
> Subject: [PATCH] rust: adding UniqueRefCounted and UniqueRef types
>
> Add `UniqueRef` as a variant of `ARef` that is guaranteed to be unique.
> This is useful when mutable access to the underlying type is required
> and we can guarantee uniqueness, and when APIs that would normally take
> an `ARef` require uniqueness.
>
> Signed-off-by: Oliver Mangold <oliver.mangold@xxxxx>


This part:


This should address all issues that have been raised with v2:

- Added a default implementation for unique_to_shared() which does a simple
rewrap of the underlying object.
- Added a SimpleUniqueRefCounted trait which requires only to implement
is_unique() as Benoît asked for. Maybe the feature is not worth
the extra code, though. For me keeping it or removing would be both fine.
- Removed the unsound conversion from &T to UniqueRef, as spotted by Benoît.
- Relaxed the requirements for Send and Sync, to be identical to the ones
for Box. See comment below.
- Added Examples for both UniqueRefCounted and SimpleUniqueRefCounted
as asked for by Boqun Feng.
For me they compile and run without errors as KUnits.
- Changed the commit message like suggested by Andreas.

@Benoît: I think you are right about Send and Sync.
What gave me a bit of a headache is if Send really does not require
the underlying object to be Sync, as the refcount itself -
which is part of the object - might be touched concurrently in a case
like with tag_to_req(), but I think one would not implement
something like that without having a synchronized refcount.

Best regards,

Oliver


Goes immedieatly after the cut like so:


From: Oliver Mangold <oliver.mangold@xxxxx>
Date: Fri, 21 Feb 2025 08:36:46 +0100
Subject: [PATCH] rust: adding UniqueRefCounted and UniqueRef types

Add `UniqueRef` as a variant of `ARef` that is guaranteed to be unique.
This is useful when mutable access to the underlying type is required
and we can guarantee uniqueness, and when APIs that would normally take
an `ARef` require uniqueness.

Signed-off-by: Oliver Mangold <oliver.mangold@xxxxx>
---


This should address all issues that have been raised with v2:

- Added a default implementation for unique_to_shared() which does a simple
rewrap of the underlying object.
- Added a SimpleUniqueRefCounted trait which requires only to implement
is_unique() as Benoît asked for. Maybe the feature is not worth
the extra code, though. For me keeping it or removing would be both fine.
- Removed the unsound conversion from &T to UniqueRef, as spotted by Benoît.
- Relaxed the requirements for Send and Sync, to be identical to the ones
for Box. See comment below.
- Added Examples for both UniqueRefCounted and SimpleUniqueRefCounted
as asked for by Boqun Feng.
For me they compile and run without errors as KUnits.
- Changed the commit message like suggested by Andreas.

@Benoît: I think you are right about Send and Sync.
What gave me a bit of a headache is if Send really does not require
the underlying object to be Sync, as the refcount itself -
which is part of the object - might be touched concurrently in a case
like with tag_to_req(), but I think one would not implement
something like that without having a synchronized refcount.

Best regards,

Oliver

---

rust/kernel/types.rs | 315 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 315 insertions(+)


Again, I'll recommend the use of b4 [1].

Best regards,
Andreas Hindborg


[1] https://b4.docs.kernel.org/en/latest/contributor/prep.html