Skip to content

Commit

Permalink
Merge pull request #53 from diondokter/defmt-efficiency
Browse files Browse the repository at this point in the history
Add defmt type hints for fieldsets
  • Loading branch information
diondokter authored Dec 31, 2024
2 parents 1cc99fb + ec6c231 commit 7b722fd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Unreleased

- Make defmt impls for fieldsets a bit more efficient by using type hints

### 1.0.0 (28-12-24)

- Enforce PascalCase for device name
Expand Down
15 changes: 13 additions & 2 deletions generation/src/lir/token_transform/field_set_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,18 @@ pub fn generate_field_set(value: &FieldSet, defmt_feature: Option<&str>) -> Toke
Some(feature_name) => {
let fields_format_string = fields
.iter()
.map(|f| format!("{}: {{}}", f.name))
.map(|f| {
let defmt_type_hint = match f.conversion_method {
FieldConversionMethod::None => {
let base_type = &f.base_type;
format!("={base_type}")
}
FieldConversionMethod::Bool => "=bool".into(),
_ => String::new(),
};

format!("{}: {{{}}}", f.name, defmt_type_hint)
})
.join(", ");

let type_format_string = format!("{} {{{{ {} }}}}", name, fields_format_string);
Expand Down Expand Up @@ -542,7 +553,7 @@ mod tests {
fn format(&self, f: defmt::Formatter) {
defmt::write!(
f,
\"MyRegister {{ my_field: {}, my_field2: {} }}\",
\"MyRegister {{ my_field: {}, my_field2: {=i16} }}\",
self.my_field(),
self.my_field2(),
)
Expand Down

0 comments on commit 7b722fd

Please sign in to comment.