-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
172: defmt impl for Vec r=korken89 a=theunkn0wn1 This PR adds support for encoding `heapless::Vec` for usage with `defmt`. `defmt` can already handle slices, so this impl simply invokes that encoder method. I chose to use the slice implementation is I did not perceive the difference between a Vec and a Slice to be of specific importance in a log file, but rather the contents of the vec / slice to be of interest. It also appeared to me as the path of least resistance. I am marking this as a draft as I have yet to be able to validate this against my local hardware, `probe-run` is giving me some grief that I am reasonably sure is unrelated to this change set. Relates to #171 Co-authored-by: joshua salzedo <thHunkn0WNd@gmail.com> Co-authored-by: Joshua Salzedo <joshuasalzedo@gmail.com>
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
//! Defmt implementations for heapless types | ||
//! | ||
use crate::ArrayLength; | ||
use crate::Vec; | ||
use defmt::Formatter; | ||
|
||
impl<T, N> defmt::Format for Vec<T, N> | ||
where | ||
N: ArrayLength<T>, | ||
T: defmt::Format, | ||
{ | ||
fn format(&self, fmt: &mut Formatter) { | ||
fmt.fmt_slice(self) | ||
} | ||
} | ||
|
||
impl<N> defmt::Format for crate::String<N> | ||
where | ||
N: ArrayLength<u8>, | ||
u8: defmt::Format, | ||
{ | ||
fn format(&self, fmt: &mut Formatter) { | ||
fmt.str(self.as_str()); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::convert::TryInto; | ||
|
||
use crate::{consts::*, Vec}; | ||
|
||
#[test] | ||
/// Tests encoding Vec with defmt, | ||
/// based loosely on https://github.com/knurling-rs/defmt/blob/main/tests/encode.rs#L483 | ||
fn test_defmt_format_vec() { | ||
let v: Vec<_, U8> = Vec::from_slice(b"abc").unwrap(); | ||
let index = defmt::export::fetch_string_index(); | ||
|
||
// borrowed from https://github.com/knurling-rs/defmt/blob/main/tests/encode.rs#L49 | ||
let fake_interned = index.wrapping_add(1) & 0x7F; | ||
|
||
let timestamp = defmt::export::fetch_timestamp(); | ||
let mut formatter = defmt::Formatter::new(); | ||
defmt::winfo!(formatter, "{:?}", v); | ||
assert_eq!( | ||
formatter.bytes(), | ||
&[ | ||
index, | ||
timestamp, | ||
v.len().try_into().unwrap(), | ||
fake_interned, // Faked | ||
// Data bytes. | ||
97u8, | ||
98u8, | ||
99u8, | ||
] | ||
); | ||
} | ||
|
||
/// Tests encoding String with defmt, | ||
/// based loosely on https://github.com/knurling-rs/defmt/blob/main/tests/encode.rs#L483 | ||
#[test] | ||
fn test_defmt_format_str() { | ||
let mut v: crate::String<crate::consts::U32> = crate::String::new(); | ||
v.push_str("foo").unwrap(); | ||
let index = defmt::export::fetch_string_index(); | ||
|
||
let timestamp = defmt::export::fetch_timestamp(); | ||
let mut formatter = defmt::Formatter::new(); | ||
defmt::winfo!(formatter, "{:?}", v); | ||
assert_eq!( | ||
formatter.bytes(), | ||
&[ | ||
index, | ||
timestamp, | ||
v.len().try_into().unwrap(), | ||
// Data bytes. | ||
102u8, | ||
111u8, | ||
111u8, | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters