Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const fn #750

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading