| 1 | use core::fmt; |
| 2 | |
| 3 | use crate as defmt; |
| 4 | use crate::{export, Format, Formatter, Str}; |
| 5 | |
| 6 | /// An "adapter" type to feed `Debug` values into defmt macros, which expect `defmt::Format` values. |
| 7 | /// |
| 8 | /// This adapter disables compression and uses the `core::fmt` code on-device! You should prefer |
| 9 | /// `defmt::Format` over `Debug` whenever possible. |
| 10 | /// |
| 11 | /// # Examples |
| 12 | /// |
| 13 | /// ```rust |
| 14 | /// # #[derive(Debug)] |
| 15 | /// # struct ExpensiveThing(); |
| 16 | /// # let expensive_thing = ExpensiveThing(); |
| 17 | /// # |
| 18 | /// defmt::info!("{:?}" , defmt::Debug2Format(&expensive_thing)); |
| 19 | /// // ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ |
| 20 | /// // must `#[derive(Debug)]` |
| 21 | /// ``` |
| 22 | /// |
| 23 | /// Note that any provided defmt display hints will be ignored |
| 24 | /// because this always uses `{:?}` to format the contained value. |
| 25 | pub struct Debug2Format<'a, T: fmt::Debug + ?Sized>(pub &'a T); |
| 26 | |
| 27 | impl<T: fmt::Debug + ?Sized> fmt::Debug for Debug2Format<'_, T> { |
| 28 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { |
| 29 | self.0.fmt(fmt) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | impl<T: fmt::Debug + ?Sized> Format for Debug2Format<'_, T> { |
| 34 | default_format!(); |
| 35 | |
| 36 | fn _format_tag() -> Str { |
| 37 | defmt_macros::internp!("{=__internal_Debug}" ) |
| 38 | } |
| 39 | |
| 40 | fn _format_data(&self) { |
| 41 | export::debug(&self.0); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// An "adapter" type to feed `Display` values into defmt macros, which expect `defmt::Format` values. |
| 46 | /// |
| 47 | /// This adapter disables compression and uses the `core::fmt` code on-device! You should prefer |
| 48 | /// `defmt::Format` over `Display` whenever possible. |
| 49 | /// |
| 50 | /// # Examples |
| 51 | /// |
| 52 | /// ```rust |
| 53 | /// # struct ExpensiveThing(); |
| 54 | /// # |
| 55 | /// # impl core::fmt::Display for ExpensiveThing { |
| 56 | /// # fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 57 | /// # write!(f, "{}" , "expensive" ) |
| 58 | /// # } |
| 59 | /// # } |
| 60 | /// # let expensive_thing = ExpensiveThing(); |
| 61 | /// # |
| 62 | /// defmt::info!("{}" , defmt::Display2Format(&expensive_thing)); |
| 63 | /// // ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ |
| 64 | /// // must implement `fmt::Display` |
| 65 | /// ``` |
| 66 | /// |
| 67 | /// Note that any provided defmt display hints will be ignored |
| 68 | /// because this always uses `{}` to format the contained value. |
| 69 | pub struct Display2Format<'a, T: fmt::Display + ?Sized>(pub &'a T); |
| 70 | |
| 71 | impl<T: fmt::Display + ?Sized> fmt::Display for Display2Format<'_, T> { |
| 72 | fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { |
| 73 | self.0.fmt(fmt) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl<T: fmt::Display + ?Sized> Format for Display2Format<'_, T> { |
| 78 | default_format!(); |
| 79 | |
| 80 | fn _format_tag() -> Str { |
| 81 | defmt_macros::internp!("{=__internal_Display}" ) |
| 82 | } |
| 83 | |
| 84 | fn _format_data(&self) { |
| 85 | export::display(&self.0); |
| 86 | } |
| 87 | } |
| 88 | |