Skip to content

Commit

Permalink
Merge #327
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
3 people authored Jan 11, 2021
2 parents 4e8e127 + 2108812 commit ca42c7d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
4 changes: 3 additions & 1 deletion decoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,9 @@ fn format_args_real(
let vals = elements
.iter()
.map(|e| match e.args.as_slice() {
[Arg::Uxx(v)] => u8::try_from(*v).expect("the value must be in u8 range"),
[Arg::Uxx(v)] => {
u8::try_from(*v).expect("the value must be in u8 range")
}
_ => panic!("FormatSlice should only contain one argument"),
})
.collect::<Vec<u8>>();
Expand Down
11 changes: 6 additions & 5 deletions firmware/qemu/src/bin/log.out
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@
0.000103 INFO EnumLarge::A051
0.000104 INFO EnumLarge::A269
0.000105 INFO S { x: "hi" }
0.000106 INFO bitfields 0x97 0b10000100 12 b"42" b"hello"
0.000107 INFO b"Hi"
0.000106 INFO S { x: PhantomData, y: 42 }
0.000107 INFO bitfields 0x97 0b10000100 12 b"42" b"hello"
0.000108 INFO b"Hi"
0.000109 INFO b"Hi"
0.000110 INFO [45054, 49406]
0.000111 INFO [Data { name: b"Hi", value: true }]
0.000112 INFO QEMU test finished!
0.000110 INFO b"Hi"
0.000111 INFO [45054, 49406]
0.000112 INFO [Data { name: b"Hi", value: true }]
0.000113 INFO QEMU test finished!
11 changes: 6 additions & 5 deletions firmware/qemu/src/bin/log.release.out
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@
0.000101 INFO EnumLarge::A051
0.000102 INFO EnumLarge::A269
0.000103 INFO S { x: "hi" }
0.000104 INFO bitfields 0x97 0b10000100 12 b"42" b"hello"
0.000105 INFO b"Hi"
0.000104 INFO S { x: PhantomData, y: 42 }
0.000105 INFO bitfields 0x97 0b10000100 12 b"42" b"hello"
0.000106 INFO b"Hi"
0.000107 INFO b"Hi"
0.000108 INFO [45054, 49406]
0.000109 INFO [Data { name: b"Hi", value: true }]
0.000110 INFO QEMU test finished!
0.000108 INFO b"Hi"
0.000109 INFO [45054, 49406]
0.000110 INFO [Data { name: b"Hi", value: true }]
0.000111 INFO QEMU test finished!
26 changes: 24 additions & 2 deletions firmware/qemu/src/bin/log.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#![no_std]
#![no_main]

use core::sync::atomic::{AtomicU32, Ordering};
use core::{
marker::PhantomData,
sync::atomic::{AtomicU32, Ordering},
};
use cortex_m_rt::entry;
use cortex_m_semihosting::debug;
use defmt::{consts, Debug2Format, Display2Format, Format, Formatter};
Expand Down Expand Up @@ -538,6 +541,22 @@ fn main() -> ! {
defmt::info!("{:?}", S { x: "hi" });
}

{
#[derive(Format)]
struct S {
x: PhantomData<u8>,
y: u8,
}

defmt::info!(
"{:?}",
S {
x: PhantomData,
y: 42
}
);
}

defmt::info!(
"bitfields \
{0=120..128:x} \
Expand All @@ -563,7 +582,10 @@ fn main() -> ! {
value: bool,
}

let data = &[Data { name: b"Hi", value: true }];
let data = &[Data {
name: b"Hi",
value: true,
}];
defmt::info!("{=[?]:a}", *data);
}

Expand Down
9 changes: 9 additions & 0 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,15 @@ impl Format for () {
}
}

impl<T> Format for core::marker::PhantomData<T> {
fn format(&self, f: Formatter) {
if f.inner.needs_tag() {
let t = internp!("PhantomData");
f.inner.u8(&t);
}
}
}

macro_rules! tuple {
( $format:expr, ($($name:ident),+) ) => (
impl<$($name:Format),+> Format for ($($name,)+) where last_type!($($name,)+): ?Sized {
Expand Down

0 comments on commit ca42c7d

Please sign in to comment.