Skip to content
Closed
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
24 changes: 16 additions & 8 deletions defmt/src/export/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,21 @@ pub fn panic() -> ! {

/// Implementation detail
pub fn fmt<T: Format + ?Sized>(f: &T) {
istr(&T::_format_tag());
istr(&f._format_tag());
f._format_data();
}

/// Implementation detail
pub fn fmt_slice<T: Format>(values: &[T]) {
usize(&values.len());
istr(&T::_format_tag());
for value in values {
value._format_data();
if let Some((first, remainder)) = values.split_first() {
usize(&values.len());
istr(&first._format_tag());
first._format_data();
for value in remainder {
value._format_data();
}
} else {
usize(&0);
}
}

Expand Down Expand Up @@ -180,9 +185,12 @@ pub fn u8_array(a: &[u8]) {

// NOTE: This is passed `&[u8; N]` – it's just coerced to a slice.
pub fn fmt_array<T: Format>(a: &[T]) {
istr(&T::_format_tag());
for value in a {
value._format_data();
if let Some((first, remainder)) = a.split_first() {
istr(&first._format_tag());
first._format_data();
for value in remainder {
value._format_data();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion defmt/src/export/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Format for NoneError {
unreachable!();
}

fn _format_tag() -> Str {
fn _format_tag(&self) -> Str {
defmt_macros::internp!("Unwrap of a None option value")
}

Expand Down
4 changes: 2 additions & 2 deletions defmt/src/impls/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<T: fmt::Debug + ?Sized> fmt::Debug for Debug2Format<'_, T> {
impl<T: fmt::Debug + ?Sized> Format for Debug2Format<'_, T> {
default_format!();

fn _format_tag() -> Str {
fn _format_tag(&self) -> Str {
defmt_macros::internp!("{=__internal_Debug}")
}

Expand Down Expand Up @@ -77,7 +77,7 @@ impl<T: fmt::Display + ?Sized> fmt::Display for Display2Format<'_, T> {
impl<T: fmt::Display + ?Sized> Format for Display2Format<'_, T> {
default_format!();

fn _format_tag() -> Str {
fn _format_tag(&self) -> Str {
defmt_macros::internp!("{=__internal_Display}")
}

Expand Down
2 changes: 1 addition & 1 deletion defmt/src/impls/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ macro_rules! arrays {
default_format!();

#[inline]
fn _format_tag() -> Str {
fn _format_tag(&self) -> Str {
match N {
$(
$len => internp!($fmt),
Expand Down
4 changes: 2 additions & 2 deletions defmt/src/impls/core_/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ where
default_format!();

#[inline]
fn _format_tag() -> Str {
fn _format_tag(&self) -> Str {
internp!("RefCell {{ value: <borrowed> }}|RefCell {{ value: {=?} }}")
}

Expand All @@ -26,7 +26,7 @@ where
Err(_) => export::u8(&0),
Ok(x) => {
export::u8(&1);
export::istr(&T::_format_tag());
export::istr(&x._format_tag());
x._format_data()
}
}
Expand Down
14 changes: 7 additions & 7 deletions defmt/src/impls/core_/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ where
default_format!();

#[inline]
fn _format_tag() -> Str {
fn _format_tag(&self) -> Str {
internp!("None|Some({=?})")
}

Expand All @@ -36,7 +36,7 @@ where
None => export::u8(&0),
Some(x) => {
export::u8(&1);
export::istr(&T::_format_tag());
export::istr(&x._format_tag());
x._format_data()
}
}
Expand All @@ -51,7 +51,7 @@ where
default_format!();

#[inline]
fn _format_tag() -> Str {
fn _format_tag(&self) -> Str {
internp!("Err({=?})|Ok({=?})")
}

Expand All @@ -60,12 +60,12 @@ where
match self {
Err(e) => {
export::u8(&0);
export::istr(&E::_format_tag());
export::istr(&e._format_tag());
e._format_data()
}
Ok(x) => {
export::u8(&1);
export::istr(&T::_format_tag());
export::istr(&x._format_tag());
x._format_data()
}
}
Expand All @@ -76,7 +76,7 @@ impl<T> Format for core::marker::PhantomData<T> {
default_format!();

#[inline]
fn _format_tag() -> Str {
fn _format_tag(&self) -> Str {
internp!("PhantomData")
}

Expand All @@ -88,7 +88,7 @@ impl Format for core::convert::Infallible {
default_format!();

#[inline]
fn _format_tag() -> Str {
fn _format_tag(&self) -> Str {
unreachable!();
}

Expand Down
6 changes: 3 additions & 3 deletions defmt/src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ macro_rules! default_format {
() => {
#[inline]
fn format(&self, _fmt: Formatter) {
crate::export::istr(&Self::_format_tag());
crate::export::istr(&self._format_tag());
self._format_data();
}
};
Expand All @@ -16,8 +16,8 @@ macro_rules! delegate_format {
}

#[inline]
fn _format_tag() -> Str {
<$ty as Format>::_format_tag()
fn _format_tag(&$self_) -> Str {
<$ty as Format>::_format_tag($val)
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions defmt/src/impls/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ macro_rules! prim {
default_format!();

#[inline]
fn _format_tag() -> Str {
fn _format_tag(&self) -> Str {
internp!($fmt)
}

Expand Down Expand Up @@ -46,7 +46,7 @@ where
default_format!();

#[inline]
fn _format_tag() -> Str {
fn _format_tag(&self) -> Str {
internp!("{=[?]}")
}

Expand Down
6 changes: 3 additions & 3 deletions defmt/src/impls/tuples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ impl Format for () {
default_format!();

#[inline]
fn _format_tag() -> Str {
fn _format_tag(&self) -> Str {
internp!("()")
}

Expand All @@ -19,7 +19,7 @@ macro_rules! tuple {
default_format!();

#[inline]
fn _format_tag() -> Str {
fn _format_tag(&self) -> Str {
internp!($format)
}

Expand All @@ -28,7 +28,7 @@ macro_rules! tuple {
fn _format_data(&self) {
let ($(ref $name,)+) = *self;
$(
export::istr(&$name::_format_tag());
export::istr(&$name._format_tag());
$name._format_data();
)+
}
Expand Down
2 changes: 1 addition & 1 deletion defmt/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub trait Format {
fn format(&self, fmt: Formatter);

#[doc(hidden)]
fn _format_tag() -> Str {
fn _format_tag(&self) -> Str {
internp!("{=__internal_FormatSequence}")
}

Expand Down
12 changes: 10 additions & 2 deletions defmt/tests/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn inc(index: u16, n: u16) -> u16 {
}

fn write_format<T: Format + ?Sized>(val: &T) {
defmt::export::istr(&T::_format_tag());
defmt::export::istr(&val._format_tag());
val._format_data();
}

Expand Down Expand Up @@ -557,6 +557,15 @@ fn slice() {
23u8, // val[0]
42u8, // val[1]
],
);

let val: &[u8] = &[];
check_format!(
val,
[
inc(index, 2), // "{=[?]}"
val.len() as u32, // length
],
)
}

Expand Down Expand Up @@ -737,7 +746,6 @@ fn format_arrays() {
&array,
[
index, // "{=[?;0]}"
inc(index, 1), // "{=u16}"
]
);

Expand Down
2 changes: 1 addition & 1 deletion macros/src/derives/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub(crate) fn expand(input: TokenStream) -> TokenStream {
defmt::unreachable!()
}

fn _format_tag() -> defmt::Str {
fn _format_tag(&self) -> defmt::Str {
#format_tag
}

Expand Down
2 changes: 1 addition & 1 deletion macros/src/items/bitflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub(crate) fn expand(input: TokenStream) -> TokenStream {
defmt::unreachable!()
}

fn _format_tag() -> defmt::Str {
fn _format_tag(&self) -> defmt::Str {
#format_tag
}

Expand Down
Loading