diff --git a/CHANGELOG.md b/CHANGELOG.md index 64e03c66..f288e976 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/). ## [Unreleased] +- Use `const fn` where allowed + ## [v0.30.1] - 2023-10-01 - Fix clippy lints on `nightly` diff --git a/src/generate/array_proxy.rs b/src/generate/array_proxy.rs index b47074ba..00f7064a 100644 --- a/src/generate/array_proxy.rs +++ b/src/generate/array_proxy.rs @@ -16,14 +16,12 @@ pub struct ArrayProxy { #[allow(clippy::len_without_is_empty)] impl ArrayProxy { /// Get a reference from an [ArrayProxy] with no bounds checking. - pub unsafe fn get_ref(&self, index: usize) -> &T { - let base = self as *const Self as usize; - let address = base + S * index; - &*(address as *const T) + pub const unsafe fn get_ref(&self, index: usize) -> &T { + &*(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. - pub fn get(&self, index: usize) -> Option<&T> { + pub const fn get(&self, index: usize) -> Option<&T> { if index < C { Some(unsafe { self.get_ref(index) }) } else { @@ -31,7 +29,7 @@ impl ArrayProxy { } } /// Return the number of items. - pub fn len(&self) -> usize { + pub const fn len(&self) -> usize { C } } diff --git a/src/generate/generic.rs b/src/generate/generic.rs index b91fa694..cc13f75a 100644 --- a/src/generate/generic.rs +++ b/src/generate/generic.rs @@ -279,7 +279,7 @@ pub mod raw { /// Creates a new instance of the reader. #[allow(unused)] #[inline(always)] - pub(crate) fn new(bits: FI::Ux) -> Self { + pub(crate) const fn new(bits: FI::Ux) -> Self { Self { bits, _reg: marker::PhantomData, @@ -296,7 +296,7 @@ pub mod raw { /// Creates a new instance of the reader. #[allow(unused)] #[inline(always)] - pub(crate) fn new(bits: bool) -> Self { + pub(crate) const fn new(bits: bool) -> Self { Self { bits, _reg: marker::PhantomData, @@ -364,7 +364,7 @@ pub type R = raw::R; impl R { /// Reads raw bits from register. #[inline(always)] - pub fn bits(&self) -> REG::Ux { + pub const fn bits(&self) -> REG::Ux { self.bits } } @@ -397,7 +397,7 @@ pub type BitReader = raw::BitReader; impl FieldReader { /// Reads raw bits from field. #[inline(always)] - pub fn bits(&self) -> FI::Ux { + pub const fn bits(&self) -> FI::Ux { self.bits } } @@ -426,7 +426,7 @@ where impl BitReader { /// Value of the field as raw bits. #[inline(always)] - pub fn bit(&self) -> bool { + pub const fn bit(&self) -> bool { self.bits } /// Returns `true` if the bit is clear (0). diff --git a/src/generate/register.rs b/src/generate/register.rs index 335915c0..9c9a54a6 100644 --- a/src/generate/register.rs +++ b/src/generate/register.rs @@ -772,7 +772,7 @@ pub fn fields( enum_items.extend(quote! { #[doc = "Get enumerated values variant"] #inline - pub fn variant(&self) -> Option<#value_read_ty> { + pub const fn variant(&self) -> Option<#value_read_ty> { match self.bits { #arms } @@ -782,7 +782,7 @@ pub fn fields( enum_items.extend(quote! { #[doc = "Get enumerated values variant"] #inline - pub fn variant(&self) -> #value_read_ty { + pub const fn variant(&self) -> #value_read_ty { match self.bits { #arms }