Skip to content

Commit

Permalink
Use defmt's public interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
theunkn0wn1 committed May 11, 2021
1 parent 75a240d commit a4e1c35
Showing 1 changed file with 7 additions and 29 deletions.
36 changes: 7 additions & 29 deletions src/defmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ where
T: defmt::Format,
{
fn format(&self, fmt: Formatter<'_>) {
fmt.inner.fmt_slice(self.as_slice())
defmt::write!(fmt, "{=[?]}", self.as_slice())
}
}

Expand All @@ -22,7 +22,7 @@ where
u8: defmt::Format,
{
fn format(&self, fmt: Formatter<'_>) {
fmt.inner.str(self.as_str());
defmt::write!(fmt, "{=str}", self.as_str());
}
}

Expand All @@ -41,7 +41,8 @@ mod tests {
}

#[test]
/// Tests encoding Vec with defmt,
/// Tests encoding Vec with defmt, asserting these types may be serialized
/// Note: the exact wire format is NOT checked since its an unstable implementation detail of an external crate.
/// based on https://github.com/knurling-rs/defmt/blob/697a8e807bd766a80ada2d57514a9da1232dbc9a/tests/encode.rs#L523
fn test_defmt_format_vec() {
let val: Vec<_, U8> = Vec::from_slice(b"abc").unwrap();
Expand All @@ -51,21 +52,10 @@ mod tests {
let g = defmt::Formatter{inner: &mut f};
val.format(g);
f.finalize();
assert_eq!(
f.bytes(),
&[
index,
val.len().try_into().unwrap(),
inc(index, 1),
// Data bytes.
97u8,
98u8,
99u8,
]
);
}

/// Tests encoding String with defmt,
/// Tests encoding String with defmt, asserting these types may be serialized
/// Note: the exact wire format is NOT checked since its an unstable implementation detail of an external crate.
/// based loosely on https://github.com/knurling-rs/defmt/blob/main/tests/encode.rs#L483
#[test]
fn test_defmt_format_str() {
Expand All @@ -74,20 +64,8 @@ mod tests {
let index = defmt::export::fetch_string_index();

let mut f = defmt::InternalFormatter::new();
let g = defmt::Formatter{inner: &mut f};
let g = defmt::Formatter { inner: &mut f };
val.format(g);
f.finalize();
assert_eq!(
f.bytes(),
&[
index,
val.len().try_into().unwrap(),
inc(index, 1),
// Data bytes.
102u8,
111u8,
111u8,
]
);
}
}

0 comments on commit a4e1c35

Please sign in to comment.