Skip to content

wire format: omit the inner type tag empty slices and arrays #929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ We have several packages which live in this repository. Changes are tracked sepa
### [defmt-next]

* [#914] Add cargo-deny as a CI action to check crate security and licensing
* [#929] Change the wire format to omit the inner type tag for empty slices and empty arrays.

### [defmt-v0.3.10] (2024-11-29)

Expand Down
25 changes: 14 additions & 11 deletions decoder/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,21 @@ impl<'t, 'b> Decoder<'t, 'b> {
&mut self,
num_elements: usize,
) -> Result<Vec<FormatSliceElement<'t>>, DecodeError> {
let format = self.get_format()?;
let is_enum = format.contains('|');

let mut elements = Vec::with_capacity(num_elements);
for _i in 0..num_elements {
let format = if is_enum {
self.get_variant(format)?
} else {
format
};
let args = self.decode_format(format)?;
elements.push(FormatSliceElement { format, args });

if num_elements > 0 {
let format = self.get_format()?;
let is_enum = format.contains('|');

for _i in 0..num_elements {
let format = if is_enum {
self.get_variant(format)?
} else {
format
};
let args = self.decode_format(format)?;
elements.push(FormatSliceElement { format, args });
}
}

Ok(elements)
Expand Down
8 changes: 4 additions & 4 deletions defmt/src/export/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,7 @@ pub fn fmt<T: Format + ?Sized>(f: &T) {
/// Implementation detail
pub fn fmt_slice<T: Format>(values: &[T]) {
usize(&values.len());
istr(&T::_format_tag());
for value in values {
value._format_data();
}
fmt_array(values);
}

/// Implementation detail
Expand Down Expand Up @@ -180,6 +177,9 @@ 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]) {
if a.is_empty() {
return;
}
istr(&T::_format_tag());
for value in a {
value._format_data();
Expand Down
12 changes: 10 additions & 2 deletions defmt/tests/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,16 @@ fn slice() {
23u8, // val[0]
42u8, // val[1]
],
)
);

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

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

Expand Down