Skip to content

Commit 806734c

Browse files
committed
use ConstZero/One instead of Default
1 parent 6649704 commit 806734c

File tree

7 files changed

+55
-38
lines changed

7 files changed

+55
-38
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
on:
22
push:
3-
branches: master
3+
branches: [master]
44
pull_request:
55
merge_group:
66

CHANGELOG.md

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

88
## [Unreleased]
99

10+
- *breaking change* use `ConstZero::ZERO` instead of `Default::default()` to force const
1011
- Add `mtvec_align` field to `riscv_config` to configure the byte alignment of interrupt vector table.
1112
- Fix reexport path when "%s" inside "derivedFrom"
1213
- Force using rust edition 2021 in CI

src/generate/generic.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use core::marker;
33
/// Raw register type (`u8`, `u16`, `u32`, ...)
44
pub trait RawReg:
55
Copy
6-
+ Default
76
+ From<bool>
87
+ core::ops::BitOr<Output = Self>
98
+ core::ops::BitAnd<Output = Self>
@@ -14,8 +13,10 @@ pub trait RawReg:
1413
{
1514
/// Mask for bits of width `WI`
1615
fn mask<const WI: u8>() -> Self;
17-
/// Mask for bits of width 1
18-
fn one() -> Self;
16+
/// `0`
17+
const ZERO: Self;
18+
/// `1`
19+
const ONE: Self;
1920
}
2021

2122
macro_rules! raw_reg {
@@ -25,10 +26,8 @@ macro_rules! raw_reg {
2526
fn mask<const WI: u8>() -> Self {
2627
$mask::<WI>()
2728
}
28-
#[inline(always)]
29-
fn one() -> Self {
30-
1
31-
}
29+
const ZERO: Self = 0;
30+
const ONE: Self = 1;
3231
}
3332
const fn $mask<const WI: u8>() -> $U {
3433
<$U>::MAX >> ($size - WI)
@@ -74,10 +73,10 @@ pub trait Writable: RegisterSpec {
7473
type Safety;
7574

7675
/// Specifies the register bits that are not changed if you pass `1` and are changed if you pass `0`
77-
const ZERO_TO_MODIFY_FIELDS_BITMAP: Self::Ux;
76+
const ZERO_TO_MODIFY_FIELDS_BITMAP: Self::Ux = Self::Ux::ZERO;
7877

7978
/// Specifies the register bits that are not changed if you pass `0` and are changed if you pass `1`
80-
const ONE_TO_MODIFY_FIELDS_BITMAP: Self::Ux;
79+
const ONE_TO_MODIFY_FIELDS_BITMAP: Self::Ux = Self::Ux::ZERO;
8180
}
8281

8382
/// Reset value of the register.
@@ -86,7 +85,7 @@ pub trait Writable: RegisterSpec {
8685
/// register by using the `reset` method.
8786
pub trait Resettable: RegisterSpec {
8887
/// Reset value of the register.
89-
const RESET_VALUE: Self::Ux;
88+
const RESET_VALUE: Self::Ux = Self::Ux::ZERO;
9089

9190
/// Reset value of the register.
9291
#[inline(always)]
@@ -247,7 +246,10 @@ impl<REG: Writable> W<REG> {
247246
self
248247
}
249248
}
250-
impl<REG> W<REG> where REG: Writable<Safety = Safe> {
249+
impl<REG> W<REG>
250+
where
251+
REG: Writable<Safety = Safe>,
252+
{
251253
/// Writes raw bits to the register.
252254
#[inline(always)]
253255
pub fn set(&mut self, bits: REG::Ux) -> &mut Self {
@@ -335,7 +337,8 @@ pub struct RangeFrom<const MIN: u64>;
335337
pub struct RangeTo<const MAX: u64>;
336338

337339
/// Write field Proxy
338-
pub type FieldWriter<'a, REG, const WI: u8, FI = u8, Safety = Unsafe> = raw::FieldWriter<'a, REG, WI, FI, Safety>;
340+
pub type FieldWriter<'a, REG, const WI: u8, FI = u8, Safety = Unsafe> =
341+
raw::FieldWriter<'a, REG, WI, FI, Safety>;
339342

340343
impl<REG, const WI: u8, FI, Safety> FieldWriter<'_, REG, WI, FI, Safety>
341344
where
@@ -390,7 +393,8 @@ where
390393
}
391394
}
392395

393-
impl<'a, REG, const WI: u8, FI, const MIN: u64, const MAX: u64> FieldWriter<'a, REG, WI, FI, Range<MIN, MAX>>
396+
impl<'a, REG, const WI: u8, FI, const MIN: u64, const MAX: u64>
397+
FieldWriter<'a, REG, WI, FI, Range<MIN, MAX>>
394398
where
395399
REG: Writable + RegisterSpec,
396400
FI: FieldSpec,
@@ -478,7 +482,7 @@ macro_rules! bit_proxy {
478482
pub const fn width(&self) -> u8 {
479483
Self::WIDTH
480484
}
481-
485+
482486
/// Field offset
483487
#[inline(always)]
484488
pub const fn offset(&self) -> u8 {
@@ -488,8 +492,8 @@ macro_rules! bit_proxy {
488492
/// Writes bit to the field
489493
#[inline(always)]
490494
pub fn bit(self, value: bool) -> &'a mut W<REG> {
491-
self.w.bits &= !(REG::Ux::one() << self.o);
492-
self.w.bits |= (REG::Ux::from(value) & REG::Ux::one()) << self.o;
495+
self.w.bits &= !(REG::Ux::ONE << self.o);
496+
self.w.bits |= (REG::Ux::from(value) & REG::Ux::ONE) << self.o;
493497
self.w
494498
}
495499
/// Writes `variant` to the field
@@ -517,13 +521,13 @@ where
517521
/// Sets the field bit
518522
#[inline(always)]
519523
pub fn set_bit(self) -> &'a mut W<REG> {
520-
self.w.bits |= REG::Ux::one() << self.o;
524+
self.w.bits |= REG::Ux::ONE << self.o;
521525
self.w
522526
}
523527
/// Clears the field bit
524528
#[inline(always)]
525529
pub fn clear_bit(self) -> &'a mut W<REG> {
526-
self.w.bits &= !(REG::Ux::one() << self.o);
530+
self.w.bits &= !(REG::Ux::ONE << self.o);
527531
self.w
528532
}
529533
}
@@ -536,7 +540,7 @@ where
536540
/// Sets the field bit
537541
#[inline(always)]
538542
pub fn set_bit(self) -> &'a mut W<REG> {
539-
self.w.bits |= REG::Ux::one() << self.o;
543+
self.w.bits |= REG::Ux::ONE << self.o;
540544
self.w
541545
}
542546
}
@@ -549,7 +553,7 @@ where
549553
/// Clears the field bit
550554
#[inline(always)]
551555
pub fn clear_bit(self) -> &'a mut W<REG> {
552-
self.w.bits &= !(REG::Ux::one() << self.o);
556+
self.w.bits &= !(REG::Ux::ONE << self.o);
553557
self.w
554558
}
555559
}
@@ -562,7 +566,7 @@ where
562566
///Clears the field bit by passing one
563567
#[inline(always)]
564568
pub fn clear_bit_by_one(self) -> &'a mut W<REG> {
565-
self.w.bits |= REG::Ux::one() << self.o;
569+
self.w.bits |= REG::Ux::ONE << self.o;
566570
self.w
567571
}
568572
}
@@ -575,7 +579,7 @@ where
575579
///Sets the field bit by passing zero
576580
#[inline(always)]
577581
pub fn set_bit_by_zero(self) -> &'a mut W<REG> {
578-
self.w.bits &= !(REG::Ux::one() << self.o);
582+
self.w.bits &= !(REG::Ux::ONE << self.o);
579583
self.w
580584
}
581585
}
@@ -588,7 +592,7 @@ where
588592
///Toggle the field bit by passing one
589593
#[inline(always)]
590594
pub fn toggle_bit(self) -> &'a mut W<REG> {
591-
self.w.bits |= REG::Ux::one() << self.o;
595+
self.w.bits |= REG::Ux::ONE << self.o;
592596
self.w
593597
}
594598
}
@@ -601,7 +605,7 @@ where
601605
///Toggle the field bit by passing zero
602606
#[inline(always)]
603607
pub fn toggle_bit(self) -> &'a mut W<REG> {
604-
self.w.bits &= !(REG::Ux::one() << self.o);
608+
self.w.bits &= !(REG::Ux::ONE << self.o);
605609
self.w
606610
}
607611
}

src/generate/generic_atomic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ mod atomic {
3939

4040
impl<REG: Readable + Writable> Reg<REG>
4141
where
42-
REG::Ux: AtomicOperations
42+
REG::Ux: AtomicOperations,
4343
{
4444
/// Set high every bit in the register that was set in the write proxy. Leave other bits
4545
/// untouched. The write is done in a single atomic instruction.
@@ -53,7 +53,7 @@ mod atomic {
5353
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
5454
{
5555
let bits = f(&mut W {
56-
bits: Default::default(),
56+
bits: REG::Ux::ZERO,
5757
_reg: marker::PhantomData,
5858
})
5959
.bits;
@@ -72,7 +72,7 @@ mod atomic {
7272
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
7373
{
7474
let bits = f(&mut W {
75-
bits: !REG::Ux::default(),
75+
bits: !REG::Ux::ZERO,
7676
_reg: marker::PhantomData,
7777
})
7878
.bits;
@@ -91,7 +91,7 @@ mod atomic {
9191
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
9292
{
9393
let bits = f(&mut W {
94-
bits: Default::default(),
94+
bits: REG::Ux::ZERO,
9595
_reg: marker::PhantomData,
9696
})
9797
.bits;

src/generate/generic_reg_vcell.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl<REG: Writable> Reg<REG> {
148148
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
149149
{
150150
let value = f(&mut W {
151-
bits: REG::Ux::default(),
151+
bits: REG::Ux::ZERO,
152152
_reg: marker::PhantomData,
153153
})
154154
.bits;
@@ -169,7 +169,7 @@ impl<REG: Writable> Reg<REG> {
169169
F: FnOnce(&mut W<REG>) -> T,
170170
{
171171
let mut writer = W {
172-
bits: REG::Ux::default(),
172+
bits: REG::Ux::ZERO,
173173
_reg: marker::PhantomData,
174174
};
175175

src/generate/register.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,24 +413,31 @@ pub fn render_register_mod(
413413

414414
let doc = format!("`write(|w| ..)` method takes [`{mod_ty}::W`](W) writer structure",);
415415

416-
let zero_to_modify_fields_bitmap = util::hex(zero_to_modify_fields_bitmap);
417-
let one_to_modify_fields_bitmap = util::hex(one_to_modify_fields_bitmap);
416+
let zero_to_modify_fields_bitmap = util::hex_nonzero(zero_to_modify_fields_bitmap)
417+
.map(|bm| quote!(const ZERO_TO_MODIFY_FIELDS_BITMAP: #rty = #bm;));
418+
let one_to_modify_fields_bitmap = util::hex_nonzero(one_to_modify_fields_bitmap)
419+
.map(|bm| quote!(const ONE_TO_MODIFY_FIELDS_BITMAP: #rty = #bm;));
418420

419421
mod_items.extend(quote! {
420422
#[doc = #doc]
421423
impl crate::Writable for #regspec_ty {
422424
type Safety = crate::#safe_ty;
423-
const ZERO_TO_MODIFY_FIELDS_BITMAP: #rty = #zero_to_modify_fields_bitmap;
424-
const ONE_TO_MODIFY_FIELDS_BITMAP: #rty = #one_to_modify_fields_bitmap;
425+
#zero_to_modify_fields_bitmap
426+
#one_to_modify_fields_bitmap
425427
}
426428
});
427429
}
428-
if let Some(rv) = properties.reset_value.map(util::hex) {
429-
let doc = format!("`reset()` method sets {} to value {rv}", register.name);
430+
if let Some(rv) = properties.reset_value.map(util::hex_nonzero) {
431+
let doc = if let Some(rv) = &rv {
432+
format!("`reset()` method sets {} to value {rv}", register.name)
433+
} else {
434+
format!("`reset()` method sets {} to value 0", register.name)
435+
};
436+
let rv = rv.map(|rv| quote!(const RESET_VALUE: #rty = #rv;));
430437
mod_items.extend(quote! {
431438
#[doc = #doc]
432439
impl crate::Resettable for #regspec_ty {
433-
const RESET_VALUE: #rty = #rv;
440+
#rv
434441
}
435442
});
436443
}

src/util.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ pub fn hex(n: u64) -> LitInt {
255255
)
256256
}
257257

258+
/// Turns non-zero `n` into an unsuffixed separated hex token
259+
pub fn hex_nonzero(n: u64) -> Option<LitInt> {
260+
(n != 0).then(|| hex(n))
261+
}
262+
258263
/// Turns `n` into an unsuffixed token
259264
pub fn unsuffixed(n: impl Into<u64>) -> LitInt {
260265
LitInt::new(&n.into().to_string(), Span::call_site())

0 commit comments

Comments
 (0)