Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defmt impl for Vec #172

Merged
merged 2 commits into from
May 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ x86-sync-pool = []
__trybuild = []
# Enable larger MPMC sizes.
mpmc_large = []
defmt-impl = ["defmt"]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could remove this line and use defmt instead of defmt-impl as the feature flag. I think that's that standard way of doing it (e.g. see smoltcp-rs/smoltcp#455).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the template provided by ufmt-impl on line 20, if we change this PR we should also change the other in a follow up.


[target.x86_64-unknown-linux-gnu.dev-dependencies]
scoped_threadpool = "0.1.8"
Expand All @@ -49,3 +50,12 @@ optional = true

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

[dependencies.defmt]
version = "0.2.1"
optional = true

[dev-dependencies.defmt]
version = "0.2.1"
features = ["unstable-test"]

56 changes: 56 additions & 0 deletions src/defmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! Defmt implementations for heapless types
//!

use crate::Vec;
use defmt::Formatter;

impl<T, const N: usize> defmt::Format for Vec<T, N>
where
T: defmt::Format,
{
fn format(&self, fmt: Formatter<'_>) {
defmt::write!(fmt, "{=[?]}", self.as_slice())
}
}

impl<const N: usize> defmt::Format for crate::String<N>
where
u8: defmt::Format,
{
fn format(&self, fmt: Formatter<'_>) {
defmt::write!(fmt, "{=str}", self.as_str());
}
}

#[cfg(test)]
mod tests {
use crate::Vec;
use defmt::Format;

#[test]
/// 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<_, 8> = Vec::from_slice(b"abc").unwrap();

let mut f = defmt::InternalFormatter::new();
let g = defmt::Formatter { inner: &mut f };
val.format(g);
f.finalize();
}

/// 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() {
let mut val: crate::String<32> = crate::String::new();
val.push_str("foo").unwrap();

let mut f = defmt::InternalFormatter::new();
let g = defmt::Formatter { inner: &mut f };
val.format(g);
f.finalize();
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ mod de;
mod ser;

pub mod binary_heap;
#[cfg(feature = "defmt-impl")]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could use defmt instead of defmt-impl here (see my other comment).

mod defmt;
#[cfg(all(has_cas, feature = "cas"))]
pub mod mpmc;
#[cfg(all(has_cas, feature = "cas"))]
Expand Down