rust: sync: Arc: Implement Debug and Display
v2 cover rust: sync: Arc: Implement Debug and Display
previous version:
v1: https://lore.kernel.org/rust-for-linux/[email protected]/
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
Signature · 1 line
2.39.1
PATCH v2 1/2 rust: sync: impl {Debug,Display} for {Unique,}Arc
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.
--- rust/kernel/sync/arc.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
rust/kernel/sync/arc.rs +25 -0
@@ -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) + } +}
Signature · 1 line
2.39.1
Re: [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc
Quoted 6 lines · Boqun Feng
This allows printing the inner data of
Arcand its friends if the inner data implementsDisplayorDebug. It's useful for logging and debugging purpose.Signed-off-by Boqun Feng <[email protected]>Reviwed-by Vincenzo Palazzo <[email protected]>
s/Reviwed/Reviewed
Quoted 34 lines
--- rust/kernel/sync/arc.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)rust/kernel/sync/arc.rs +15
-0@@ -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 > {
rust/kernel/sync/arc.rs impl<T: ?Sized> DerefMut for UniqueArc<T> { · line 549
Quoted 10 lines
rust/kernel/sync/arc.rs +10
-0+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
Re: [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc
Quoted 11 lines · Carlos Bilbao
Quoted 6 lines · Boqun Feng
This allows printing the inner data of
Arcand its friends if the inner data implementsDisplayorDebug. It's useful for logging and debugging purpose.Signed-off-by Boqun Feng <[email protected]>Reviwed-by Vincenzo Palazzo <[email protected]>s/Reviwed/Reviewed
Ops! this is my fautl! I will review the version 2.
Re: [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc
In reply to Boqun FengQuoted 7 lines
This allows printing the inner data of
Arcand its friends if the inner data implementsDisplayorDebug. It's useful for logging and debugging purpose.Signed-off-by Boqun Feng <[email protected]>Reviwed-by Vincenzo Palazzo <[email protected]>---
With this review I will override my previous one, because it contains a typo inside the Reviewed-by.
Sorry about that!
Re: [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc
Quoted 4 lines · Vincenzo Palazzo
Quoted 4 lines
This allows printing the inner data of
Arcand its friends if the inner data implementsDisplayorDebug. It's useful for logging and debugging purpose.
Quoted 8 lines
Quoted 3 lines
Signed-off-by Boqun Feng <[email protected]>Reviwed-by Vincenzo Palazzo <[email protected]>---
With this review I will override my previous one, because it contains a typo inside the Reviewed-by.
Sorry about that!
No worries.
Quoted 1 line
Reviewed-by Vincenzo Palazzo <[email protected]>
Thanks!
Regards,
Boqun
Re: [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc
In reply to Boqun FengQuoted 6 lines · On Tue, 7 Feb 2023 10:52:15 -0800 Boqun Feng <[email protected]> wrote:
This allows printing the inner data of
Arcand its friends if the inner data implementsDisplayorDebug. It's useful for logging and debugging purpose.Signed-off-by Boqun Feng <[email protected]>Reviwed-by Vincenzo Palazzo <[email protected]>
Quoted 44 lines
--- rust/kernel/sync/arc.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)rust/kernel/sync/arc.rs +25
-0@@ -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) + } +}
Re: [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc
In reply to Boqun FengQuoted 7 lines · Boqun Feng
This allows printing the inner data of
Arcand its friends if the inner data implementsDisplayorDebug. It's useful for logging and debugging purpose.Signed-off-by Boqun Feng <[email protected]>Reviwed-by Vincenzo Palazzo <[email protected]>---
Quoted 43 lines
rust/kernel/sync/arc.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)rust/kernel/sync/arc.rs +25
-0@@ -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) + } +}
Re: [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc
In reply to Boqun FengQuoted 7 lines · Boqun Feng
This allows printing the inner data of
Arcand its friends if the inner data implementsDisplayorDebug. It's useful for logging and debugging purpose.Signed-off-by Boqun Feng [email protected]Reviwed-by Vincenzo Palazzo [email protected]
Quoted 47 lines
--- rust/kernel/sync/arc.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)rust/kernel/sync/arc.rs +25
-1@@ -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
PATCH v2 2/2 sample: rust: print: Add sampe code for Arc printing
In reply to Boqun FengThis 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.
--- samples/rust/rust_print.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
samples/rust/rust_print.rs +26 -0
@@ -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: &'staticThisModule ) ->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 ) } }
Signature · 1 line
2.39.1
Re: [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing
Quoted 8 lines · On Tue, 7 Feb 2023 10:52:16 -0800 Boqun Feng <[email protected]> wrote:
This both demonstrates the usage of different print format in Rust and serves as a selftest for the
DisplayandDebugimplementation ofArcand its friends.Signed-off-by Boqun Feng <[email protected]>Reviewed-by Björn Roy Baron <[email protected]>Reviewed-by Finn Behrens <[email protected]>Reviewed-by Vincenzo Palazzo <[email protected]>
Quoted 48 lines
--- samples/rust/rust_print.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)samples/rust/rust_print.rs +26
-0@@ -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: &'staticThisModule ) ->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 ) } }
Re: [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing
In reply to Boqun FengQuoted 9 lines · Boqun Feng
This both demonstrates the usage of different print format in Rust and serves as a selftest for the
DisplayandDebugimplementation ofArcand its friends.Signed-off-by Boqun Feng <[email protected]>Reviewed-by Björn Roy Baron <[email protected]>Reviewed-by Finn Behrens <[email protected]>Reviewed-by Vincenzo Palazzo <[email protected]>---
Quoted 47 lines
samples/rust/rust_print.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)samples/rust/rust_print.rs +26
-0@@ -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: &'staticThisModule ) ->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 ) } }
Re: [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing
In reply to Boqun Fengsamples/rust/rust_print.rs module! { · line 34
Quoted 3 lines · Boqun Feng
samples/rust/rust_print.rs +2
-0+// 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.
samples/rust/rust_print.rs module! { · line 37
Quoted 2 lines
samples/rust/rust_print.rs +2
-0+// 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
Re: [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing
Quoted 5 lines · Miguel Ojeda
samples/rust/rust_print.rs module! { · line 34
Quoted 3 lines · Boqun Feng
samples/rust/rust_print.rs +2
-0+// Uses `dbg` to print, will move `c`. +dbg! (c);
Quoted 7 lines
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!
Quoted 7 lines
samples/rust/rust_print.rs module! { · line 37
Quoted 2 lines
samples/rust/rust_print.rs +2
-0+// 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 bothxand?, i.e. it is not that#alone implies pretty-printing.
Oh, good to know!
Quoted 13 lines
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.
Quoted 4 lines
Finally, there is a small typo in the commit title. What about:
rust: samples: print: add sample code for
Arcprinting
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.
Quoted 3 lines
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
Quoted 4 lines
Thanks for these patches, Boqun!
Cheers,
Miguel
Re: [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing
Quoted 2 lines · Boqun Feng
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.
Quoted 4 lines
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).
Quoted 2 lines
2) avoid using "`" in the title to save space because title space
is precious.
I see, makes sense, thanks!
Cheers,
Miguel
Re: [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing
Quoted 6 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
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
Quoted 19 lines
Quoted 5 lines
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).
Quoted 2 lines
2) avoid using "`" in the title to save space because title space
is precious.I see, makes sense, thanks!
Cheers,
Miguel
Miguel Ojeda
AppliedQuoted 3 lines · Boqun Feng
I found that our Arc doesn't implement
DebugorDisplaywhen I tried to play with them, therefore add these implementation.
Applied to rust-next (with the few changes mentioned). Thanks!
Cheers,
Miguel