Skip to contentSkip to conversation

rust: kernel: str: Implement Debug for CString

PATCH Applied Asahi Lina 7 participants 11 messages Jul 14, 2023 – Dec 13, 2023 Rust

lore
Share & tools
lore

Asahi Lina

author
#0 lore raw

PATCH 1/1 rust: kernel: str: Implement Debug for CString

Trivial implementation.

---
 rust/kernel/str.rs | 6 ++++++
 1 file changed, 6 insertions(+)
rust/kernel/str.rs +6 -0
index c9dd3bf59e34..a94e396d39e1 100644
@@ -606,6 +606,12 @@ fn try_from(cstr: &'a CStr) -> Result<CString, AllocError> {
     }
 }
 
+impl fmt::Debug for CString {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt::Debug::fmt(&**self, f)
+    }
+}
+
 /// A convenience alias for [`core::format_args`].
 #[macro_export]
 macro_rules! fmt {

---
base-commit: 06c2afb862f9da8dc5efa4b6076a0e48c3fbaaa5
change-id: 20230714-cstring-debug-ca021fe0ad78

Thank you,
~~ Lina

author
#2 lore raw
Quoted 8 lines · Alice Ryhl
Quoted 3 lines · Asahi Lina

Trivial implementation.

The commit message is a bit short, but the change itself looks fine.

It's so trivial I just didn't know what else to write... suggestions welcome (for this or next time I have a patch like this) ^^

~~ Lina

#3 lore raw
Quoted 6 lines · Asahi Lina
Quoted 5 lines · Alice Ryhl
Quoted 3 lines · Asahi Lina

Trivial implementation.

Quoted 9 lines
Quoted 3 lines

The commit message is a bit short, but the change itself looks fine.

It's so trivial I just didn't know what else to write... suggestions welcome (for this or next time I have a patch like this) ^^

~~ Lina

Just describe what it does, like "cast the CStr to an &str and call fmt::Debug::fmt with it". I'll add my Reviewed-by, so retain it in case it's asked to change the commit message.

#4 lore raw
In reply to Asahi Lina
Quoted 30 lines · Asahi Lina

Trivial implementation.

---
 rust/kernel/str.rs | 6 ++++++
 1 file changed, 6 insertions(+)
rust/kernel/str.rs +6 -1
index c9dd3bf59e34..a94e396d39e1 100644
@@ -606,6 +606,12 @@ fn try_from(cstr: &'a CStr) -> Result<CString, AllocError> {
     }
 }
 
+impl fmt::Debug for CString {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt::Debug::fmt(&**self, f)
+    }
+}
+
 /// A convenience alias for [`core::format_args`].
 #[macro_export]
 macro_rules! fmt {
 
---

base-commit: 06c2afb862f9da8dc5efa4b6076a0e48c3fbaaa5
change-id: 20230714-cstring-debug-ca021fe0ad78

Quoted 3 lines

Thank you,
~~ Lina

Glad I wasn't the only one missing this, now I don't have to write the awkard &* anymore, as in:

```
pr_debug!("trying to open {:?}\n", &*filename);
```

Cheers,
Ariel

reviewer
#5 lore raw
Quoted 6 lines · Asahi Lina
Quoted 5 lines · Alice Ryhl
Quoted 3 lines · Asahi Lina

Trivial implementation.

Quoted 8 lines
Quoted 3 lines

The commit message is a bit short, but the change itself looks fine.

It's so trivial I just didn't know what else to write... suggestions welcome (for this or next time I have a patch like this) ^^

~ Lina

Adding some sort of motivation usually works quite well, e.g.:

Make it possible to use a CString with the pr_* macros directly, that is, instead of

	pr_debug!("trying to open {:?}\n", &*filename);

we can now write

	pr_debug!("trying to open {:?}\n", filename);

Alice

#6 lore raw
Quoted 11 lines · Alice Ryhl

Adding some sort of motivation usually works quite well, e.g.:

Make it possible to use a CString with the pr_* macros directly, that is, instead of

        pr_debug!("trying to open {:?}\n", &*filename);

we can now write

        pr_debug!("trying to open {:?}\n", filename);

Indeed, this would be the most important bit, i.e. answering the "why?".

The "what?" and the "how?" are pretty much explained by the title, but it is also fine giving more details (but if the implementation requires an explanation, then it is usually best to write an actual source code comment instead).

Cheers,
Miguel

#7 lore raw
Quoted 15 lines · Miguel Ojeda
Quoted 11 lines · Alice Ryhl

Adding some sort of motivation usually works quite well, e.g.:

Make it possible to use a CString with the pr_* macros directly, that is, instead of

        pr_debug!("trying to open {:?}\n", &*filename);

we can now write

        pr_debug!("trying to open {:?}\n", filename);

Indeed, this would be the most important bit, i.e. answering the "why?".

Quoted 7 lines

The "what?" and the "how?" are pretty much explained by the title, but it is also fine giving more details (but if the implementation requires an explanation, then it is usually best to write an actual source code comment instead).

Cheers,
Miguel

Any follow-up on this? It sure would make my logging cleaner.

Cheers,
Ariel

#8 lore raw
Quoted 2 lines · Ariel Miculas (amiculas)

Any follow-up on this? It sure would make my logging cleaner.

We were expecting a new version, but I can pick it up with e.g. the message that Alice suggested (i.e. marking it as modified by me).

Cheers,
Miguel

Reviewed-by on v1 1/1
#9 lore raw
In reply to Asahi Lina
Quoted 3 lines · Asahi Lina

Trivial implementation.

With a better commit message you can add

Signature · 2 lines
Cheers,
Benno
Quoted 29 lines
---
  rust/kernel/str.rs | 6 ++++++
  1 file changed, 6 insertions(+)
rust/kernel/str.rs +6 -1
index c9dd3bf59e34..a94e396d39e1 100644
@@ -606,6 +606,12 @@ fn try_from(cstr: &'a CStr) -> Result<CString, AllocError> {
      }
  }
 
+impl fmt::Debug for CString {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt::Debug::fmt(&**self, f)
+    }
+}
+
  /// A convenience alias for [`core::format_args`].
  #[macro_export]
  macro_rules! fmt {
 
---

base-commit: 06c2afb862f9da8dc5efa4b6076a0e48c3fbaaa5
change-id: 20230714-cstring-debug-ca021fe0ad78

Thank you,
~~ Lina