From b3e19c15a10964cca199f19794960f515bd0f403 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Mon, 5 Jun 2023 17:14:04 +0300 Subject: [PATCH 1/4] revert #465 --- .github/workflows/ci.yml | 2 +- ci/script.sh | 5 +-- src/generate/generic.rs | 72 ++++++++++++++++------------------ src/generate/generic_atomic.rs | 18 ++++----- src/generate/register.rs | 71 +++------------------------------ src/main.rs | 6 --- src/util.rs | 3 -- 7 files changed, 49 insertions(+), 128 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d47815bc..9d46001d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -75,7 +75,7 @@ jobs: - { rust: stable, vendor: Spansion, options: "--atomics" } - { rust: stable, vendor: STMicro, options: "" } - { rust: stable, vendor: STMicro, options: "--atomics" } - - { rust: stable, vendor: STM32-patched, options: "--strict --const_generic --derive_more --pascal_enum_values --max_cluster_size --atomics" } + - { rust: stable, vendor: STM32-patched, options: "--strict --const_generic --pascal_enum_values --max_cluster_size --atomics" } - { rust: stable, vendor: Toshiba, options: all } - { rust: stable, vendor: Toshiba, options: "" } # Test MSRV diff --git a/ci/script.sh b/ci/script.sh index b5e446f2..b09e9fa1 100755 --- a/ci/script.sh +++ b/ci/script.sh @@ -31,7 +31,7 @@ main() { case $OPTIONS in all) - options="--const_generic --strict --derive_more --atomics" + options="--const_generic --strict --atomics" ;; *) options=$OPTIONS @@ -43,9 +43,6 @@ main() { echo 'cortex-m = "0.7.4"' >> $td/Cargo.toml echo 'cortex-m-rt = "0.7.1"' >> $td/Cargo.toml echo 'vcell = "0.1.3"' >> $td/Cargo.toml - if [[ "$options" == *"--derive_more"* ]]; then - echo 'derive_more = "0.99"' >> $td/Cargo.toml - fi if [[ "$options" == *"--atomics"* ]]; then echo 'portable-atomic = { version = "0.3.16", default-features = false }' >> $td/Cargo.toml fi diff --git a/src/generate/generic.rs b/src/generate/generic.rs index fd99d5c9..c329da09 100644 --- a/src/generate/generic.rs +++ b/src/generate/generic.rs @@ -59,10 +59,7 @@ pub trait FieldSpec: Sized { /// Trait implemented by readable registers to enable the `read` method. /// /// Registers marked with `Writable` can be also be `modify`'ed. -pub trait Readable: RegisterSpec { - /// Result from a call to `read` and argument to `modify`. - type Reader: From> + core::ops::Deref>; -} +pub trait Readable: RegisterSpec {} /// Trait implemented by writeable registers. /// @@ -70,9 +67,6 @@ pub trait Readable: RegisterSpec { /// /// Registers marked with `Readable` can be also be `modify`'ed. pub trait Writable: RegisterSpec { - /// Writer type argument to `write`, et al. - type Writer: From> + core::ops::DerefMut>; - /// Specifies the register bits that are not changed if you pass `1` and are changed if you pass `0` const ZERO_TO_MODIFY_FIELDS_BITMAP: Self::Ux; @@ -130,11 +124,11 @@ impl Reg { /// let flag = reader.field2().bit_is_set(); /// ``` #[inline(always)] - pub fn read(&self) -> REG::Reader { - REG::Reader::from(R { + pub fn read(&self) -> R { + R { bits: self.register.get(), _reg: marker::PhantomData, - }) + } } } @@ -173,14 +167,14 @@ impl Reg { #[inline(always)] pub fn write(&self, f: F) where - F: FnOnce(&mut REG::Writer) -> &mut W, + F: FnOnce(&mut W) -> &mut W, { self.register.set( - f(&mut REG::Writer::from(W { + f(&mut W { bits: REG::RESET_VALUE & !REG::ONE_TO_MODIFY_FIELDS_BITMAP | REG::ZERO_TO_MODIFY_FIELDS_BITMAP, _reg: marker::PhantomData, - })) + }) .bits, ); } @@ -197,13 +191,13 @@ impl Reg { #[inline(always)] pub unsafe fn write_with_zero(&self, f: F) where - F: FnOnce(&mut REG::Writer) -> &mut W, + F: FnOnce(&mut W) -> &mut W, { self.register.set( - f(&mut REG::Writer::from(W { + f(&mut W { bits: REG::Ux::default(), _reg: marker::PhantomData, - })) + }) .bits, ); } @@ -238,20 +232,20 @@ impl Reg { #[inline(always)] pub fn modify(&self, f: F) where - for<'w> F: FnOnce(®::Reader, &'w mut REG::Writer) -> &'w mut W, + for<'w> F: FnOnce(&R, &'w mut W) -> &'w mut W, { let bits = self.register.get(); self.register.set( f( - ®::Reader::from(R { + &R { bits, _reg: marker::PhantomData, - }), - &mut REG::Writer::from(W { + }, + &mut W { bits: bits & !REG::ONE_TO_MODIFY_FIELDS_BITMAP | REG::ZERO_TO_MODIFY_FIELDS_BITMAP, _reg: marker::PhantomData, - }), + }, ) .bits, ); @@ -414,7 +408,7 @@ where REG: Writable + RegisterSpec, FI: FieldSpec, { - pub(crate) w: &'a mut REG::Writer, + pub(crate) w: &'a mut W, _field: marker::PhantomData<(FI, Safety)>, } @@ -427,7 +421,7 @@ where /// Creates a new instance of the writer #[allow(unused)] #[inline(always)] - pub(crate) fn new(w: &'a mut REG::Writer) -> Self { + pub(crate) fn new(w: &'a mut W) -> Self { Self { w, _field: marker::PhantomData, @@ -441,7 +435,7 @@ where REG: Writable + RegisterSpec, bool: From, { - pub(crate) w: &'a mut REG::Writer, + pub(crate) w: &'a mut W, _field: marker::PhantomData<(FI, M)>, } @@ -453,7 +447,7 @@ where /// Creates a new instance of the writer #[allow(unused)] #[inline(always)] - pub(crate) fn new(w: &'a mut REG::Writer) -> Self { + pub(crate) fn new(w: &'a mut W) -> Self { Self { w, _field: marker::PhantomData, @@ -514,14 +508,14 @@ macro_rules! impl_bit_proxy { { /// Writes bit to the field #[inline(always)] - pub fn bit(self, value: bool) -> &'a mut REG::Writer { + pub fn bit(self, value: bool) -> &'a mut W { self.w.bits &= !(REG::Ux::one() << OF); self.w.bits |= (REG::Ux::from(value) & REG::Ux::one()) << OF; self.w } /// Writes `variant` to the field #[inline(always)] - pub fn variant(self, variant: FI) -> &'a mut REG::Writer { + pub fn variant(self, variant: FI) -> &'a mut W { self.bit(bool::from(variant)) } } @@ -548,14 +542,14 @@ where /// /// Passing incorrect value can cause undefined behaviour. See reference manual #[inline(always)] - pub unsafe fn bits(self, value: FI::Ux) -> &'a mut REG::Writer { + pub unsafe fn bits(self, value: FI::Ux) -> &'a mut W { self.w.bits &= !(REG::Ux::mask::() << OF); self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::()) << OF; self.w } /// Writes `variant` to the field #[inline(always)] - pub fn variant(self, variant: FI) -> &'a mut REG::Writer { + pub fn variant(self, variant: FI) -> &'a mut W { unsafe { self.bits(FI::Ux::from(variant)) } } } @@ -567,14 +561,14 @@ where { /// Writes raw bits to the field #[inline(always)] - pub fn bits(self, value: FI::Ux) -> &'a mut REG::Writer { + pub fn bits(self, value: FI::Ux) -> &'a mut W { self.w.bits &= !(REG::Ux::mask::() << OF); self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::()) << OF; self.w } /// Writes `variant` to the field #[inline(always)] - pub fn variant(self, variant: FI) -> &'a mut REG::Writer { + pub fn variant(self, variant: FI) -> &'a mut W { self.bits(FI::Ux::from(variant)) } } @@ -594,13 +588,13 @@ where { /// Sets the field bit #[inline(always)] - pub fn set_bit(self) -> &'a mut REG::Writer { + pub fn set_bit(self) -> &'a mut W { self.w.bits |= REG::Ux::one() << OF; self.w } /// Clears the field bit #[inline(always)] - pub fn clear_bit(self) -> &'a mut REG::Writer { + pub fn clear_bit(self) -> &'a mut W { self.w.bits &= !(REG::Ux::one() << OF); self.w } @@ -613,7 +607,7 @@ where { /// Sets the field bit #[inline(always)] - pub fn set_bit(self) -> &'a mut REG::Writer { + pub fn set_bit(self) -> &'a mut W { self.w.bits |= REG::Ux::one() << OF; self.w } @@ -626,7 +620,7 @@ where { /// Clears the field bit #[inline(always)] - pub fn clear_bit(self) -> &'a mut REG::Writer { + pub fn clear_bit(self) -> &'a mut W { self.w.bits &= !(REG::Ux::one() << OF); self.w } @@ -639,7 +633,7 @@ where { ///Clears the field bit by passing one #[inline(always)] - pub fn clear_bit_by_one(self) -> &'a mut REG::Writer { + pub fn clear_bit_by_one(self) -> &'a mut W { self.w.bits |= REG::Ux::one() << OF; self.w } @@ -652,7 +646,7 @@ where { ///Sets the field bit by passing zero #[inline(always)] - pub fn set_bit_by_zero(self) -> &'a mut REG::Writer { + pub fn set_bit_by_zero(self) -> &'a mut W { self.w.bits &= !(REG::Ux::one() << OF); self.w } @@ -665,7 +659,7 @@ where { ///Toggle the field bit by passing one #[inline(always)] - pub fn toggle_bit(self) -> &'a mut REG::Writer { + pub fn toggle_bit(self) -> &'a mut W { self.w.bits |= REG::Ux::one() << OF; self.w } @@ -678,7 +672,7 @@ where { ///Toggle the field bit by passing zero #[inline(always)] - pub fn toggle_bit(self) -> &'a mut REG::Writer { + pub fn toggle_bit(self) -> &'a mut W { self.w.bits &= !(REG::Ux::one() << OF); self.w } diff --git a/src/generate/generic_atomic.rs b/src/generate/generic_atomic.rs index c9f5380a..9df5cfd1 100644 --- a/src/generate/generic_atomic.rs +++ b/src/generate/generic_atomic.rs @@ -51,12 +51,12 @@ where #[inline(always)] pub unsafe fn set_bits(&self, f: F) where - F: FnOnce(&mut REG::Writer) -> &mut W, + F: FnOnce(&mut W) -> &mut W, { - let bits = f(&mut REG::Writer::from(W { + let bits = f(&mut W { bits: Default::default(), _reg: marker::PhantomData, - })) + }) .bits; REG::Ux::atomic_or(self.register.as_ptr(), bits); } @@ -70,12 +70,12 @@ where #[inline(always)] pub unsafe fn clear_bits(&self, f: F) where - F: FnOnce(&mut REG::Writer) -> &mut W, + F: FnOnce(&mut W) -> &mut W, { - let bits = f(&mut REG::Writer::from(W { + let bits = f(&mut W { bits: !REG::Ux::default(), _reg: marker::PhantomData, - })) + }) .bits; REG::Ux::atomic_and(self.register.as_ptr(), bits); } @@ -89,12 +89,12 @@ where #[inline(always)] pub unsafe fn toggle_bits(&self, f: F) where - F: FnOnce(&mut REG::Writer) -> &mut W, + F: FnOnce(&mut W) -> &mut W, { - let bits = f(&mut REG::Writer::from(W { + let bits = f(&mut W { bits: Default::default(), _reg: marker::PhantomData, - })) + }) .bits; REG::Ux::atomic_xor(self.register.as_ptr(), bits); } diff --git a/src/generate/register.rs b/src/generate/register.rs index f552b469..bc718e32 100644 --- a/src/generate/register.rs +++ b/src/generate/register.rs @@ -146,78 +146,19 @@ pub fn render_register_mod( if can_read { let desc = format!("Register `{}` reader", register.name); - let derive = if config.derive_more { - Some(quote! { #[derive(derive_more::Deref, derive_more::From)] }) - } else { - None - }; mod_items.extend(quote! { #[doc = #desc] - #derive - pub struct R(crate::R<#regspec_ident>); + pub type R = crate::R<#regspec_ident>; }); - - if !config.derive_more { - mod_items.extend(quote! { - impl core::ops::Deref for R { - type Target = crate::R<#regspec_ident>; - - #[inline(always)] - fn deref(&self) -> &Self::Target { - &self.0 - } - } - - impl From> for R { - #[inline(always)] - fn from(reader: crate::R<#regspec_ident>) -> Self { - R(reader) - } - } - }); - } methods.push("read"); } if can_write { let desc = format!("Register `{}` writer", register.name); - let derive = if config.derive_more { - Some(quote! { #[derive(derive_more::Deref, derive_more::DerefMut, derive_more::From)] }) - } else { - None - }; mod_items.extend(quote! { #[doc = #desc] - #derive - pub struct W(crate::W<#regspec_ident>); + pub type W = crate::W<#regspec_ident>; }); - - if !config.derive_more { - mod_items.extend(quote! { - impl core::ops::Deref for W { - type Target = crate::W<#regspec_ident>; - - #[inline(always)] - fn deref(&self) -> &Self::Target { - &self.0 - } - } - - impl core::ops::DerefMut for W { - #[inline(always)] - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } - } - - impl From> for W { - #[inline(always)] - fn from(writer: crate::W<#regspec_ident>) -> Self { - W(writer) - } - } - }); - } methods.push("write_with_zero"); if can_reset { methods.push("reset"); @@ -332,6 +273,7 @@ pub fn render_register_mod( mod_items.extend(w_impl_items); +/* // the writer can be safe if: // * there is a single field that covers the entire register // * that field can represent all values @@ -364,7 +306,7 @@ pub fn render_register_mod( self } }); - } + }*/ close.to_tokens(&mut mod_items); } @@ -406,9 +348,7 @@ pub fn render_register_mod( let doc = format!("`read()` method returns [{name_snake_case}::R](R) reader structure",); mod_items.extend(quote! { #[doc = #doc] - impl crate::Readable for #regspec_ident { - type Reader = R; - } + impl crate::Readable for #regspec_ident {} }); } if can_write { @@ -421,7 +361,6 @@ pub fn render_register_mod( mod_items.extend(quote! { #[doc = #doc] impl crate::Writable for #regspec_ident { - type Writer = W; const ZERO_TO_MODIFY_FIELDS_BITMAP: Self::Ux = #zero_to_modify_fields_bitmap; const ONE_TO_MODIFY_FIELDS_BITMAP: Self::Ux = #one_to_modify_fields_bitmap; } diff --git a/src/main.rs b/src/main.rs index d9dc7d5b..c01ea1b6 100755 --- a/src/main.rs +++ b/src/main.rs @@ -158,12 +158,6 @@ fn run() -> Result<()> { .action(ArgAction::SetTrue) .help("Use PascalCase in stead of UPPER_CASE for enumerated values"), ) - .arg( - Arg::new("derive_more") - .long("derive_more") - .action(ArgAction::SetTrue) - .help("Use derive_more procedural macros to implement Deref and From"), - ) .arg( Arg::new("source_type") .long("source_type") diff --git a/src/util.rs b/src/util.rs index 92333916..4bf6ed07 100644 --- a/src/util.rs +++ b/src/util.rs @@ -46,8 +46,6 @@ pub struct Config { #[cfg_attr(feature = "serde", serde(default))] pub pascal_enum_values: bool, #[cfg_attr(feature = "serde", serde(default))] - pub derive_more: bool, - #[cfg_attr(feature = "serde", serde(default))] pub feature_group: bool, #[cfg_attr(feature = "serde", serde(default))] pub feature_peripheral: bool, @@ -121,7 +119,6 @@ impl Default for Config { keep_list: false, strict: false, pascal_enum_values: false, - derive_more: false, feature_group: false, feature_peripheral: false, max_cluster_size: false, From cfb4dcf4955445efe37669cb1ed1bbf097b7a295 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Mon, 5 Jun 2023 18:32:20 +0300 Subject: [PATCH 2/4] generic REG --- src/generate/register.rs | 41 +++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/generate/register.rs b/src/generate/register.rs index bc718e32..3873cb26 100644 --- a/src/generate/register.rs +++ b/src/generate/register.rs @@ -273,7 +273,7 @@ pub fn render_register_mod( mod_items.extend(w_impl_items); -/* + /* // the writer can be safe if: // * there is a single field that covers the entire register // * that field can represent all values @@ -923,7 +923,7 @@ pub fn fields( proxy_items.extend(quote! { #[doc = #doc] #inline - pub fn #sc(self) -> &'a mut W { + pub fn #sc(self) -> &'a mut crate::W { self.variant(#value_write_ty::#pc) } }); @@ -955,9 +955,9 @@ pub fn fields( span, ); if value_write_ty == "bool" { - quote! { crate::#wproxy<'a, #regspec_ident, O> } + quote! { crate::#wproxy<'a, REG, O> } } else { - quote! { crate::#wproxy<'a, #regspec_ident, O, #value_write_ty> } + quote! { crate::#wproxy<'a, REG, O, #value_write_ty> } } } else { let wproxy = Ident::new( @@ -970,22 +970,37 @@ pub fn fields( ); let width = &util::unsuffixed(width as _); if value_write_ty == "u8" { - quote! { crate::#wproxy<'a, #regspec_ident, #width, O> } + quote! { crate::#wproxy<'a, REG, #width, O> } } else { - quote! { crate::#wproxy<'a, #regspec_ident, #width, O, #value_write_ty> } + quote! { crate::#wproxy<'a, REG, #width, O, #value_write_ty> } } }; mod_items.extend(quote! { #[doc = #field_writer_brief] - pub type #writer_ty<'a, const O: u8> = #proxy; + pub type #writer_ty<'a, REG, const O: u8> = #proxy; }); } // generate proxy items from collected information if !proxy_items.is_empty() { - mod_items.extend(quote! { - impl<'a, const O: u8> #writer_ty<'a, O> { - #proxy_items + mod_items.extend(if width == 1 { + quote! { + impl<'a, REG, const O: u8> #writer_ty<'a, REG, O> + where + REG: crate::Writable + crate::RegisterSpec, + { + #proxy_items + } + } + } else { + quote! { + impl<'a, REG, const O: u8> #writer_ty<'a, REG, O> + where + REG: crate::Writable + crate::RegisterSpec, + REG::Ux: From<#fty> + { + #proxy_items + } } }); } @@ -1032,7 +1047,7 @@ pub fn fields( #[doc = #doc] #inline #[must_use] - pub unsafe fn #name_snake_case(&mut self) -> #writer_ty { + pub unsafe fn #name_snake_case(&mut self) -> #writer_ty<#regspec_ident, O> { #writer_ty::new(self) } }); @@ -1051,7 +1066,7 @@ pub fn fields( #[doc = #doc] #inline #[must_use] - pub fn #name_snake_case_n(&mut self) -> #writer_ty<#sub_offset> { + pub fn #name_snake_case_n(&mut self) -> #writer_ty<#regspec_ident, #sub_offset> { #writer_ty::new(self) } }); @@ -1063,7 +1078,7 @@ pub fn fields( #[doc = #doc] #inline #[must_use] - pub fn #name_snake_case(&mut self) -> #writer_ty<#offset> { + pub fn #name_snake_case(&mut self) -> #writer_ty<#regspec_ident, #offset> { #writer_ty::new(self) } }); From 129a548a22c207e41869c9d87e6458ae85469c03 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Mon, 5 Jun 2023 19:33:37 +0300 Subject: [PATCH 3/4] use instead of type alias for writer --- src/generate/register.rs | 51 +++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/src/generate/register.rs b/src/generate/register.rs index 3873cb26..1e65886e 100644 --- a/src/generate/register.rs +++ b/src/generate/register.rs @@ -616,11 +616,10 @@ pub fn fields( // information in enumeratedValues; // if it's not enumeratedValues, always derive the read proxy as we do not need to re-export // it again from BitReader or FieldReader. - let should_derive_reader = match lookup_filter(&lookup_results, Usage::Read) { - Some((_evs, Some(_base))) => false, - Some((_evs, None)) => true, - None => true, - }; + let should_derive_reader = matches!( + lookup_filter(&lookup_results, Usage::Read), + Some((_, None)) | None + ); // derive the read proxy structure if necessary. if should_derive_reader { @@ -932,11 +931,10 @@ pub fn fields( // derive writer. We derive writer if the write proxy is in current register module, // or writer in different register have different _SPEC structures - let should_derive_writer = match lookup_filter(&lookup_results, Usage::Write) { - Some((_evs, Some(base))) => base.register() != fpath.register(), - Some((_evs, None)) => true, - None => true, - }; + let should_derive_writer = matches!( + lookup_filter(&lookup_results, Usage::Write), + Some((_, None)) | None + ); // derive writer structure by type alias to generic write proxy structure. if should_derive_writer { @@ -1006,6 +1004,22 @@ pub fn fields( } if let Some((evs, Some(base))) = lookup_filter(&lookup_results, Usage::Write) { + // if base.register == None, derive write from the same module. This is allowed because both + // the generated and source write proxy are in the same module. + // we never reuse writer for writer in different module does not have the same _SPEC strcuture, + // thus we cannot write to current register using re-exported write proxy. + + // generate pub use field_1 writer as field_2 writer + let base_field = util::replace_suffix(&base.field.name, ""); + let base_w = (base_field + "_W").to_constant_case_ident(span); + if !writer_derives.contains(&writer_ty) { + let base_path = base_syn_path(base, &fpath, &base_w)?; + mod_items.extend(quote! { + #[doc = #field_writer_brief] + pub use #base_path as #writer_ty; + }); + writer_derives.insert(writer_ty.clone()); + } // if base.register == None, it emits pub use structure from same module. if base.register() != fpath.register() { let writer_reader_different_enum = evs_r != Some(evs); @@ -1020,23 +1034,6 @@ pub fn fields( writer_enum_derives.insert(value_write_ty.clone()); } } - } else { - // if base.register == None, derive write from the same module. This is allowed because both - // the generated and source write proxy are in the same module. - // we never reuse writer for writer in different module does not have the same _SPEC strcuture, - // thus we cannot write to current register using re-exported write proxy. - - // generate pub use field_1 writer as field_2 writer - let base_field = util::replace_suffix(&base.field.name, ""); - let base_w = (base_field + "_W").to_constant_case_ident(span); - if !writer_derives.contains(&writer_ty) { - let base_path = base_syn_path(base, &fpath, &base_w)?; - mod_items.extend(quote! { - #[doc = #field_writer_brief] - pub use #base_path as #writer_ty; - }); - writer_derives.insert(writer_ty.clone()); - } } } From d77344aca8d2fc70771a65cff1aad5533d949bc8 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Mon, 5 Jun 2023 20:08:26 +0300 Subject: [PATCH 4/4] hidden WRaw & RRaw for better docs --- CHANGELOG.md | 1 + src/generate/generic.rs | 23 ++++++++--------------- src/generate/register.rs | 7 +++---- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f837518..b386f07f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/). ## [Unreleased] +- removed register writer & reader wrappers, generic `REG` in field writers (#731) - Updated syn to version 2 (#732) ## [v0.29.0] - 2023-06-05 diff --git a/src/generate/generic.rs b/src/generate/generic.rs index c329da09..c589b149 100644 --- a/src/generate/generic.rs +++ b/src/generate/generic.rs @@ -256,7 +256,10 @@ impl Reg { /// /// Result of the `read` methods of registers. Also used as a closure argument in the `modify` /// method. -pub struct R { +pub type R = RRaw; + +#[doc(hidden)] +pub struct RRaw { pub(crate) bits: REG::Ux, _reg: marker::PhantomData, } @@ -284,25 +287,15 @@ where /// Register writer. /// /// Used as an argument to the closures in the `write` and `modify` methods of the register. -pub struct W { +pub type W = WRaw; + +#[doc(hidden)] +pub struct WRaw { ///Writable bits pub(crate) bits: REG::Ux, _reg: marker::PhantomData, } -impl W { - /// Writes raw bits to the register. - /// - /// # Safety - /// - /// Read datasheet or reference manual to find what values are allowed to pass. - #[inline(always)] - pub unsafe fn bits(&mut self, bits: REG::Ux) -> &mut Self { - self.bits = bits; - self - } -} - #[doc(hidden)] pub struct FieldReaderRaw where diff --git a/src/generate/register.rs b/src/generate/register.rs index 1e65886e..663b61c7 100644 --- a/src/generate/register.rs +++ b/src/generate/register.rs @@ -273,7 +273,6 @@ pub fn render_register_mod( mod_items.extend(w_impl_items); - /* // the writer can be safe if: // * there is a single field that covers the entire register // * that field can represent all values @@ -293,7 +292,7 @@ pub fn render_register_mod( #[doc = "Writes raw bits to the register."] #[inline(always)] pub fn bits(&mut self, bits: #rty) -> &mut Self { - unsafe { self.0.bits(bits) }; + self.bits = bits; self } }); @@ -302,11 +301,11 @@ pub fn render_register_mod( #[doc = "Writes raw bits to the register."] #[inline(always)] pub unsafe fn bits(&mut self, bits: #rty) -> &mut Self { - self.0.bits(bits); + self.bits = bits; self } }); - }*/ + } close.to_tokens(&mut mod_items); }