Skip to content

Commit d462120

Browse files
committed
use ConstDefault instead of Default
1 parent f0f71e9 commit d462120

File tree

8 files changed

+43
-19
lines changed

8 files changed

+43
-19
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 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 `ConstDefault::DEFAULT` instead of `Default::default()` to force const
11+
1012
## [v0.33.3] - 2024-05-10
1113

1214
- Yet more clean field & register `Debug`
@@ -891,8 +893,7 @@ peripheral.register.write(|w| w.field().set());
891893

892894
- Initial version of the `svd2rust` tool
893895

894-
[Unreleased]: https://github.com/rust-embedded/svd2rust/compare/v0.33.3...HEAD
895-
[v0.33.3]: https://github.com/rust-embedded/svd2rust/compare/v0.33.2...v0.33.3
896+
[Unreleased]: https://github.com/rust-embedded/svd2rust/compare/v0.33.2...HEAD
896897
[v0.33.2]: https://github.com/rust-embedded/svd2rust/compare/v0.33.1...v0.33.2
897898
[v0.33.1]: https://github.com/rust-embedded/svd2rust/compare/v0.33.0...v0.33.1
898899
[v0.33.0]: https://github.com/rust-embedded/svd2rust/compare/v0.32.0...v0.33.0

ci/script.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ main() {
4343
echo 'cortex-m = "0.7.7"' >> $td/Cargo.toml
4444
echo 'cortex-m-rt = "0.7.3"' >> $td/Cargo.toml
4545
echo 'vcell = "0.1.3"' >> $td/Cargo.toml
46+
echo 'const-default = { version = "1.0", default-features = false }' >> $td/Cargo.toml
4647
if [[ "$options" == *"--atomics"* ]]; then
4748
echo 'portable-atomic = { version = "1.4", default-features = false }' >> $td/Cargo.toml
4849
fi

ci/svd2rust-regress/src/svd_test.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ use std::{
1111
path::Path,
1212
};
1313

14-
const CRATES_ALL: &[&str] = &["critical-section = \"1.0\"", "vcell = \"0.1.2\""];
14+
const CRATES_ALL: &[&str] = &[
15+
"critical-section = \"1.0\"",
16+
"vcell = \"0.1.2\"",
17+
"const-default = { version = \"1.0\", default-features = false }",
18+
];
1519
const CRATES_MSP430: &[&str] = &["msp430 = \"0.4.0\"", "msp430-rt = \"0.4.0\""];
1620
const CRATES_ATOMICS: &[&str] =
1721
&["portable-atomic = { version = \"0.3.16\", default-features = false }"];

src/generate/generic.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use core::marker;
2+
use const_default::ConstDefault;
23

34
/// Raw register type (`u8`, `u16`, `u32`, ...)
45
pub trait RawReg:
56
Copy
6-
+ Default
7+
+ ConstDefault
78
+ From<bool>
89
+ core::ops::BitOr<Output = Self>
910
+ core::ops::BitAnd<Output = Self>
@@ -74,10 +75,10 @@ pub trait Writable: RegisterSpec {
7475
type Safety;
7576

7677
/// 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;
78+
const ZERO_TO_MODIFY_FIELDS_BITMAP: Self::Ux = Self::Ux::DEFAULT;
7879

7980
/// 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;
81+
const ONE_TO_MODIFY_FIELDS_BITMAP: Self::Ux = Self::Ux::DEFAULT;
8182
}
8283

8384
/// Reset value of the register.
@@ -86,7 +87,7 @@ pub trait Writable: RegisterSpec {
8687
/// register by using the `reset` method.
8788
pub trait Resettable: RegisterSpec {
8889
/// Reset value of the register.
89-
const RESET_VALUE: Self::Ux;
90+
const RESET_VALUE: Self::Ux = Self::Ux::DEFAULT;
9091

9192
/// Reset value of the register.
9293
#[inline(always)]
@@ -201,7 +202,7 @@ impl<REG: Writable> Reg<REG> {
201202
{
202203
self.register.set(
203204
f(&mut W {
204-
bits: REG::Ux::default(),
205+
bits: REG::Ux::DEFAULT,
205206
_reg: marker::PhantomData,
206207
})
207208
.bits,

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 + Default + core::ops::Not<Output = REG::Ux>,
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::DEFAULT,
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::DEFAULT,
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::DEFAULT,
9595
_reg: marker::PhantomData,
9696
})
9797
.bits;

src/generate/register.rs

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

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

410-
let zero_to_modify_fields_bitmap = util::hex(zero_to_modify_fields_bitmap);
411-
let one_to_modify_fields_bitmap = util::hex(one_to_modify_fields_bitmap);
410+
let zero_to_modify_fields_bitmap = util::hex_nonzero(zero_to_modify_fields_bitmap)
411+
.map(|bm| quote!(const ZERO_TO_MODIFY_FIELDS_BITMAP: #rty = #bm;));
412+
let one_to_modify_fields_bitmap = util::hex_nonzero(one_to_modify_fields_bitmap)
413+
.map(|bm| quote!(const ONE_TO_MODIFY_FIELDS_BITMAP: #rty = #bm;));
412414

413415
mod_items.extend(quote! {
414416
#[doc = #doc]
415417
impl crate::Writable for #regspec_ty {
416418
type Safety = crate::#safe_ty;
417-
const ZERO_TO_MODIFY_FIELDS_BITMAP: #rty = #zero_to_modify_fields_bitmap;
418-
const ONE_TO_MODIFY_FIELDS_BITMAP: #rty = #one_to_modify_fields_bitmap;
419+
#zero_to_modify_fields_bitmap
420+
#one_to_modify_fields_bitmap
419421
}
420422
});
421423
}
422-
if let Some(rv) = properties.reset_value.map(util::hex) {
423-
let doc = format!("`reset()` method sets {} to value {rv}", register.name);
424+
if let Some(rv) = properties.reset_value.map(util::hex_nonzero) {
425+
let doc = if let Some(rv) = &rv {
426+
format!("`reset()` method sets {} to value {rv}", register.name)
427+
} else {
428+
format!("`reset()` method sets {} to value 0", register.name)
429+
};
430+
let rv = rv.map(|rv| quote!(const RESET_VALUE: #rty = #rv;));
424431
mod_items.extend(quote! {
425432
#[doc = #doc]
426433
impl crate::Resettable for #regspec_ty {
427-
const RESET_VALUE: #rty = #rv;
434+
#rv
428435
}
429436
});
430437
}

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
//! - [`cortex-m`](https://crates.io/crates/cortex-m) >=v0.7.6
6363
//! - [`cortex-m-rt`](https://crates.io/crates/cortex-m-rt) >=v0.6.13
6464
//! - [`vcell`](https://crates.io/crates/vcell) >=v0.1.2
65+
//! - [`const-default`](https://crates.io/crates/const-default) >=v1.0
6566
//!
6667
//! Furthermore, the "device" feature of `cortex-m-rt` must be enabled when the `rt` feature
6768
//! is enabled. The `Cargo.toml` of the device crate will look like this:
@@ -126,6 +127,7 @@
126127
//! - [`msp430`](https://crates.io/crates/msp430) v0.4.x
127128
//! - [`msp430-rt`](https://crates.io/crates/msp430-rt) v0.4.x
128129
//! - [`vcell`](https://crates.io/crates/vcell) v0.1.x
130+
//! - [`const-default`](https://crates.io/crates/const-default) v1.x.x
129131
//!
130132
//! The "device" feature of `msp430-rt` must be enabled when the `rt` feature is
131133
//! enabled. The `Cargo.toml` of the device crate will look like this:
@@ -136,6 +138,7 @@
136138
//! msp430 = "0.4.0"
137139
//! msp430-rt = { version = "0.4.0", optional = true }
138140
//! vcell = "0.1.0"
141+
//! const-default = { version = "1.0", default-features = false }
139142
//!
140143
//! [features]
141144
//! rt = ["msp430-rt/device"]
@@ -152,6 +155,7 @@
152155
//! - [`riscv`](https://crates.io/crates/riscv) v0.9.x (if target is RISC-V)
153156
//! - [`riscv-rt`](https://crates.io/crates/riscv-rt) v0.9.x (if target is RISC-V)
154157
//! - [`vcell`](https://crates.io/crates/vcell) v0.1.x
158+
//! - [`const-default`](https://crates.io/crates/const-default) v1.x.x
155159
//!
156160
//! The `*-rt` dependencies must be optional only enabled when the `rt` feature is enabled. The
157161
//! `Cargo.toml` of the device crate will look like this for a RISC-V target:
@@ -162,6 +166,7 @@
162166
//! riscv = "0.9.0"
163167
//! riscv-rt = { version = "0.9.0", optional = true }
164168
//! vcell = "0.1.0"
169+
//! const-default = { version = "1.0", default-features = false }
165170
//!
166171
//! [features]
167172
//! rt = ["riscv-rt"]

src/util.rs

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

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

0 commit comments

Comments
 (0)