Skip to content

Commit

Permalink
[rust-embedded#171] extracted new impl to own optional module
Browse files Browse the repository at this point in the history
 - to conform with other optional features
  • Loading branch information
theunkn0wn1 committed Dec 9, 2020
1 parent eaf3455 commit 9c4585d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ufmt-impl = ["ufmt-write"]
x86-sync-pool = []
# only for tests
__trybuild = []
defmt_format = ["defmt"]
defmt-impl = ["defmt"]

[target.x86_64-unknown-linux-gnu.dev-dependencies]
scoped_threadpool = "0.1.8"
Expand Down
47 changes: 47 additions & 0 deletions src/defmt.rs
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,
]);
}
}
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@ pub mod mpmc;
pub mod pool;
#[cfg(has_atomics)]
pub mod spsc;
#[cfg(feature="defmt-impl")]
pub mod defmt;

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

mod sealed;
34 changes: 2 additions & 32 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use core::{fmt, hash, iter::FromIterator, mem::MaybeUninit, ops, ptr, slice};

use generic_array::{ArrayLength, GenericArray};
use hash32;
use defmt::Formatter;

impl<A> crate::i::Vec<A> {
/// `Vec` `const` constructor; wrap the returned value in [`Vec`](../struct.Vec.html)
Expand Down Expand Up @@ -824,19 +823,13 @@ where
}
}

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 crate::{consts::*, Vec};
use as_slice::AsSlice;
use core::fmt::Write;
use std::convert::TryInto;

#[test]
fn static_new() {
Expand Down Expand Up @@ -1161,28 +1154,5 @@ mod tests {
assert!(!v.ends_with(b"a"));
}

#[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,
]);
}

}

0 comments on commit 9c4585d

Please sign in to comment.