forked from rust-embedded/heapless
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rust-embedded#171] extracted new impl to own optional module
- to conform with other optional features
- Loading branch information
1 parent
eaf3455
commit 9c4585d
Showing
4 changed files
with
53 additions
and
34 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,47 @@ | ||
//! Defmt implementations for heapless types | ||
//! | ||
use defmt::Formatter; | ||
use generic_array::ArrayLength; | ||
|
||
use crate::Vec; | ||
|
||
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) | ||
} | ||
} | ||
|
||
|
||
#[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() { | ||
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, | ||
]); | ||
} | ||
} |
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