defmt_or_log/
traits.rs

1#[cfg(not(any(feature = "defmt", feature = "log")))]
2/// A trait that describes a type which can be logged with defmt or log, depending on the current configuration.
3pub trait FormatOrDebug {}
4
5#[cfg(feature = "defmt")]
6/// A trait that describes a type which can be logged with defmt or log, depending on the current configuration.
7pub trait FormatOrDebug: defmt::Format {}
8
9#[cfg(feature = "defmt")]
10impl<T> FormatOrDebug for T where T: defmt::Format {}
11
12#[cfg(feature = "log")]
13/// A trait that describes a type which can be logged with defmt or log, depending on the current configuration.
14pub trait FormatOrDebug: core::fmt::Debug {}
15
16#[cfg(feature = "log")]
17impl<T> FormatOrDebug for T where T: core::fmt::Debug {}
18
19/// Copy of defmt::Debug2Format, except it behaves transparent if log is enabled
20pub struct Debug2Format<'a, T: core::fmt::Debug + ?Sized>(pub &'a T);
21
22#[cfg(not(feature = "defmt"))]
23impl<T: core::fmt::Debug + ?Sized> core::fmt::Debug for Debug2Format<'_, T> {
24    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
25        self.0.fmt(f)
26    }
27}
28
29#[cfg(feature = "defmt")]
30impl<T: core::fmt::Debug + ?Sized> defmt::Format for Debug2Format<'_, T> {
31    fn format(&self, f: defmt::Formatter<'_>) {
32        defmt::Debug2Format(self.0).format(f)
33    }
34}
35
36/// Copy of defmt::Display2Format, except it behaves transparent if log is enabled
37pub struct Display2Format<'a, T: core::fmt::Display + ?Sized>(pub &'a T);
38
39#[cfg(not(feature = "defmt"))]
40impl<T: core::fmt::Display + ?Sized> core::fmt::Display for Display2Format<'_, T> {
41    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
42        self.0.fmt(f)
43    }
44}
45
46#[cfg(feature = "defmt")]
47impl<T: core::fmt::Display + ?Sized> defmt::Format for Display2Format<'_, T> {
48    fn format(&self, f: defmt::Formatter<'_>) {
49        defmt::Display2Format(self.0).format(f)
50    }
51}