Skip to content

Commit

Permalink
Merge pull request #839 from rust-embedded/dbg
Browse files Browse the repository at this point in the history
implement Debug for Field/BitReader + generic Debug for Reg
  • Loading branch information
burrbull authored May 10, 2024
2 parents 6979dc4 + 05bc353 commit 959b10e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

- Yet more clean field & register `Debug`

## [v0.33.2] - 2024-05-07

- Remove unneeded `format_args` in register `Debug` impl
Expand Down
23 changes: 22 additions & 1 deletion src/generate/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub trait RegisterSpec {
/// Raw field type
pub trait FieldSpec: Sized {
/// Raw field type (`u8`, `u16`, `u32`, ...).
type Ux: Copy + PartialEq + From<Self>;
type Ux: Copy + core::fmt::Debug + PartialEq + From<Self>;
}

/// Marker for fields with fixed values
Expand Down Expand Up @@ -258,6 +258,15 @@ impl<REG: Readable + Writable> Reg<REG> {
}
}

impl<REG: Readable> core::fmt::Debug for crate::generic::Reg<REG>
where
R<REG>: core::fmt::Debug
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Debug::fmt(&self.read(), f)
}
}

#[doc(hidden)]
pub mod raw {
use super::{marker, BitM, FieldSpec, RegisterSpec, Unsafe, Writable};
Expand Down Expand Up @@ -433,6 +442,12 @@ impl<FI: FieldSpec> FieldReader<FI> {
}
}

impl<FI: FieldSpec> core::fmt::Debug for FieldReader<FI> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Debug::fmt(&self.bits, f)
}
}

impl<FI> PartialEq<FI> for FieldReader<FI>
where
FI: FieldSpec + Copy,
Expand Down Expand Up @@ -472,6 +487,12 @@ impl<FI> BitReader<FI> {
}
}

impl<FI> core::fmt::Debug for BitReader<FI> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Debug::fmt(&self.bits, f)
}
}

/// Marker for register/field writers which can take any value of specified width
pub struct Safe;
/// You should check that value is allowed to pass to register/field writer marked with this
Expand Down
20 changes: 2 additions & 18 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,6 @@ pub fn render_register_mod(
write!(f, "{}", self.bits())
}
}
#debug_feature
impl core::fmt::Debug for crate::generic::Reg<#regspec_ty> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Debug::fmt(&self.read(), f)
}
}
});
}

Expand Down Expand Up @@ -467,24 +461,22 @@ fn render_register_mod_debug(
Some(a) => a,
None => access,
};
let bit_or_bits = if f.bit_width() > 1 { "bits" } else { "bit" };
let bit_or_bits = syn::Ident::new(bit_or_bits, span);
log::debug!("register={} field={}", name, f.name);
if field_access.can_read() && f.read_action.is_none() {
if let Field::Array(_, de) = &f {
for suffix in de.indexes() {
let f_name_n = field_accessor(&f.name.expand_dim(&suffix), config, span);
let f_name_n_s = format!("{f_name_n}");
r_debug_impl.extend(quote! {
.field(#f_name_n_s, &self.#f_name_n().#bit_or_bits())
.field(#f_name_n_s, &self.#f_name_n())
});
}
} else {
let f_name = f.name.remove_dim();
let f_name = field_accessor(&f_name, config, span);
let f_name_s = format!("{f_name}");
r_debug_impl.extend(quote! {
.field(#f_name_s, &self.#f_name().#bit_or_bits())
.field(#f_name_s, &self.#f_name())
});
}
}
Expand All @@ -494,14 +486,6 @@ fn render_register_mod_debug(
#close
#close
});
r_debug_impl.extend(quote! {
#debug_feature
impl core::fmt::Debug for crate::generic::Reg<#regspec_ty> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Debug::fmt(&self.read(), f)
}
}
});
} else if !access.can_read() || register.read_action.is_some() {
r_debug_impl.extend(quote! {
#debug_feature
Expand Down

0 comments on commit 959b10e

Please sign in to comment.