Skip to content

Commit

Permalink
Merge #172
Browse files Browse the repository at this point in the history
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
3 people authored Sep 24, 2020
2 parents 9ff3a5f + 883cd43 commit 831333b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ufmt-impl = ["ufmt-write"]
x86-sync-pool = []
# only for tests
__trybuild = []
defmt-impl = ["defmt"]

[target.x86_64-unknown-linux-gnu.dev-dependencies]
scoped_threadpool = "0.1.8"
Expand All @@ -51,3 +52,8 @@ optional = true

[dev-dependencies.ufmt]
version = "0.1"

[dependencies.defmt]
git = "https://github.com/knurling-rs/defmt"
branch = "main"
optional = true
86 changes: 86 additions & 0 deletions src/defmt.rs
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,
]
);
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ pub mod mpmc;
pub mod pool;
#[cfg(has_atomics)]
pub mod spsc;
#[cfg(feature = "defmt-impl")]
mod defmt;

#[cfg(feature = "ufmt-impl")]
mod ufmt;
Expand Down

0 comments on commit 831333b

Please sign in to comment.