[RFC 4/5] rust: sync: impl Debug for {Unique,}Arc

Boqun Feng posted 5 patches 2 years, 3 months ago
There is a newer version of this series
[RFC 4/5] rust: sync: impl Debug for {Unique,}Arc
Posted by Boqun Feng 2 years, 3 months ago
This allows printing macros to print the data and the refcount nubmer
of these struct for debugging purposes.

Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
---
 rust/kernel/sync/arc.rs | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index 4d8de20c996f..f143d8305c36 100644
--- a/rust/kernel/sync/arc.rs
+++ b/rust/kernel/sync/arc.rs
@@ -474,6 +474,7 @@ impl<T: ?Sized> Deref for ArcBorrow<'_, T> {
 ///
 /// # test().unwrap();
 /// ```
+#[derive(Debug)]
 pub struct UniqueArc<T: ?Sized> {
     inner: Arc<T>,
 }
@@ -545,3 +546,15 @@ impl<T: fmt::Display + ?Sized> fmt::Display for Arc<T> {
         fmt::Display::fmt(self.deref(), f)
     }
 }
+
+impl<T: fmt::Debug + ?Sized> fmt::Debug for Arc<T> {
+    /// Formats debug output for [`Arc`].
+    ///
+    /// Refcount is also printed for debugging purpose.
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.debug_struct("Arc")
+            .field("refcount", &self.get_inner().count())
+            .field("data", &self.deref())
+            .finish()
+    }
+}
-- 
2.39.1
Re: [RFC 4/5] rust: sync: impl Debug for {Unique,}Arc
Posted by Vincenzo Palazzo 2 years, 3 months ago
On Thu Feb 2, 2023 at 12:22 AM CET, Boqun Feng wrote:
> This allows printing macros to print the data and the refcount nubmer
> of these struct for debugging purposes.
>
> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> ---
Reviwed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>

>  rust/kernel/sync/arc.rs | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
>
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index 4d8de20c996f..f143d8305c36 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -474,6 +474,7 @@ impl<T: ?Sized> Deref for ArcBorrow<'_, T> {
>  ///
>  /// # test().unwrap();
>  /// ```
> +#[derive(Debug)]
>  pub struct UniqueArc<T: ?Sized> {
>      inner: Arc<T>,
>  }
> @@ -545,3 +546,15 @@ impl<T: fmt::Display + ?Sized> fmt::Display for Arc<T> {
>          fmt::Display::fmt(self.deref(), f)
>      }
>  }
> +
> +impl<T: fmt::Debug + ?Sized> fmt::Debug for Arc<T> {
> +    /// Formats debug output for [`Arc`].
> +    ///
> +    /// Refcount is also printed for debugging purpose.
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        f.debug_struct("Arc")
> +            .field("refcount", &self.get_inner().count())
> +            .field("data", &self.deref())
> +            .finish()
> +    }
> +}
> -- 
> 2.39.1
Re: [RFC 4/5] rust: sync: impl Debug for {Unique,}Arc
Posted by Gary Guo 2 years, 3 months ago
On Wed,  1 Feb 2023 15:22:43 -0800
Boqun Feng <boqun.feng@gmail.com> wrote:

> This allows printing macros to print the data and the refcount nubmer
> of these struct for debugging purposes.
> 
> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> ---
>  rust/kernel/sync/arc.rs | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
> 
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index 4d8de20c996f..f143d8305c36 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -474,6 +474,7 @@ impl<T: ?Sized> Deref for ArcBorrow<'_, T> {
>  ///
>  /// # test().unwrap();
>  /// ```
> +#[derive(Debug)]

I don't think this should be a `#[derive(Debug)]`. For `UniqueArc` the
refcount field in `Arc` is useless, and we should just delegate the
`Debug` impl to that of deref, just like `Display` does.

Best,
Gary
Re: [RFC 4/5] rust: sync: impl Debug for {Unique,}Arc
Posted by Boqun Feng 2 years, 3 months ago
On Thu, Feb 02, 2023 at 02:28:04PM +0000, Gary Guo wrote:
> On Wed,  1 Feb 2023 15:22:43 -0800
> Boqun Feng <boqun.feng@gmail.com> wrote:
> 
> > This allows printing macros to print the data and the refcount nubmer
> > of these struct for debugging purposes.
> > 
> > Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> > ---
> >  rust/kernel/sync/arc.rs | 13 +++++++++++++
> >  1 file changed, 13 insertions(+)
> > 
> > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> > index 4d8de20c996f..f143d8305c36 100644
> > --- a/rust/kernel/sync/arc.rs
> > +++ b/rust/kernel/sync/arc.rs
> > @@ -474,6 +474,7 @@ impl<T: ?Sized> Deref for ArcBorrow<'_, T> {
> >  ///
> >  /// # test().unwrap();
> >  /// ```
> > +#[derive(Debug)]
> 
> I don't think this should be a `#[derive(Debug)]`. For `UniqueArc` the
> refcount field in `Arc` is useless, and we should just delegate the
> `Debug` impl to that of deref, just like `Display` does.
> 

I was just being lazy ;-) Will change this in v2.

Regards,
Boqun

> Best,
> Gary