linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] rust: sync: Arc: Implement Debug and Display
@ 2023-02-07 18:52 Boqun Feng
  2023-02-07 18:52 ` [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc Boqun Feng
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Boqun Feng @ 2023-02-07 18:52 UTC (permalink / raw)
  To: linux-kernel, rust-for-linux
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, Björn Roy Baron, Vincenzo Palazzo, Will Deacon,
	Peter Zijlstra, Mark Rutland, Greg Kroah-Hartman,
	Sergio González Collado, Finn Behrens

previous version:

v1:	https://lore.kernel.org/rust-for-linux/20230201232244.212908-1-boqun.feng@gmail.com/	

Changes since v1:

*	Remove the refcount for Debug impl as per Peter and Greg.
*	Since the refcount_read() bits are removed, therefore squash all
	4 patches into one (I keep the Reviewed-by tags for anyone that
	gave to both patch #1 and patch #4 in v1). Thanks for everyone
	for the reviewing ;-)


I found that our Arc doesn't implement `Debug` or `Display` when I tried
to play with them, therefore add these implementation.

With these changes, I could get the following print with the sample code
in patch #2:

	[..] rust_print: 1
	[..] rust_print: "hello, world"
	[..] rust_print: [samples/rust/rust_print.rs:34] c = "hello, world"
	[..] rust_print: "hello, world"

Suggestions and comments are welcome!

Regards,
Boqun

-- 
2.39.1


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

* [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc
  2023-02-07 18:52 [PATCH v2 0/2] rust: sync: Arc: Implement Debug and Display Boqun Feng
@ 2023-02-07 18:52 ` Boqun Feng
  2023-02-07 19:03   ` Carlos Bilbao
                     ` (4 more replies)
  2023-02-07 18:52 ` [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing Boqun Feng
  2023-04-10  2:53 ` [PATCH v2 0/2] rust: sync: Arc: Implement Debug and Display Miguel Ojeda
  2 siblings, 5 replies; 17+ messages in thread
From: Boqun Feng @ 2023-02-07 18:52 UTC (permalink / raw)
  To: linux-kernel, rust-for-linux
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, Björn Roy Baron, Vincenzo Palazzo, Will Deacon,
	Peter Zijlstra, Mark Rutland, Greg Kroah-Hartman,
	Sergio González Collado, Finn Behrens

This allows printing the inner data of `Arc` and its friends if the
inner data implements `Display` or `Debug`. It's useful for logging and
debugging purpose.

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

diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index 519a6ec43644..e6176d9b5b29 100644
--- a/rust/kernel/sync/arc.rs
+++ b/rust/kernel/sync/arc.rs
@@ -22,6 +22,7 @@ use crate::{
 };
 use alloc::boxed::Box;
 use core::{
+    fmt,
     marker::{PhantomData, Unsize},
     mem::{ManuallyDrop, MaybeUninit},
     ops::{Deref, DerefMut},
@@ -522,3 +523,27 @@ impl<T: ?Sized> DerefMut for UniqueArc<T> {
         unsafe { &mut self.inner.ptr.as_mut().data }
     }
 }
+
+impl<T: fmt::Display + ?Sized> fmt::Display for UniqueArc<T> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt::Display::fmt(self.deref(), f)
+    }
+}
+
+impl<T: fmt::Display + ?Sized> fmt::Display for Arc<T> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt::Display::fmt(self.deref(), f)
+    }
+}
+
+impl<T: fmt::Debug + ?Sized> fmt::Debug for UniqueArc<T> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt::Debug::fmt(self.deref(), f)
+    }
+}
+
+impl<T: fmt::Debug + ?Sized> fmt::Debug for Arc<T> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt::Debug::fmt(self.deref(), f)
+    }
+}
-- 
2.39.1


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

* [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing
  2023-02-07 18:52 [PATCH v2 0/2] rust: sync: Arc: Implement Debug and Display Boqun Feng
  2023-02-07 18:52 ` [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc Boqun Feng
@ 2023-02-07 18:52 ` Boqun Feng
  2023-02-08  4:57   ` Gary Guo
                     ` (2 more replies)
  2023-04-10  2:53 ` [PATCH v2 0/2] rust: sync: Arc: Implement Debug and Display Miguel Ojeda
  2 siblings, 3 replies; 17+ messages in thread
From: Boqun Feng @ 2023-02-07 18:52 UTC (permalink / raw)
  To: linux-kernel, rust-for-linux
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, Björn Roy Baron, Vincenzo Palazzo, Will Deacon,
	Peter Zijlstra, Mark Rutland, Greg Kroah-Hartman,
	Sergio González Collado, Finn Behrens

This both demonstrates the usage of different print format in Rust and
serves as a selftest for the `Display` and `Debug` implementation of
`Arc` and its friends.

Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Reviewed-by: Björn Roy Baron <bjorn3_gh@protonmail.com>
Reviewed-by: Finn Behrens <fin@nyantec.com>
Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
---
 samples/rust/rust_print.rs | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/samples/rust/rust_print.rs b/samples/rust/rust_print.rs
index 8b39d9cef6d1..165a8d7b1c07 100644
--- a/samples/rust/rust_print.rs
+++ b/samples/rust/rust_print.rs
@@ -15,6 +15,30 @@ module! {
 
 struct RustPrint;
 
+fn arc_print() -> Result {
+    use kernel::sync::*;
+
+    let a = Arc::try_new(1)?;
+    let b = UniqueArc::try_new("hello, world")?;
+
+    // Prints the value of data in `a`.
+    pr_info!("{}", a);
+
+    // Uses ":?" to print debug fmt of `b`.
+    pr_info!("{:?}", b);
+
+    let a: Arc<&str> = b.into();
+    let c = a.clone();
+
+    // Uses `dbg` to print, will move `c`.
+    dbg!(c);
+
+    // Prints debug fmt with pretty-print "#" and number-in-hex "x".
+    pr_info!("{:#x?}", a);
+
+    Ok(())
+}
+
 impl kernel::Module for RustPrint {
     fn init(_module: &'static ThisModule) -> Result<Self> {
         pr_info!("Rust printing macros sample (init)\n");
@@ -43,6 +67,8 @@ impl kernel::Module for RustPrint {
         pr_cont!(" is {}", "continued");
         pr_cont!(" with {}\n", "args");
 
+        arc_print()?;
+
         Ok(RustPrint)
     }
 }
-- 
2.39.1


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

* Re: [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc
  2023-02-07 18:52 ` [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc Boqun Feng
@ 2023-02-07 19:03   ` Carlos Bilbao
  2023-02-07 20:27     ` Vincenzo Palazzo
  2023-02-07 20:45   ` Vincenzo Palazzo
                     ` (3 subsequent siblings)
  4 siblings, 1 reply; 17+ messages in thread
From: Carlos Bilbao @ 2023-02-07 19:03 UTC (permalink / raw)
  To: Boqun Feng, linux-kernel, rust-for-linux
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Gary Guo,
	Björn Roy Baron, Vincenzo Palazzo, Will Deacon,
	Peter Zijlstra, Mark Rutland, Greg Kroah-Hartman,
	Sergio González Collado, Finn Behrens

On 2/7/23 12:52, Boqun Feng wrote:

> This allows printing the inner data of `Arc` and its friends if the
> inner data implements `Display` or `Debug`. It's useful for logging and
> debugging purpose.
>
> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> Reviwed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>


s/Reviwed/Reviewed


> ---
>   rust/kernel/sync/arc.rs | 25 +++++++++++++++++++++++++
>   1 file changed, 25 insertions(+)
>
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index 519a6ec43644..e6176d9b5b29 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -22,6 +22,7 @@ use crate::{
>   };
>   use alloc::boxed::Box;
>   use core::{
> +    fmt,
>       marker::{PhantomData, Unsize},
>       mem::{ManuallyDrop, MaybeUninit},
>       ops::{Deref, DerefMut},
> @@ -522,3 +523,27 @@ impl<T: ?Sized> DerefMut for UniqueArc<T> {
>           unsafe { &mut self.inner.ptr.as_mut().data }
>       }
>   }
> +
> +impl<T: fmt::Display + ?Sized> fmt::Display for UniqueArc<T> {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Display::fmt(self.deref(), f)
> +    }
> +}
> +
> +impl<T: fmt::Display + ?Sized> fmt::Display for Arc<T> {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Display::fmt(self.deref(), f)
> +    }
> +}
> +
> +impl<T: fmt::Debug + ?Sized> fmt::Debug for UniqueArc<T> {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Debug::fmt(self.deref(), f)
> +    }
> +}
> +
> +impl<T: fmt::Debug + ?Sized> fmt::Debug for Arc<T> {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Debug::fmt(self.deref(), f)
> +    }
> +}


Just a thought, perhaps it's worth creating a macro to implement this
trait, since all do the same with fmt and others might join later on.


Thanks,
Carlos

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

* Re: [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc
  2023-02-07 19:03   ` Carlos Bilbao
@ 2023-02-07 20:27     ` Vincenzo Palazzo
  0 siblings, 0 replies; 17+ messages in thread
From: Vincenzo Palazzo @ 2023-02-07 20:27 UTC (permalink / raw)
  To: Carlos Bilbao, Boqun Feng, linux-kernel, rust-for-linux
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Gary Guo,
	Björn Roy Baron, Will Deacon, Peter Zijlstra, Mark Rutland,
	Greg Kroah-Hartman, Sergio González Collado, Finn Behrens

On Tue Feb 7, 2023 at 7:03 PM UTC, Carlos Bilbao wrote:
> On 2/7/23 12:52, Boqun Feng wrote:
>
> > This allows printing the inner data of `Arc` and its friends if the
> > inner data implements `Display` or `Debug`. It's useful for logging and
> > debugging purpose.
> >
> > Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> > Reviwed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
>
>
> s/Reviwed/Reviewed
Ops! this is my fautl! I will review the version 2.

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

* Re: [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc
  2023-02-07 18:52 ` [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc Boqun Feng
  2023-02-07 19:03   ` Carlos Bilbao
@ 2023-02-07 20:45   ` Vincenzo Palazzo
  2023-02-07 20:54     ` Boqun Feng
  2023-02-08  4:57   ` Gary Guo
                     ` (2 subsequent siblings)
  4 siblings, 1 reply; 17+ messages in thread
From: Vincenzo Palazzo @ 2023-02-07 20:45 UTC (permalink / raw)
  To: Boqun Feng, linux-kernel, rust-for-linux
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Gary Guo,
	Björn Roy Baron, Will Deacon, Peter Zijlstra, Mark Rutland,
	Greg Kroah-Hartman, Sergio González Collado, Finn Behrens

> This allows printing the inner data of `Arc` and its friends if the
> inner data implements `Display` or `Debug`. It's useful for logging and
> debugging purpose.
>
> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> Reviwed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
> ---
With this review I will override my previous one, because it 
contains a typo inside the Reviewed-by.

Sorry about that!

Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>

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

* Re: [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc
  2023-02-07 20:45   ` Vincenzo Palazzo
@ 2023-02-07 20:54     ` Boqun Feng
  0 siblings, 0 replies; 17+ messages in thread
From: Boqun Feng @ 2023-02-07 20:54 UTC (permalink / raw)
  To: Vincenzo Palazzo
  Cc: linux-kernel, rust-for-linux, Miguel Ojeda, Alex Gaynor,
	Wedson Almeida Filho, Gary Guo, Björn Roy Baron,
	Will Deacon, Peter Zijlstra, Mark Rutland, Greg Kroah-Hartman,
	Sergio González Collado, Finn Behrens

On Tue, Feb 07, 2023 at 08:45:59PM +0000, Vincenzo Palazzo wrote:
> > This allows printing the inner data of `Arc` and its friends if the
> > inner data implements `Display` or `Debug`. It's useful for logging and
> > debugging purpose.
> >
> > Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> > Reviwed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
> > ---
> With this review I will override my previous one, because it 
> contains a typo inside the Reviewed-by.
> 
> Sorry about that!
> 

No worries.

> Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>

Thanks!

Regards,
Boqun

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

* Re: [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc
  2023-02-07 18:52 ` [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc Boqun Feng
  2023-02-07 19:03   ` Carlos Bilbao
  2023-02-07 20:45   ` Vincenzo Palazzo
@ 2023-02-08  4:57   ` Gary Guo
  2023-02-08 10:00   ` Andreas Hindborg
  2023-02-08 13:43   ` Björn Roy Baron
  4 siblings, 0 replies; 17+ messages in thread
From: Gary Guo @ 2023-02-08  4:57 UTC (permalink / raw)
  To: Boqun Feng
  Cc: linux-kernel, rust-for-linux, Miguel Ojeda, Alex Gaynor,
	Wedson Almeida Filho, Björn Roy Baron, Vincenzo Palazzo,
	Will Deacon, Peter Zijlstra, Mark Rutland, Greg Kroah-Hartman,
	Sergio González Collado, Finn Behrens

On Tue,  7 Feb 2023 10:52:15 -0800
Boqun Feng <boqun.feng@gmail.com> wrote:

> This allows printing the inner data of `Arc` and its friends if the
> inner data implements `Display` or `Debug`. It's useful for logging and
> debugging purpose.
> 
> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> Reviwed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
>  rust/kernel/sync/arc.rs | 25 +++++++++++++++++++++++++
>  1 file changed, 25 insertions(+)
> 
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index 519a6ec43644..e6176d9b5b29 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -22,6 +22,7 @@ use crate::{
>  };
>  use alloc::boxed::Box;
>  use core::{
> +    fmt,
>      marker::{PhantomData, Unsize},
>      mem::{ManuallyDrop, MaybeUninit},
>      ops::{Deref, DerefMut},
> @@ -522,3 +523,27 @@ impl<T: ?Sized> DerefMut for UniqueArc<T> {
>          unsafe { &mut self.inner.ptr.as_mut().data }
>      }
>  }
> +
> +impl<T: fmt::Display + ?Sized> fmt::Display for UniqueArc<T> {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Display::fmt(self.deref(), f)
> +    }
> +}
> +
> +impl<T: fmt::Display + ?Sized> fmt::Display for Arc<T> {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Display::fmt(self.deref(), f)
> +    }
> +}
> +
> +impl<T: fmt::Debug + ?Sized> fmt::Debug for UniqueArc<T> {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Debug::fmt(self.deref(), f)
> +    }
> +}
> +
> +impl<T: fmt::Debug + ?Sized> fmt::Debug for Arc<T> {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Debug::fmt(self.deref(), f)
> +    }
> +}


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

* Re: [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing
  2023-02-07 18:52 ` [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing Boqun Feng
@ 2023-02-08  4:57   ` Gary Guo
  2023-02-08 10:01   ` Andreas Hindborg
  2023-02-08 15:19   ` Miguel Ojeda
  2 siblings, 0 replies; 17+ messages in thread
From: Gary Guo @ 2023-02-08  4:57 UTC (permalink / raw)
  To: Boqun Feng
  Cc: linux-kernel, rust-for-linux, Miguel Ojeda, Alex Gaynor,
	Wedson Almeida Filho, Björn Roy Baron, Vincenzo Palazzo,
	Will Deacon, Peter Zijlstra, Mark Rutland, Greg Kroah-Hartman,
	Sergio González Collado, Finn Behrens

On Tue,  7 Feb 2023 10:52:16 -0800
Boqun Feng <boqun.feng@gmail.com> wrote:

> This both demonstrates the usage of different print format in Rust and
> serves as a selftest for the `Display` and `Debug` implementation of
> `Arc` and its friends.
> 
> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> Reviewed-by: Björn Roy Baron <bjorn3_gh@protonmail.com>
> Reviewed-by: Finn Behrens <fin@nyantec.com>
> Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
>  samples/rust/rust_print.rs | 26 ++++++++++++++++++++++++++
>  1 file changed, 26 insertions(+)
> 
> diff --git a/samples/rust/rust_print.rs b/samples/rust/rust_print.rs
> index 8b39d9cef6d1..165a8d7b1c07 100644
> --- a/samples/rust/rust_print.rs
> +++ b/samples/rust/rust_print.rs
> @@ -15,6 +15,30 @@ module! {
>  
>  struct RustPrint;
>  
> +fn arc_print() -> Result {
> +    use kernel::sync::*;
> +
> +    let a = Arc::try_new(1)?;
> +    let b = UniqueArc::try_new("hello, world")?;
> +
> +    // Prints the value of data in `a`.
> +    pr_info!("{}", a);
> +
> +    // Uses ":?" to print debug fmt of `b`.
> +    pr_info!("{:?}", b);
> +
> +    let a: Arc<&str> = b.into();
> +    let c = a.clone();
> +
> +    // Uses `dbg` to print, will move `c`.
> +    dbg!(c);
> +
> +    // Prints debug fmt with pretty-print "#" and number-in-hex "x".
> +    pr_info!("{:#x?}", a);
> +
> +    Ok(())
> +}
> +
>  impl kernel::Module for RustPrint {
>      fn init(_module: &'static ThisModule) -> Result<Self> {
>          pr_info!("Rust printing macros sample (init)\n");
> @@ -43,6 +67,8 @@ impl kernel::Module for RustPrint {
>          pr_cont!(" is {}", "continued");
>          pr_cont!(" with {}\n", "args");
>  
> +        arc_print()?;
> +
>          Ok(RustPrint)
>      }
>  }


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

* Re: [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc
  2023-02-07 18:52 ` [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc Boqun Feng
                     ` (2 preceding siblings ...)
  2023-02-08  4:57   ` Gary Guo
@ 2023-02-08 10:00   ` Andreas Hindborg
  2023-02-08 13:43   ` Björn Roy Baron
  4 siblings, 0 replies; 17+ messages in thread
From: Andreas Hindborg @ 2023-02-08 10:00 UTC (permalink / raw)
  To: Boqun Feng
  Cc: linux-kernel, rust-for-linux, Miguel Ojeda, Alex Gaynor,
	Wedson Almeida Filho, Gary Guo, Björn Roy Baron,
	Vincenzo Palazzo, Will Deacon, Peter Zijlstra, Mark Rutland,
	Greg Kroah-Hartman, Sergio González Collado, Finn Behrens


Boqun Feng <boqun.feng@gmail.com> writes:

> This allows printing the inner data of `Arc` and its friends if the
> inner data implements `Display` or `Debug`. It's useful for logging and
> debugging purpose.
>
> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> Reviwed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
> ---

Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com>

>  rust/kernel/sync/arc.rs | 25 +++++++++++++++++++++++++
>  1 file changed, 25 insertions(+)
>
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index 519a6ec43644..e6176d9b5b29 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -22,6 +22,7 @@ use crate::{
>  };
>  use alloc::boxed::Box;
>  use core::{
> +    fmt,
>      marker::{PhantomData, Unsize},
>      mem::{ManuallyDrop, MaybeUninit},
>      ops::{Deref, DerefMut},
> @@ -522,3 +523,27 @@ impl<T: ?Sized> DerefMut for UniqueArc<T> {
>          unsafe { &mut self.inner.ptr.as_mut().data }
>      }
>  }
> +
> +impl<T: fmt::Display + ?Sized> fmt::Display for UniqueArc<T> {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Display::fmt(self.deref(), f)
> +    }
> +}
> +
> +impl<T: fmt::Display + ?Sized> fmt::Display for Arc<T> {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Display::fmt(self.deref(), f)
> +    }
> +}
> +
> +impl<T: fmt::Debug + ?Sized> fmt::Debug for UniqueArc<T> {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Debug::fmt(self.deref(), f)
> +    }
> +}
> +
> +impl<T: fmt::Debug + ?Sized> fmt::Debug for Arc<T> {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Debug::fmt(self.deref(), f)
> +    }
> +}


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

* Re: [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing
  2023-02-07 18:52 ` [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing Boqun Feng
  2023-02-08  4:57   ` Gary Guo
@ 2023-02-08 10:01   ` Andreas Hindborg
  2023-02-08 15:19   ` Miguel Ojeda
  2 siblings, 0 replies; 17+ messages in thread
From: Andreas Hindborg @ 2023-02-08 10:01 UTC (permalink / raw)
  To: Boqun Feng
  Cc: linux-kernel, rust-for-linux, Miguel Ojeda, Alex Gaynor,
	Wedson Almeida Filho, Gary Guo, Björn Roy Baron,
	Vincenzo Palazzo, Will Deacon, Peter Zijlstra, Mark Rutland,
	Greg Kroah-Hartman, Sergio González Collado, Finn Behrens


Boqun Feng <boqun.feng@gmail.com> writes:

> This both demonstrates the usage of different print format in Rust and
> serves as a selftest for the `Display` and `Debug` implementation of
> `Arc` and its friends.
>
> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> Reviewed-by: Björn Roy Baron <bjorn3_gh@protonmail.com>
> Reviewed-by: Finn Behrens <fin@nyantec.com>
> Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
> ---

Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com>


>  samples/rust/rust_print.rs | 26 ++++++++++++++++++++++++++
>  1 file changed, 26 insertions(+)
>
> diff --git a/samples/rust/rust_print.rs b/samples/rust/rust_print.rs
> index 8b39d9cef6d1..165a8d7b1c07 100644
> --- a/samples/rust/rust_print.rs
> +++ b/samples/rust/rust_print.rs
> @@ -15,6 +15,30 @@ module! {
>  
>  struct RustPrint;
>  
> +fn arc_print() -> Result {
> +    use kernel::sync::*;
> +
> +    let a = Arc::try_new(1)?;
> +    let b = UniqueArc::try_new("hello, world")?;
> +
> +    // Prints the value of data in `a`.
> +    pr_info!("{}", a);
> +
> +    // Uses ":?" to print debug fmt of `b`.
> +    pr_info!("{:?}", b);
> +
> +    let a: Arc<&str> = b.into();
> +    let c = a.clone();
> +
> +    // Uses `dbg` to print, will move `c`.
> +    dbg!(c);
> +
> +    // Prints debug fmt with pretty-print "#" and number-in-hex "x".
> +    pr_info!("{:#x?}", a);
> +
> +    Ok(())
> +}
> +
>  impl kernel::Module for RustPrint {
>      fn init(_module: &'static ThisModule) -> Result<Self> {
>          pr_info!("Rust printing macros sample (init)\n");
> @@ -43,6 +67,8 @@ impl kernel::Module for RustPrint {
>          pr_cont!(" is {}", "continued");
>          pr_cont!(" with {}\n", "args");
>  
> +        arc_print()?;
> +
>          Ok(RustPrint)
>      }
>  }


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

* Re: [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc
  2023-02-07 18:52 ` [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc Boqun Feng
                     ` (3 preceding siblings ...)
  2023-02-08 10:00   ` Andreas Hindborg
@ 2023-02-08 13:43   ` Björn Roy Baron
  4 siblings, 0 replies; 17+ messages in thread
From: Björn Roy Baron @ 2023-02-08 13:43 UTC (permalink / raw)
  To: Boqun Feng
  Cc: linux-kernel, rust-for-linux, Miguel Ojeda, Alex Gaynor,
	Wedson Almeida Filho, Gary Guo, Vincenzo Palazzo, Will Deacon,
	Peter Zijlstra, Mark Rutland, Greg Kroah-Hartman,
	Sergio González Collado, Finn Behrens

On Tuesday, February 7th, 2023 at 19:52, Boqun Feng <boqun.feng@gmail.com> wrote:

> This allows printing the inner data of `Arc` and its friends if the
> inner data implements `Display` or `Debug`. It's useful for logging and
> debugging purpose.
> 
> Signed-off-by: Boqun Feng boqun.feng@gmail.com
> 
> Reviwed-by: Vincenzo Palazzo vincenzopalazzodev@gmail.com

Reviewed-by: Björn Roy Baron <bjorn3_gh@protonmail.com>

> 
> ---
>  rust/kernel/sync/arc.rs | 25 +++++++++++++++++++++++++
>  1 file changed, 25 insertions(+)
> 
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index 519a6ec43644..e6176d9b5b29 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -22,6 +22,7 @@ use crate::{
>  };
>  use alloc::boxed::Box;
>  use core::{
> +    fmt,
>      marker::{PhantomData, Unsize},
>      mem::{ManuallyDrop, MaybeUninit},
>      ops::{Deref, DerefMut},
> @@ -522,3 +523,27 @@ impl<T: ?Sized> DerefMut for UniqueArc<T> {
>          unsafe { &mut self.inner.ptr.as_mut().data }
>      }
>  }
> +
> +impl<T: fmt::Display + ?Sized> fmt::Display for UniqueArc<T> {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Display::fmt(self.deref(), f)
> +    }
> +}
> +
> +impl<T: fmt::Display + ?Sized> fmt::Display for Arc<T> {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Display::fmt(self.deref(), f)
> +    }
> +}
> +
> +impl<T: fmt::Debug + ?Sized> fmt::Debug for UniqueArc<T> {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Debug::fmt(self.deref(), f)
> +    }
> +}
> +
> +impl<T: fmt::Debug + ?Sized> fmt::Debug for Arc<T> {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Debug::fmt(self.deref(), f)
> +    }
> +}
> --
> 2.39.1

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

* Re: [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing
  2023-02-07 18:52 ` [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing Boqun Feng
  2023-02-08  4:57   ` Gary Guo
  2023-02-08 10:01   ` Andreas Hindborg
@ 2023-02-08 15:19   ` Miguel Ojeda
  2023-02-08 16:33     ` Boqun Feng
  2 siblings, 1 reply; 17+ messages in thread
From: Miguel Ojeda @ 2023-02-08 15:19 UTC (permalink / raw)
  To: Boqun Feng
  Cc: linux-kernel, rust-for-linux, Miguel Ojeda, Alex Gaynor,
	Wedson Almeida Filho, Gary Guo, Björn Roy Baron,
	Vincenzo Palazzo, Will Deacon, Peter Zijlstra, Mark Rutland,
	Greg Kroah-Hartman, Sergio González Collado, Finn Behrens

On Tue, Feb 7, 2023 at 7:52 PM Boqun Feng <boqun.feng@gmail.com> wrote:
>
> +    // Uses `dbg` to print, will move `c`.
> +    dbg!(c);

Perhaps:

    // Uses `dbg` to print, will move `c` (for temporary debugging purposes).
    dbg!(c);

To make it clear it is not meant to be usually committed into the tree.

> +    // Prints debug fmt with pretty-print "#" and number-in-hex "x".
> +    pr_info!("{:#x?}", a);

Apparently, `:#x?` is a bit of an accident: `#` means "alternate"
form, but it turns out it applies to both `x` and `?`, i.e. it is not
that `#` alone implies pretty-printing.

Given the above and that there are improvements under discussion
upstream, perhaps we could avoid giving details for the moment and
just say what it does as a whole, e.g.

    // Pretty-prints the debug formatting with lower-case hexadecimal integers.
    pr_info!("{:#x?}", a);

Some links for those interested:
https://doc.rust-lang.org/std/fmt/index.html#sign0,
https://github.com/rust-lang/rust/issues/75766,
https://github.com/rust-lang/rust/pull/99138#issuecomment-1385331055
and https://github.com/rust-lang/libs-team/issues/165.

Finally, there is a small typo in the commit title. What about:

    rust: samples: print: add sample code for `Arc` printing

I can change these bits on my side if you want & agree with them, to
avoid a v3 just for this.

Thanks for these patches, Boqun!

Cheers,
Miguel

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

* Re: [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing
  2023-02-08 15:19   ` Miguel Ojeda
@ 2023-02-08 16:33     ` Boqun Feng
  2023-02-08 16:56       ` Miguel Ojeda
  0 siblings, 1 reply; 17+ messages in thread
From: Boqun Feng @ 2023-02-08 16:33 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: linux-kernel, rust-for-linux, Miguel Ojeda, Alex Gaynor,
	Wedson Almeida Filho, Gary Guo, Björn Roy Baron,
	Vincenzo Palazzo, Will Deacon, Peter Zijlstra, Mark Rutland,
	Greg Kroah-Hartman, Sergio González Collado, Finn Behrens

On Wed, Feb 08, 2023 at 04:19:04PM +0100, Miguel Ojeda wrote:
> On Tue, Feb 7, 2023 at 7:52 PM Boqun Feng <boqun.feng@gmail.com> wrote:
> >
> > +    // Uses `dbg` to print, will move `c`.
> > +    dbg!(c);
> 
> Perhaps:
> 
>     // Uses `dbg` to print, will move `c` (for temporary debugging purposes).
>     dbg!(c);
> 
> To make it clear it is not meant to be usually committed into the tree.
> 

Thanks!

> > +    // Prints debug fmt with pretty-print "#" and number-in-hex "x".
> > +    pr_info!("{:#x?}", a);
> 
> Apparently, `:#x?` is a bit of an accident: `#` means "alternate"
> form, but it turns out it applies to both `x` and `?`, i.e. it is not
> that `#` alone implies pretty-printing.
> 

Oh, good to know!

> Given the above and that there are improvements under discussion
> upstream, perhaps we could avoid giving details for the moment and
> just say what it does as a whole, e.g.
> 
>     // Pretty-prints the debug formatting with lower-case hexadecimal integers.
>     pr_info!("{:#x?}", a);
> 
> Some links for those interested:
> https://doc.rust-lang.org/std/fmt/index.html#sign0,
> https://github.com/rust-lang/rust/issues/75766,
> https://github.com/rust-lang/rust/pull/99138#issuecomment-1385331055
> and https://github.com/rust-lang/libs-team/issues/165.
> 
> Finally, there is a small typo in the commit title. What about:
> 
>     rust: samples: print: add sample code for `Arc` printing
> 

Hmm.. I'm OK with this change, but it's not a typo ;-)

I deliberately

1)	capitalize the first letter after subsystem tags in the title
	since that's kinda the rule for a few subsystems I usually work
	on, I don't have my own preference, just something I'm used to
	;-)

2)	avoid using "`" in the title to save space because title space
	is precious.

> I can change these bits on my side if you want & agree with them, to
> avoid a v3 just for this.
> 

That'll be great, thanks!

Regards,
Boqun

> Thanks for these patches, Boqun!
> 
> Cheers,
> Miguel

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

* Re: [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing
  2023-02-08 16:33     ` Boqun Feng
@ 2023-02-08 16:56       ` Miguel Ojeda
  2023-02-08 16:58         ` Boqun Feng
  0 siblings, 1 reply; 17+ messages in thread
From: Miguel Ojeda @ 2023-02-08 16:56 UTC (permalink / raw)
  To: Boqun Feng
  Cc: linux-kernel, rust-for-linux, Miguel Ojeda, Alex Gaynor,
	Wedson Almeida Filho, Gary Guo, Björn Roy Baron,
	Vincenzo Palazzo, Will Deacon, Peter Zijlstra, Mark Rutland,
	Greg Kroah-Hartman, Sergio González Collado, Finn Behrens

On Wed, Feb 8, 2023 at 5:34 PM Boqun Feng <boqun.feng@gmail.com> wrote:
>
> Hmm.. I'm OK with this change, but it's not a typo ;-)

By typo I meant the "sampe", not the other changes -- sorry, I should
have been more clear.

> 1)      capitalize the first letter after subsystem tags in the title
>         since that's kinda the rule for a few subsystems I usually work
>         on, I don't have my own preference, just something I'm used to
>         ;-)

Yeah, I don't mind one way or the other (in fact, personally I prefer
uppercase slightly because it is a bit easier to "scan" visually to
see where it starts after the prefixes). The lowercase one is just the
one we have used so far for Rust (which I picked looking at what Linus
et al. usually do).

> 2)      avoid using "`" in the title to save space because title space
>         is precious.

I see, makes sense, thanks!

Cheers,
Miguel

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

* Re: [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing
  2023-02-08 16:56       ` Miguel Ojeda
@ 2023-02-08 16:58         ` Boqun Feng
  0 siblings, 0 replies; 17+ messages in thread
From: Boqun Feng @ 2023-02-08 16:58 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: linux-kernel, rust-for-linux, Miguel Ojeda, Alex Gaynor,
	Wedson Almeida Filho, Gary Guo, Björn Roy Baron,
	Vincenzo Palazzo, Will Deacon, Peter Zijlstra, Mark Rutland,
	Greg Kroah-Hartman, Sergio González Collado, Finn Behrens

On Wed, Feb 08, 2023 at 05:56:43PM +0100, Miguel Ojeda wrote:
> On Wed, Feb 8, 2023 at 5:34 PM Boqun Feng <boqun.feng@gmail.com> wrote:
> >
> > Hmm.. I'm OK with this change, but it's not a typo ;-)
> 
> By typo I meant the "sampe", not the other changes -- sorry, I should
> have been more clear.

Ah, good eyes! Sorry I missed that twice: one when submitting and one
you mention it's a typo ;-(


Regards,
Boqun

> 
> > 1)      capitalize the first letter after subsystem tags in the title
> >         since that's kinda the rule for a few subsystems I usually work
> >         on, I don't have my own preference, just something I'm used to
> >         ;-)
> 
> Yeah, I don't mind one way or the other (in fact, personally I prefer
> uppercase slightly because it is a bit easier to "scan" visually to
> see where it starts after the prefixes). The lowercase one is just the
> one we have used so far for Rust (which I picked looking at what Linus
> et al. usually do).
> 
> > 2)      avoid using "`" in the title to save space because title space
> >         is precious.
> 
> I see, makes sense, thanks!
> 
> Cheers,
> Miguel

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

* Re: [PATCH v2 0/2] rust: sync: Arc: Implement Debug and Display
  2023-02-07 18:52 [PATCH v2 0/2] rust: sync: Arc: Implement Debug and Display Boqun Feng
  2023-02-07 18:52 ` [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc Boqun Feng
  2023-02-07 18:52 ` [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing Boqun Feng
@ 2023-04-10  2:53 ` Miguel Ojeda
  2 siblings, 0 replies; 17+ messages in thread
From: Miguel Ojeda @ 2023-04-10  2:53 UTC (permalink / raw)
  To: Boqun Feng
  Cc: linux-kernel, rust-for-linux, Miguel Ojeda, Alex Gaynor,
	Wedson Almeida Filho, Gary Guo, Björn Roy Baron,
	Vincenzo Palazzo, Will Deacon, Peter Zijlstra, Mark Rutland,
	Greg Kroah-Hartman, Sergio González Collado, Finn Behrens

On Tue, Feb 7, 2023 at 7:52 PM Boqun Feng <boqun.feng@gmail.com> wrote:
>
> I found that our Arc doesn't implement `Debug` or `Display` when I tried
> to play with them, therefore add these implementation.

Applied to `rust-next` (with the few changes mentioned). Thanks!

Cheers,
Miguel

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

end of thread, other threads:[~2023-04-10  2:53 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-07 18:52 [PATCH v2 0/2] rust: sync: Arc: Implement Debug and Display Boqun Feng
2023-02-07 18:52 ` [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc Boqun Feng
2023-02-07 19:03   ` Carlos Bilbao
2023-02-07 20:27     ` Vincenzo Palazzo
2023-02-07 20:45   ` Vincenzo Palazzo
2023-02-07 20:54     ` Boqun Feng
2023-02-08  4:57   ` Gary Guo
2023-02-08 10:00   ` Andreas Hindborg
2023-02-08 13:43   ` Björn Roy Baron
2023-02-07 18:52 ` [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing Boqun Feng
2023-02-08  4:57   ` Gary Guo
2023-02-08 10:01   ` Andreas Hindborg
2023-02-08 15:19   ` Miguel Ojeda
2023-02-08 16:33     ` Boqun Feng
2023-02-08 16:56       ` Miguel Ojeda
2023-02-08 16:58         ` Boqun Feng
2023-04-10  2:53 ` [PATCH v2 0/2] rust: sync: Arc: Implement Debug and Display Miguel Ojeda

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).