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
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
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
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
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
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)
> }
> }
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)
> }
> }
© 2016 - 2025 Red Hat, Inc.