rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: sync: extend module documentation of aref
@ 2025-07-22 12:14 Benno Lossin
  2025-07-23 10:25 ` Daniel Sedlak
  2025-08-08  3:20 ` Alexandre Courbot
  0 siblings, 2 replies; 4+ messages in thread
From: Benno Lossin @ 2025-07-22 12:14 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Shankari Anand
  Cc: rust-for-linux, linux-kernel

Commit 07dad44aa9a9 ("rust: kernel: move ARef and AlwaysRefCounted to
sync::aref") moved `ARef` and `AlwaysRefCounted` into their own module.
In that process only a short, single line description of the module was
added. Extend the description by explaining what is meant by "internal
reference counting", the two items in the trait & the difference to
`Arc`.

Signed-off-by: Benno Lossin <lossin@kernel.org>
---
 rust/kernel/sync/aref.rs | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs
index dbd77bb68617..1c212238c0e5 100644
--- a/rust/kernel/sync/aref.rs
+++ b/rust/kernel/sync/aref.rs
@@ -1,6 +1,21 @@
 // SPDX-License-Identifier: GPL-2.0
 
 //! Internal reference counting support.
+//!
+//! Many C types already have their own reference counting mechanism (e.g. by storing a
+//! `refcount_t`). This module provides support for directly using their internal reference count
+//! from Rust; instead of making users have to use an additional Rust-reference count in the form of
+//! [`Arc`].
+//!
+//! The smart pointer [`ARef<T>`] acts similarly to [`Arc<T>`] in that it holds a refcount on the
+//! underlying object, but this refcount is internal to the object. It essentially is a Rust
+//! implementation of the `get_` and `put_` pattern used in C for reference counting.
+//!
+//! To make use of [`ARef<MyType>`], `MyType` needs to implement [`AlwaysRefCounted`]. It is a trait
+//! for accessing the internal reference count of an object of the `MyType` type.
+//!
+//! [`Arc`]: crate::sync::Arc
+//! [`Arc<T>`]: crate::sync::Arc
 
 use core::{marker::PhantomData, mem::ManuallyDrop, ops::Deref, ptr::NonNull};
 

base-commit: 07dad44aa9a93b16af19e8609a10b241c352b440
-- 
2.50.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] rust: sync: extend module documentation of aref
  2025-07-22 12:14 [PATCH] rust: sync: extend module documentation of aref Benno Lossin
@ 2025-07-23 10:25 ` Daniel Sedlak
  2025-07-23 10:51   ` Miguel Ojeda
  2025-08-08  3:20 ` Alexandre Courbot
  1 sibling, 1 reply; 4+ messages in thread
From: Daniel Sedlak @ 2025-07-23 10:25 UTC (permalink / raw)
  To: Benno Lossin, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Shankari Anand
  Cc: rust-for-linux, linux-kernel

Hi Benno,

On 7/22/25 2:14 PM, Benno Lossin wrote:
> Commit 07dad44aa9a9 ("rust: kernel: move ARef and AlwaysRefCounted to
> sync::aref") moved `ARef` and `AlwaysRefCounted` into their own module.
> In that process only a short, single line description of the module was
> added. Extend the description by explaining what is meant by "internal
> reference counting", the two items in the trait & the difference to
> `Arc`.
> 
> Signed-off-by: Benno Lossin <lossin@kernel.org>
> ---
>   rust/kernel/sync/aref.rs | 15 +++++++++++++++
>   1 file changed, 15 insertions(+)
> 
> diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs
> index dbd77bb68617..1c212238c0e5 100644
> --- a/rust/kernel/sync/aref.rs
> +++ b/rust/kernel/sync/aref.rs
> @@ -1,6 +1,21 @@
>   // SPDX-License-Identifier: GPL-2.0
>   
>   //! Internal reference counting support.
> +//!
> +//! Many C types already have their own reference counting mechanism (e.g. by storing a
> +//! `refcount_t`). This module provides support for directly using their internal reference count
> +//! from Rust; instead of making users have to use an additional Rust-reference count in the form of
> +//! [`Arc`].
> +//!
> +//! The smart pointer [`ARef<T>`] acts similarly to [`Arc<T>`] in that it holds a refcount on the
> +//! underlying object, but this refcount is internal to the object. It essentially is a Rust
> +//! implementation of the `get_` and `put_` pattern used in C for reference counting.
> +//!
> +//! To make use of [`ARef<MyType>`], `MyType` needs to implement [`AlwaysRefCounted`]. It is a trait
> +//! for accessing the internal reference count of an object of the `MyType` type.
> +//!
> +//! [`Arc`]: crate::sync::Arc
> +//! [`Arc<T>`]: crate::sync::Arc

It got me curios. Why is it required to declare the doc reference for 
Arc and Arc<T>, but not ARef<MyType> and ARef<T>?

Is it because ARef is in file scope but not the Arc?

If so, you could just add

	use crate::sync::Arc;

in the file imports and you wouldn't have to duplicate the

	//! [`Arc`]: crate::sync::Arc
	//! [`Arc<T>`]: crate::sync::Arc

And even cleanup it a bit by simplifying

	[`Arc`](crate::sync::Arc) to [`Arc`]

in the AlwaysRefCounted trait.

Thanks!
Daniel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] rust: sync: extend module documentation of aref
  2025-07-23 10:25 ` Daniel Sedlak
@ 2025-07-23 10:51   ` Miguel Ojeda
  0 siblings, 0 replies; 4+ messages in thread
From: Miguel Ojeda @ 2025-07-23 10:51 UTC (permalink / raw)
  To: Daniel Sedlak
  Cc: Benno Lossin, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Shankari Anand, rust-for-linux, linux-kernel

On Wed, Jul 23, 2025 at 12:25 PM Daniel Sedlak <daniel@sedlak.dev> wrote:
>
> It got me curios. Why is it required to declare the doc reference for
> Arc and Arc<T>, but not ARef<MyType> and ARef<T>?
>
> Is it because ARef is in file scope but not the Arc?
>
> If so, you could just add
>
>         use crate::sync::Arc;

That would be unused -- i.e. we don't add imports in the actual code
only for docs.

The compiler also warns about it (`unused_imports`).

Thanks!

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] rust: sync: extend module documentation of aref
  2025-07-22 12:14 [PATCH] rust: sync: extend module documentation of aref Benno Lossin
  2025-07-23 10:25 ` Daniel Sedlak
@ 2025-08-08  3:20 ` Alexandre Courbot
  1 sibling, 0 replies; 4+ messages in thread
From: Alexandre Courbot @ 2025-08-08  3:20 UTC (permalink / raw)
  To: Benno Lossin, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Shankari Anand
  Cc: rust-for-linux, linux-kernel

On Tue Jul 22, 2025 at 9:14 PM JST, Benno Lossin wrote:
> Commit 07dad44aa9a9 ("rust: kernel: move ARef and AlwaysRefCounted to
> sync::aref") moved `ARef` and `AlwaysRefCounted` into their own module.
> In that process only a short, single line description of the module was
> added. Extend the description by explaining what is meant by "internal
> reference counting", the two items in the trait & the difference to
> `Arc`.
>
> Signed-off-by: Benno Lossin <lossin@kernel.org>
> ---
>  rust/kernel/sync/aref.rs | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)
>
> diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs
> index dbd77bb68617..1c212238c0e5 100644
> --- a/rust/kernel/sync/aref.rs
> +++ b/rust/kernel/sync/aref.rs
> @@ -1,6 +1,21 @@
>  // SPDX-License-Identifier: GPL-2.0
>  
>  //! Internal reference counting support.
> +//!
> +//! Many C types already have their own reference counting mechanism (e.g. by storing a
> +//! `refcount_t`). This module provides support for directly using their internal reference count
> +//! from Rust; instead of making users have to use an additional Rust-reference count in the form of
> +//! [`Arc`].
> +//!
> +//! The smart pointer [`ARef<T>`] acts similarly to [`Arc<T>`] in that it holds a refcount on the
> +//! underlying object, but this refcount is internal to the object. It essentially is a Rust
> +//! implementation of the `get_` and `put_` pattern used in C for reference counting.
> +//!
> +//! To make use of [`ARef<MyType>`], `MyType` needs to implement [`AlwaysRefCounted`]. It is a trait
> +//! for accessing the internal reference count of an object of the `MyType` type.
> +//!
> +//! [`Arc`]: crate::sync::Arc
> +//! [`Arc<T>`]: crate::sync::Arc

Very useful explanation IMHO.

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-08-08  3:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-22 12:14 [PATCH] rust: sync: extend module documentation of aref Benno Lossin
2025-07-23 10:25 ` Daniel Sedlak
2025-07-23 10:51   ` Miguel Ojeda
2025-08-08  3:20 ` Alexandre Courbot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).