Skip to content

Commit

Permalink
Merge pull request #750 from rust-embedded/const-fn
Browse files Browse the repository at this point in the history
const fn
  • Loading branch information
burrbull authored Oct 18, 2023
2 parents 460ea2e + 2e62581 commit effc1cb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 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]

- Use `const fn` where allowed

## [v0.30.1] - 2023-10-01

- Fix clippy lints on `nightly`
Expand Down
10 changes: 4 additions & 6 deletions src/generate/array_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@ pub struct ArrayProxy<T, const COUNT: usize, const STRIDE: usize> {
#[allow(clippy::len_without_is_empty)]
impl<T, const C: usize, const S: usize> ArrayProxy<T, C, S> {
/// 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::<u8>().add(S * index).cast::<T>()
}
/// 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 {
None
}
}
/// Return the number of items.
pub fn len(&self) -> usize {
pub const fn len(&self) -> usize {
C
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/generate/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -364,7 +364,7 @@ pub type R<REG> = raw::R<REG>;
impl<REG: RegisterSpec> R<REG> {
/// Reads raw bits from register.
#[inline(always)]
pub fn bits(&self) -> REG::Ux {
pub const fn bits(&self) -> REG::Ux {
self.bits
}
}
Expand Down Expand Up @@ -397,7 +397,7 @@ pub type BitReader<FI = bool> = raw::BitReader<FI>;
impl<FI: FieldSpec> FieldReader<FI> {
/// Reads raw bits from field.
#[inline(always)]
pub fn bits(&self) -> FI::Ux {
pub const fn bits(&self) -> FI::Ux {
self.bits
}
}
Expand Down Expand Up @@ -426,7 +426,7 @@ where
impl<FI> BitReader<FI> {
/// 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).
Expand Down
4 changes: 2 additions & 2 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit effc1cb

Please sign in to comment.