Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Display2Format adapter #281

Merged
merged 3 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion firmware/qemu/src/bin/log.out
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,5 @@
0.000093 INFO Some(S { x: -1, y: 2 })
0.000094 INFO [S { x: -1, y: 2 }, S { x: -1, y: 2 }]
0.000095 INFO [Some(S { x: -1, y: 2 }), None]
0.000096 INFO QEMU test finished!
0.000096 INFO 127.0.0.1:8888
0.000097 INFO QEMU test finished!
3 changes: 2 additions & 1 deletion firmware/qemu/src/bin/log.release.out
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,5 @@
0.000091 INFO Some(S { x: -1, y: 2 })
0.000092 INFO [S { x: -1, y: 2 }, S { x: -1, y: 2 }]
0.000093 INFO [Some(S { x: -1, y: 2 }), None]
0.000094 INFO QEMU test finished!
0.000094 INFO 127.0.0.1:8888
0.000095 INFO QEMU test finished!
26 changes: 25 additions & 1 deletion firmware/qemu/src/bin/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use core::sync::atomic::{AtomicU32, Ordering};
use cortex_m_rt::entry;
use cortex_m_semihosting::debug;
use defmt::{consts, Debug2Format, Format, Formatter};
use defmt::{consts, Debug2Format, Display2Format, Format, Formatter};

use defmt_semihosting as _; // global logger

Expand Down Expand Up @@ -458,6 +458,30 @@ fn main() -> ! {
defmt::info!("{:?}", Debug2Format::<consts::U128>(&[Some(s), None]));
}

{
struct SocketAddr {
ip: [u8; 4],
port: u16,
}

impl core::fmt::Display for SocketAddr {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(
f,
"{}.{}.{}.{}:{}",
self.ip[0], self.ip[1], self.ip[2], self.ip[3], self.port
)
}
}

let addr = SocketAddr {
ip: [127, 0, 0, 1],
port: 8888,
};

defmt::info!("{:?}", Display2Format::<consts::U32>(&addr));
}

defmt::info!("QEMU test finished!");

loop {
Expand Down
72 changes: 72 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,3 +681,75 @@ where
value,
}
}

/// An "adapter" type to feed `Display` values into defmt macros, which expect `defmt::Format` values.
///
/// This adapter disables compression! You should prefer `defmt::Format` over `Display` whenever
/// possible.
///
/// This adapter works by formatting the `Display` value into a stack-allocated buffer. You need to
/// specify how large that buffer is. If you pick a size that's too small an *incomplete* string
/// will be transmitted.
///
/// The size of the stack-allocated array is specified using one of the type-level integers in the
/// `consts` module. Example:
///
/// ```
/// use core::fmt;
/// use defmt::{consts, Display2Format};
///
/// struct SocketAddr {
/// ip: [u8; 4],
/// port: u16,
/// }
///
/// impl fmt::Display for SocketAddr {
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
/// write!(f, "{}.{}.{}.{}:{}", self.ip[0], self.ip[1], self.ip[2], self.ip[3], self.port)
/// }
/// }
///
/// let addr = SocketAddr { ip: [127, 0, 0, 1], port: 8888 };
/// defmt::info!("{:?}", Display2Format::<consts::U32>(&addr));
/// // ^^^^^^^^^^^ size = 32 bytes
/// // -> 0.000000 INFO 127.0.0.1:8888
/// ```
pub struct Display2Format<'a, N>
where
N: ArrayLength<u8>,
{
_capacity: PhantomData<N>,

value: &'a dyn fmt::Display,
}

impl<N> Format for Display2Format<'_, N>
where
N: ArrayLength<u8>,
{
fn format(&self, fmt: &mut Formatter) {
use core::fmt::Write as _;

let mut buf = String::<N>::new();
core::write!(buf, "{}", self.value).ok();

// tell defmt we are formatting a `str` value
if fmt.needs_tag() {
let t = impls::str_tag();
fmt.u8(&t);
}
fmt.str(&buf);
}
}

/// `Display2Format` constructor.
#[allow(non_snake_case)]
pub fn Display2Format<'a, N>(value: &'a dyn fmt::Display) -> Display2Format<'a, N>
where
N: ArrayLength<u8>,
{
Display2Format {
_capacity: PhantomData,
value,
}
}