Skip to content

Commit ca42c7d

Browse files
bors[bot]mattico
andauthored
Merge #327
327: impl<T> Format for PhantomData<T> r=japaric a=mattico I needed this to be able to `#[derive(Format)]` on a few structs. Co-authored-by: Matt Ickstadt <matt@beckenterprises.com> Co-authored-by: Matt Ickstadt <mattico8@gmail.com>
2 parents 4e8e127 + 2108812 commit ca42c7d

5 files changed

Lines changed: 48 additions & 13 deletions

File tree

decoder/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,9 @@ fn format_args_real(
10411041
let vals = elements
10421042
.iter()
10431043
.map(|e| match e.args.as_slice() {
1044-
[Arg::Uxx(v)] => u8::try_from(*v).expect("the value must be in u8 range"),
1044+
[Arg::Uxx(v)] => {
1045+
u8::try_from(*v).expect("the value must be in u8 range")
1046+
}
10451047
_ => panic!("FormatSlice should only contain one argument"),
10461048
})
10471049
.collect::<Vec<u8>>();

firmware/qemu/src/bin/log.out

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@
104104
0.000103 INFO EnumLarge::A051
105105
0.000104 INFO EnumLarge::A269
106106
0.000105 INFO S { x: "hi" }
107-
0.000106 INFO bitfields 0x97 0b10000100 12 b"42" b"hello"
108-
0.000107 INFO b"Hi"
107+
0.000106 INFO S { x: PhantomData, y: 42 }
108+
0.000107 INFO bitfields 0x97 0b10000100 12 b"42" b"hello"
109109
0.000108 INFO b"Hi"
110110
0.000109 INFO b"Hi"
111-
0.000110 INFO [45054, 49406]
112-
0.000111 INFO [Data { name: b"Hi", value: true }]
113-
0.000112 INFO QEMU test finished!
111+
0.000110 INFO b"Hi"
112+
0.000111 INFO [45054, 49406]
113+
0.000112 INFO [Data { name: b"Hi", value: true }]
114+
0.000113 INFO QEMU test finished!

firmware/qemu/src/bin/log.release.out

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,11 @@
102102
0.000101 INFO EnumLarge::A051
103103
0.000102 INFO EnumLarge::A269
104104
0.000103 INFO S { x: "hi" }
105-
0.000104 INFO bitfields 0x97 0b10000100 12 b"42" b"hello"
106-
0.000105 INFO b"Hi"
105+
0.000104 INFO S { x: PhantomData, y: 42 }
106+
0.000105 INFO bitfields 0x97 0b10000100 12 b"42" b"hello"
107107
0.000106 INFO b"Hi"
108108
0.000107 INFO b"Hi"
109-
0.000108 INFO [45054, 49406]
110-
0.000109 INFO [Data { name: b"Hi", value: true }]
111-
0.000110 INFO QEMU test finished!
109+
0.000108 INFO b"Hi"
110+
0.000109 INFO [45054, 49406]
111+
0.000110 INFO [Data { name: b"Hi", value: true }]
112+
0.000111 INFO QEMU test finished!

firmware/qemu/src/bin/log.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#![no_std]
22
#![no_main]
33

4-
use core::sync::atomic::{AtomicU32, Ordering};
4+
use core::{
5+
marker::PhantomData,
6+
sync::atomic::{AtomicU32, Ordering},
7+
};
58
use cortex_m_rt::entry;
69
use cortex_m_semihosting::debug;
710
use defmt::{consts, Debug2Format, Display2Format, Format, Formatter};
@@ -538,6 +541,22 @@ fn main() -> ! {
538541
defmt::info!("{:?}", S { x: "hi" });
539542
}
540543

544+
{
545+
#[derive(Format)]
546+
struct S {
547+
x: PhantomData<u8>,
548+
y: u8,
549+
}
550+
551+
defmt::info!(
552+
"{:?}",
553+
S {
554+
x: PhantomData,
555+
y: 42
556+
}
557+
);
558+
}
559+
541560
defmt::info!(
542561
"bitfields \
543562
{0=120..128:x} \
@@ -563,7 +582,10 @@ fn main() -> ! {
563582
value: bool,
564583
}
565584

566-
let data = &[Data { name: b"Hi", value: true }];
585+
let data = &[Data {
586+
name: b"Hi",
587+
value: true,
588+
}];
567589
defmt::info!("{=[?]:a}", *data);
568590
}
569591

src/impls.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,15 @@ impl Format for () {
346346
}
347347
}
348348

349+
impl<T> Format for core::marker::PhantomData<T> {
350+
fn format(&self, f: Formatter) {
351+
if f.inner.needs_tag() {
352+
let t = internp!("PhantomData");
353+
f.inner.u8(&t);
354+
}
355+
}
356+
}
357+
349358
macro_rules! tuple {
350359
( $format:expr, ($($name:ident),+) ) => (
351360
impl<$($name:Format),+> Format for ($($name,)+) where last_type!($($name,)+): ?Sized {

0 commit comments

Comments
 (0)