From 71ff37fdb61b3fb0d73a5510269571c081ab4a3a Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Thu, 19 Oct 2023 13:16:13 +0300 Subject: [PATCH] shorten names for constants --- CHANGELOG.md | 5 +- src/generate/array_proxy.rs | 4 +- src/generate/generic.rs | 88 +++++++++++++--------------------- src/generate/generic_atomic.rs | 6 +-- src/generate/register.rs | 4 +- 5 files changed, 44 insertions(+), 63 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03c8720f..13a3fb75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/). ## [Unreleased] +- Shorter names for `Writable` trait constants + ## [v0.30.2] - 2023-10-22 - Fix documentation warnings @@ -816,8 +818,7 @@ peripheral.register.write(|w| w.field().set()); - Initial version of the `svd2rust` tool -[Unreleased]: https://github.com/rust-embedded/svd2rust/compare/v0.30.2...HEAD -[v0.30.2]: https://github.com/rust-embedded/svd2rust/compare/v0.30.1...v0.30.2 +[Unreleased]: https://github.com/rust-embedded/svd2rust/compare/v0.30.1...HEAD [v0.30.1]: https://github.com/rust-embedded/svd2rust/compare/v0.30.0...v0.30.1 [v0.30.0]: https://github.com/rust-embedded/svd2rust/compare/v0.29.0...v0.30.0 [v0.29.0]: https://github.com/rust-embedded/svd2rust/compare/v0.28.0...v0.29.0 diff --git a/src/generate/array_proxy.rs b/src/generate/array_proxy.rs index 5fc0550f..974063d9 100644 --- a/src/generate/array_proxy.rs +++ b/src/generate/array_proxy.rs @@ -12,13 +12,13 @@ pub struct ArrayProxy { /// As well as providing a PhantomData, this field is non-public, and /// therefore ensures that code outside of this module can never create /// an ArrayProxy. - _array: marker::PhantomData, + _array: Marker, } #[allow(clippy::len_without_is_empty)] impl ArrayProxy { /// Get a reference from an [ArrayProxy] with no bounds checking. pub const unsafe fn get_ref(&self, index: usize) -> &T { - &*(self as *const Self).cast::().add(S * index).cast::() + &*(self as *const Self).cast::().add(S * index).cast() } /// Get a reference from an [ArrayProxy], or return `None` if the index /// is out of bounds. diff --git a/src/generate/generic.rs b/src/generate/generic.rs index afc7e2fa..eaf1f4eb 100644 --- a/src/generate/generic.rs +++ b/src/generate/generic.rs @@ -1,4 +1,4 @@ -use core::marker; +use core::marker::PhantomData as Marker; /// Raw register type (`u8`, `u16`, `u32`, ...) pub trait RawReg: @@ -15,7 +15,7 @@ pub trait RawReg: /// Mask for bits of width `WI` fn mask() -> Self; /// Mask for bits of width 1 - fn one() -> Self; + const ONE: Self; } macro_rules! raw_reg { @@ -25,10 +25,7 @@ macro_rules! raw_reg { fn mask() -> Self { $mask::() } - #[inline(always)] - fn one() -> Self { - 1 - } + const ONE: Self = 1; } const fn $mask() -> $U { <$U>::MAX >> ($size - WI) @@ -68,10 +65,10 @@ pub trait Readable: RegisterSpec {} /// Registers marked with `Readable` can be also be `modify`'ed. pub trait Writable: RegisterSpec { /// 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; + const ZEROS_BITMAP: Self::Ux; /// Specifies the register bits that are not changed if you pass `0` and are changed if you pass `1` - const ONE_TO_MODIFY_FIELDS_BITMAP: Self::Ux; + const ONES_BITMAP: Self::Ux; } /// Reset value of the register. @@ -93,7 +90,7 @@ pub trait Resettable: RegisterSpec { #[repr(transparent)] pub struct Reg { register: vcell::VolatileCell, - _marker: marker::PhantomData, + _marker: Marker, } unsafe impl Send for Reg where REG::Ux: Send {} @@ -127,7 +124,7 @@ impl Reg { pub fn read(&self) -> R { R { bits: self.register.get(), - _reg: marker::PhantomData, + _reg: Marker, } } } @@ -171,9 +168,8 @@ impl Reg { { self.register.set( f(&mut W { - bits: REG::RESET_VALUE & !REG::ONE_TO_MODIFY_FIELDS_BITMAP - | REG::ZERO_TO_MODIFY_FIELDS_BITMAP, - _reg: marker::PhantomData, + bits: REG::RESET_VALUE & !REG::ONES_BITMAP | REG::ZEROS_BITMAP, + _reg: Marker, }) .bits, ); @@ -196,7 +192,7 @@ impl Reg { self.register.set( f(&mut W { bits: REG::Ux::default(), - _reg: marker::PhantomData, + _reg: Marker, }) .bits, ); @@ -237,14 +233,10 @@ impl Reg { let bits = self.register.get(); self.register.set( f( - &R { - bits, - _reg: marker::PhantomData, - }, + &R { bits, _reg: Marker }, &mut W { - bits: bits & !REG::ONE_TO_MODIFY_FIELDS_BITMAP - | REG::ZERO_TO_MODIFY_FIELDS_BITMAP, - _reg: marker::PhantomData, + bits: bits & !REG::ONES_BITMAP | REG::ZEROS_BITMAP, + _reg: Marker, }, ) .bits, @@ -254,17 +246,17 @@ impl Reg { #[doc(hidden)] pub mod raw { - use super::{marker, BitM, FieldSpec, RegisterSpec, Unsafe, Writable}; + use super::{BitM, FieldSpec, Marker, RegisterSpec, Unsafe, Writable}; pub struct R { pub(crate) bits: REG::Ux, - pub(super) _reg: marker::PhantomData, + pub(super) _reg: Marker, } pub struct W { ///Writable bits pub(crate) bits: REG::Ux, - pub(super) _reg: marker::PhantomData, + pub(super) _reg: Marker, } pub struct FieldReader @@ -272,7 +264,7 @@ pub mod raw { FI: FieldSpec, { pub(crate) bits: FI::Ux, - _reg: marker::PhantomData, + _reg: Marker, } impl FieldReader { @@ -280,16 +272,13 @@ pub mod raw { #[allow(unused)] #[inline(always)] pub(crate) const fn new(bits: FI::Ux) -> Self { - Self { - bits, - _reg: marker::PhantomData, - } + Self { bits, _reg: Marker } } } pub struct BitReader { pub(crate) bits: bool, - _reg: marker::PhantomData, + _reg: Marker, } impl BitReader { @@ -297,10 +286,7 @@ pub mod raw { #[allow(unused)] #[inline(always)] pub(crate) const fn new(bits: bool) -> Self { - Self { - bits, - _reg: marker::PhantomData, - } + Self { bits, _reg: Marker } } } @@ -310,7 +296,7 @@ pub mod raw { FI: FieldSpec, { pub(crate) w: &'a mut W, - _field: marker::PhantomData<(FI, Safety)>, + _field: Marker<(FI, Safety)>, } impl<'a, REG, const WI: u8, const O: u8, FI, Safety> FieldWriter<'a, REG, WI, O, FI, Safety> @@ -322,10 +308,7 @@ pub mod raw { #[allow(unused)] #[inline(always)] pub(crate) fn new(w: &'a mut W) -> Self { - Self { - w, - _field: marker::PhantomData, - } + Self { w, _field: Marker } } } @@ -335,7 +318,7 @@ pub mod raw { bool: From, { pub(crate) w: &'a mut W, - _field: marker::PhantomData<(FI, M)>, + _field: Marker<(FI, M)>, } impl<'a, REG, const O: u8, FI, M> BitWriter<'a, REG, O, FI, M> @@ -347,10 +330,7 @@ pub mod raw { #[allow(unused)] #[inline(always)] pub(crate) fn new(w: &'a mut W) -> Self { - Self { - w, - _field: marker::PhantomData, - } + Self { w, _field: Marker } } } } @@ -522,8 +502,8 @@ macro_rules! bit_proxy { /// Writes bit to the field #[inline(always)] 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.bits &= !(REG::Ux::ONE << OF); + self.w.bits |= (REG::Ux::from(value) & REG::Ux::ONE) << OF; self.w } /// Writes `variant` to the field @@ -551,13 +531,13 @@ where /// Sets the field bit #[inline(always)] pub fn set_bit(self) -> &'a mut W { - self.w.bits |= REG::Ux::one() << OF; + self.w.bits |= REG::Ux::ONE << OF; self.w } /// Clears the field bit #[inline(always)] pub fn clear_bit(self) -> &'a mut W { - self.w.bits &= !(REG::Ux::one() << OF); + self.w.bits &= !(REG::Ux::ONE << OF); self.w } } @@ -570,7 +550,7 @@ where /// Sets the field bit #[inline(always)] pub fn set_bit(self) -> &'a mut W { - self.w.bits |= REG::Ux::one() << OF; + self.w.bits |= REG::Ux::ONE << OF; self.w } } @@ -583,7 +563,7 @@ where /// Clears the field bit #[inline(always)] pub fn clear_bit(self) -> &'a mut W { - self.w.bits &= !(REG::Ux::one() << OF); + self.w.bits &= !(REG::Ux::ONE << OF); self.w } } @@ -596,7 +576,7 @@ where ///Clears the field bit by passing one #[inline(always)] pub fn clear_bit_by_one(self) -> &'a mut W { - self.w.bits |= REG::Ux::one() << OF; + self.w.bits |= REG::Ux::ONE << OF; self.w } } @@ -609,7 +589,7 @@ where ///Sets the field bit by passing zero #[inline(always)] pub fn set_bit_by_zero(self) -> &'a mut W { - self.w.bits &= !(REG::Ux::one() << OF); + self.w.bits &= !(REG::Ux::ONE << OF); self.w } } @@ -622,7 +602,7 @@ where ///Toggle the field bit by passing one #[inline(always)] pub fn toggle_bit(self) -> &'a mut W { - self.w.bits |= REG::Ux::one() << OF; + self.w.bits |= REG::Ux::ONE << OF; self.w } } @@ -635,7 +615,7 @@ where ///Toggle the field bit by passing zero #[inline(always)] pub fn toggle_bit(self) -> &'a mut W { - self.w.bits &= !(REG::Ux::one() << OF); + self.w.bits &= !(REG::Ux::ONE << OF); self.w } } diff --git a/src/generate/generic_atomic.rs b/src/generate/generic_atomic.rs index f0c436f6..bd1feb2a 100644 --- a/src/generate/generic_atomic.rs +++ b/src/generate/generic_atomic.rs @@ -54,7 +54,7 @@ mod atomic { { let bits = f(&mut W { bits: Default::default(), - _reg: marker::PhantomData, + _reg: Marker, }) .bits; REG::Ux::atomic_or(self.register.as_ptr(), bits); @@ -73,7 +73,7 @@ mod atomic { { let bits = f(&mut W { bits: !REG::Ux::default(), - _reg: marker::PhantomData, + _reg: Marker, }) .bits; REG::Ux::atomic_and(self.register.as_ptr(), bits); @@ -92,7 +92,7 @@ mod atomic { { let bits = f(&mut W { bits: Default::default(), - _reg: marker::PhantomData, + _reg: Marker, }) .bits; REG::Ux::atomic_xor(self.register.as_ptr(), bits); diff --git a/src/generate/register.rs b/src/generate/register.rs index 8e8aede0..c741ea85 100644 --- a/src/generate/register.rs +++ b/src/generate/register.rs @@ -420,8 +420,8 @@ pub fn render_register_mod( mod_items.extend(quote! { #[doc = #doc] impl crate::Writable for #regspec_ident { - 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; + const ZEROS_BITMAP: Self::Ux = #zero_to_modify_fields_bitmap; + const ONES_BITMAP: Self::Ux = #one_to_modify_fields_bitmap; } }); }