Skip to content

Commit

Permalink
Format: format empty arrays as nothing
Browse files Browse the repository at this point in the history
  • Loading branch information
jordens committed Jan 29, 2025
1 parent 406b8b4 commit 713c7b4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 7 additions & 1 deletion defmt/src/export/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,13 @@ pub fn u8_array(a: &[u8]) {

// NOTE: This is passed `&[u8; N]` – it's just coerced to a slice.
pub fn fmt_array<T: Format>(a: &[T]) {
fmt_slice(a)
if let Some((first, remainder)) = a.split_first() {
istr(&first._format_tag());
first._format_data();
for value in remainder {
value._format_data();
}
}
}

/// Implementation detail
Expand Down
10 changes: 9 additions & 1 deletion defmt/tests/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,15 @@ fn slice() {
23u8, // val[0]
42u8, // val[1]
],
);

let val: &[u8] = &[];
check_format!(
val,
[
inc(index, 2), // "{=[?]}"
val.len() as u32, // length
],
)
}

Expand Down Expand Up @@ -737,7 +746,6 @@ fn format_arrays() {
&array,
[
index, // "{=[?;0]}"
inc(index, 1), // "{=u16}"
]
);

Expand Down

0 comments on commit 713c7b4

Please sign in to comment.