Skip to contentSkip to conversation

rust: Implement Display for Box

PATCH Superseded Guangbo Cui 3 participants 5 messages Nov 4, 2024 – Nov 8, 2024 1 message not in the archive

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v4 1/2 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.

---
 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 Reviewed-by on v4 1/2
#2 lore raw
Quoted 11 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.

#3 lore raw

PATCH v4 2/2 rust: align Debug implementation for Box with Display

In reply to a message not in the archive

Ensure consistency between Debug and Display for Box by updating Debug to match the new Display style.

---
 rust/kernel/alloc/kbox.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/alloc/kbox.rs +1 -1
index a496a866d..49c24727f 100644
@@ -443,7 +443,7 @@ impl<T, A> fmt::Debug for Box<T, A>
     A: Allocator,
 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        fmt::Debug::fmt(&**self, f)
+        <T as fmt::Debug>::fmt(&**self, f)
     }
 }
 
Signature · 1 line
2.34.1
maintainer Acked-by on v4 2/2
#5 lore raw

Re: [PATCH v4 2/2] rust: align Debug implementation for Box with Display

In reply to Guangbo Cui
Quoted 4 lines · Guangbo Cui

Ensure consistency between Debug and Display for Box by updating Debug to match the new Display style.

The ACK was for patch 1 -- this patch I haven't seen before obviously.

However, you can keep it. For both:

Quoted 21 lines
---
 rust/kernel/alloc/kbox.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/alloc/kbox.rs +1 -1
index a496a866d..49c24727f 100644
@@ -443,7 +443,7 @@ impl<T, A> fmt::Debug for Box<T, A>
     A: Allocator,
 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        fmt::Debug::fmt(&**self, f)
+        <T as fmt::Debug>::fmt(&**self, f)
     }
 }
 
Signature · 1 line
2.34.1