Skip to contentSkip to conversation

rust: Implement Display for Box

PATCH Superseded Guangbo Cui 2 participants 3 messages Oct 29, 2024 – Nov 4, 2024

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 rust: Implement Display for Box

Currently impl Display is missing for Box<T, A>, as a result, things like using Box<..> directly as an operand in pr_info!() are impossible, which is less ergonomic compared to Box in Rust std.

Therefore add impl Display for Box.

---
Changes in v3:
- Update fmt implementation to clarify that it does not call itself infinitely.
- Link to v2: https://lore.kernel.org/rust-for-linux/[email protected]
Changes in v2:
- Add missing tag: Suggested-by.
- Update the patch title and the changelogs.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]

 rust/kernel/alloc/kbox.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)
rust/kernel/alloc/kbox.rs +10 -0
index d69c32496..a496a866d 100644
@@ -427,6 +427,16 @@ fn deref_mut(&mut self) -> &mut T {
     }
 }
 
+impl<T, A> fmt::Display for Box<T, A>
+where
+    T: ?Sized + fmt::Display,
+    A: Allocator,
+{
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        <T as fmt::Display>::fmt(&**self, f)
+    }
+}
+
 impl<T, A> fmt::Debug for Box<T, A>
 where
     T: ?Sized + fmt::Debug,
Signature · 1 line
2.34.1
maintainer Acked-by on v3 1/1
#1 lore raw
Quoted 27 lines · Guangbo Cui

Currently impl Display is missing for Box<T, A>, as a result, things like using Box<..> directly as an operand in pr_info!() are impossible, which is less ergonomic compared to Box in Rust std.

Therefore add impl Display for Box.

---
Changes in v3:
- Update fmt implementation to clarify that it does not call itself infinitely.
- Link to v2: https://lore.kernel.org/rust-for-linux/[email protected]
Changes in v2:
- Add missing tag: Suggested-by.
- Update the patch title and the changelogs.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]

 rust/kernel/alloc/kbox.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)
rust/kernel/alloc/kbox.rs +0 -0
index d69c32496..a496a866d 100644

rust/kernel/alloc/kbox.rs fn deref_mut(&mut self) -> &mut T { · line 436

Quoted 10 lines
rust/kernel/alloc/kbox.rs +7 -0
@@ -427,6 +427,16 @@ fn deref_mut(&mut self) -> &mut T {
     }
 }
 
+impl<T, A> fmt::Display for Box<T, A>
+where
+    T: ?Sized + fmt::Display,
+    A: Allocator,
+{
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        <T as fmt::Display>::fmt(&**self, f)

Since you went with this variant,

rust/kernel/alloc/kbox.rs fn deref_mut(&mut self) -> &mut T { · line 440

Quoted 4 lines
rust/kernel/alloc/kbox.rs +3 -0
+    }
+}
+
 impl<T, A> fmt::Debug for Box<T, A>

please also change it for Debug in a separate patch, as requested in [1].

With that,

[1] https://lore.kernel.org/all/[email protected]/

Quoted 5 lines
rust/kernel/alloc/kbox.rs +0 -0
 where
     T: ?Sized + fmt::Debug,
Signature · 1 line
2.34.1
#2 lore raw
Quoted 41 lines · Danilo Krummrich
Quoted 27 lines · Guangbo Cui

Currently impl Display is missing for Box<T, A>, as a result, things like using Box<..> directly as an operand in pr_info!() are impossible, which is less ergonomic compared to Box in Rust std.

Therefore add impl Display for Box.

---
Changes in v3:
- Update fmt implementation to clarify that it does not call itself infinitely.
- Link to v2: https://lore.kernel.org/rust-for-linux/[email protected]
Changes in v2:
- Add missing tag: Suggested-by.
- Update the patch title and the changelogs.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]

 rust/kernel/alloc/kbox.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)
rust/kernel/alloc/kbox.rs +0 -0
index d69c32496..a496a866d 100644

rust/kernel/alloc/kbox.rs fn deref_mut(&mut self) -> &mut T { · line 436

Quoted 10 lines
rust/kernel/alloc/kbox.rs +7 -0
@@ -427,6 +427,16 @@ fn deref_mut(&mut self) -> &mut T {
     }
 }
 
+impl<T, A> fmt::Display for Box<T, A>
+where
+    T: ?Sized + fmt::Display,
+    A: Allocator,
+{
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        <T as fmt::Display>::fmt(&**self, f)

Since you went with this variant,

Quoted 6 lines

rust/kernel/alloc/kbox.rs fn deref_mut(&mut self) -> &mut T { · line 440

Quoted 4 lines
rust/kernel/alloc/kbox.rs +3 -0
+    }
+}
+
 impl<T, A> fmt::Debug for Box<T, A>

please also change it for Debug in a separate patch, as requested in [1].

Thanks dakr, I forgot it last week. Fix it in v4.

Quoted 12 lines

With that,

[1] https://lore.kernel.org/all/[email protected]/

Quoted 5 lines
rust/kernel/alloc/kbox.rs +0 -0
 where
     T: ?Sized + fmt::Debug,
Signature · 1 line
2.34.1