rust-bitfield icon indicating copy to clipboard operation
rust-bitfield copied to clipboard

Support `defmt`

Open dzamlo opened this issue 1 year ago • 3 comments

This depends on the level of support we want:

  • We could simply support #[derive(Format)] but we will not get all the fields in the debug output only .0
  • We could port the bitfield_debug to defmt and get all the fields in the output, but this seems like a lot of effort

(ping @brandonros)

dzamlo avatar Jul 28 '23 08:07 dzamlo

Actually, the derive option should already work, but I haven't tested.

dzamlo avatar Jul 28 '23 08:07 dzamlo

Actually, the derive option should already work, but I haven't tested.

It does not seem to work for me, and it should not right?

If I read the macro correct:

macro_rules! bitfield {
    ($(#[$attribute:meta])* pub struct $($rest:tt)*) => { ... };
    ($(#[$attribute:meta])* struct $($rest:tt)*) => { ... };
    ($(#[$attribute:meta])* ($($vis:tt)*) struct $name:ident($($type:tt)*); impl Debug; no default 
BitRange; $($rest:tt)*) => { ... };

   etc etc etc

In that last line the macro matches directly on impl Debug. Honestly I do not get while it compiles at all with impl defmt::Format. Adding a line impl defmt::Format could work as a interim solution.

As a embedded user (where bitfields are rather commen) I would love to get all the fields in the output though.

dvdsk avatar Apr 23 '24 22:04 dvdsk

The correct syntax to auto derive defmt::Format that display .0 should be #[derive(defmt::Format)] above the struct name.

Like so:

bitfield! {
+   #[derive(defmt::Format)]
     struct CtrlMeasurement(u8);
-   impl defmt::Format;
    u8;
    pub from into Oversampling, temperature_os, set_temperature_os: 7, 5;
    pub from into Oversampling, pressure_os, set_pressure_os: 4, 2;
    pub from into SensorMode, mode, set_mode: 1, 0;
}

dzamlo avatar Apr 25 '24 11:04 dzamlo