Skip to content

Commit effc1cb

Browse files
authored
Merge pull request #750 from rust-embedded/const-fn
const fn
2 parents 460ea2e + 2e62581 commit effc1cb

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
77

88
## [Unreleased]
99

10+
- Use `const fn` where allowed
11+
1012
## [v0.30.1] - 2023-10-01
1113

1214
- Fix clippy lints on `nightly`

src/generate/array_proxy.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,20 @@ pub struct ArrayProxy<T, const COUNT: usize, const STRIDE: usize> {
1616
#[allow(clippy::len_without_is_empty)]
1717
impl<T, const C: usize, const S: usize> ArrayProxy<T, C, S> {
1818
/// Get a reference from an [ArrayProxy] with no bounds checking.
19-
pub unsafe fn get_ref(&self, index: usize) -> &T {
20-
let base = self as *const Self as usize;
21-
let address = base + S * index;
22-
&*(address as *const T)
19+
pub const unsafe fn get_ref(&self, index: usize) -> &T {
20+
&*(self as *const Self).cast::<u8>().add(S * index).cast::<T>()
2321
}
2422
/// Get a reference from an [ArrayProxy], or return `None` if the index
2523
/// is out of bounds.
26-
pub fn get(&self, index: usize) -> Option<&T> {
24+
pub const fn get(&self, index: usize) -> Option<&T> {
2725
if index < C {
2826
Some(unsafe { self.get_ref(index) })
2927
} else {
3028
None
3129
}
3230
}
3331
/// Return the number of items.
34-
pub fn len(&self) -> usize {
32+
pub const fn len(&self) -> usize {
3533
C
3634
}
3735
}

src/generate/generic.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ pub mod raw {
279279
/// Creates a new instance of the reader.
280280
#[allow(unused)]
281281
#[inline(always)]
282-
pub(crate) fn new(bits: FI::Ux) -> Self {
282+
pub(crate) const fn new(bits: FI::Ux) -> Self {
283283
Self {
284284
bits,
285285
_reg: marker::PhantomData,
@@ -296,7 +296,7 @@ pub mod raw {
296296
/// Creates a new instance of the reader.
297297
#[allow(unused)]
298298
#[inline(always)]
299-
pub(crate) fn new(bits: bool) -> Self {
299+
pub(crate) const fn new(bits: bool) -> Self {
300300
Self {
301301
bits,
302302
_reg: marker::PhantomData,
@@ -364,7 +364,7 @@ pub type R<REG> = raw::R<REG>;
364364
impl<REG: RegisterSpec> R<REG> {
365365
/// Reads raw bits from register.
366366
#[inline(always)]
367-
pub fn bits(&self) -> REG::Ux {
367+
pub const fn bits(&self) -> REG::Ux {
368368
self.bits
369369
}
370370
}
@@ -397,7 +397,7 @@ pub type BitReader<FI = bool> = raw::BitReader<FI>;
397397
impl<FI: FieldSpec> FieldReader<FI> {
398398
/// Reads raw bits from field.
399399
#[inline(always)]
400-
pub fn bits(&self) -> FI::Ux {
400+
pub const fn bits(&self) -> FI::Ux {
401401
self.bits
402402
}
403403
}
@@ -426,7 +426,7 @@ where
426426
impl<FI> BitReader<FI> {
427427
/// Value of the field as raw bits.
428428
#[inline(always)]
429-
pub fn bit(&self) -> bool {
429+
pub const fn bit(&self) -> bool {
430430
self.bits
431431
}
432432
/// Returns `true` if the bit is clear (0).

src/generate/register.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ pub fn fields(
772772
enum_items.extend(quote! {
773773
#[doc = "Get enumerated values variant"]
774774
#inline
775-
pub fn variant(&self) -> Option<#value_read_ty> {
775+
pub const fn variant(&self) -> Option<#value_read_ty> {
776776
match self.bits {
777777
#arms
778778
}
@@ -782,7 +782,7 @@ pub fn fields(
782782
enum_items.extend(quote! {
783783
#[doc = "Get enumerated values variant"]
784784
#inline
785-
pub fn variant(&self) -> #value_read_ty {
785+
pub const fn variant(&self) -> #value_read_ty {
786786
match self.bits {
787787
#arms
788788
}

0 commit comments

Comments
 (0)