-
Notifications
You must be signed in to change notification settings - Fork 187
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
defmt impl for Vec #172
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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(); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -94,6 +94,8 @@ mod de; | |
mod ser; | ||
|
||
pub mod binary_heap; | ||
#[cfg(feature = "defmt-impl")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could use |
||
mod defmt; | ||
#[cfg(all(has_cas, feature = "cas"))] | ||
pub mod mpmc; | ||
#[cfg(all(has_cas, feature = "cas"))] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ofdefmt-impl
as the feature flag. I think that's that standard way of doing it (e.g. see smoltcp-rs/smoltcp#455).There was a problem hiding this comment.
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.