Skip to content

Commit c6316a3

Browse files
committed
use ConstZero instead of Default
1 parent b6ef0b0 commit c6316a3

File tree

10 files changed

+66
-321
lines changed

10 files changed

+66
-321
lines changed

.github/workflows/ci.yml

Lines changed: 16 additions & 4 deletions
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

@@ -83,7 +83,11 @@ jobs:
8383
- { rust: stable, vendor: Spansion, options: "--atomics" }
8484
- { rust: stable, vendor: STMicro, options: "" }
8585
- { rust: stable, vendor: STMicro, options: "--atomics" }
86-
- { rust: stable, vendor: STM32-patched, options: "--strict -f enum_value::p: --max-cluster-size --atomics --atomics-feature atomics --impl-debug --impl-defmt defmt" }
86+
- {
87+
rust: stable,
88+
vendor: STM32-patched,
89+
options: "--strict -f enum_value::p: --max-cluster-size --atomics --atomics-feature atomics --impl-debug --impl-defmt defmt",
90+
}
8791
- { rust: stable, vendor: Toshiba, options: all }
8892
- { rust: stable, vendor: Toshiba, options: "" }
8993
# Test MSRV
@@ -92,8 +96,16 @@ jobs:
9296
- { rust: nightly, vendor: MSP430, options: "--atomics" }
9397
- { rust: nightly, vendor: MSP430, options: "" }
9498
# Workaround for _1token0
95-
- { rust: nightly-2024-09-25, vendor: Espressif, options: "--atomics --ident-formats-theme legacy" }
96-
- { rust: nightly-2024-09-25, vendor: Espressif, options: "--ident-format register:::Reg" }
99+
- {
100+
rust: nightly-2024-09-25,
101+
vendor: Espressif,
102+
options: "--atomics --ident-formats-theme legacy",
103+
}
104+
- {
105+
rust: nightly-2024-09-25,
106+
vendor: Espressif,
107+
options: "--ident-format register:::Reg",
108+
}
97109

98110
steps:
99111
- uses: actions/checkout@v4

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 `ConstZero::ZERO` instead of `Default::default()` to force const
11+
1012
## [v0.35.0] - 2024-11-12
1113

1214
- Add `crate_path` setting
@@ -930,8 +932,7 @@ peripheral.register.write(|w| w.field().set());
930932

931933
- Initial version of the `svd2rust` tool
932934

933-
[Unreleased]: https://github.com/rust-embedded/svd2rust/compare/v0.35.0...HEAD
934-
[v0.35.0]: https://github.com/rust-embedded/svd2rust/compare/v0.34.0...v0.35.0
935+
[Unreleased]: https://github.com/rust-embedded/svd2rust/compare/v0.34.0...HEAD
935936
[v0.34.0]: https://github.com/rust-embedded/svd2rust/compare/v0.33.5...v0.34.0
936937
[v0.33.5]: https://github.com/rust-embedded/svd2rust/compare/v0.33.4...v0.33.5
937938
[v0.33.4]: https://github.com/rust-embedded/svd2rust/compare/v0.33.3...v0.33.4

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 'num-traits = { version = "0.2.19", 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+
"num-traits = { version = \"0.2.19\", 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: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::marker;
33
/// Raw register type (`u8`, `u16`, `u32`, ...)
44
pub trait RawReg:
55
Copy
6-
+ Default
6+
+ num_traits::ConstZero
77
+ From<bool>
88
+ core::ops::BitOr<Output = Self>
99
+ core::ops::BitAnd<Output = Self>
@@ -74,10 +74,10 @@ pub trait Writable: RegisterSpec {
7474
type Safety;
7575

7676
/// 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;
77+
const ZERO_TO_MODIFY_FIELDS_BITMAP: Self::Ux = Self::Ux::ZERO;
7878

7979
/// 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;
80+
const ONE_TO_MODIFY_FIELDS_BITMAP: Self::Ux = Self::Ux::ZERO;
8181
}
8282

8383
/// Reset value of the register.
@@ -86,7 +86,7 @@ pub trait Writable: RegisterSpec {
8686
/// register by using the `reset` method.
8787
pub trait Resettable: RegisterSpec {
8888
/// Reset value of the register.
89-
const RESET_VALUE: Self::Ux;
89+
const RESET_VALUE: Self::Ux = Self::Ux::ZERO;
9090

9191
/// Reset value of the register.
9292
#[inline(always)]
@@ -247,7 +247,10 @@ impl<REG: Writable> W<REG> {
247247
self
248248
}
249249
}
250-
impl<REG> W<REG> where REG: Writable<Safety = Safe> {
250+
impl<REG> W<REG>
251+
where
252+
REG: Writable<Safety = Safe>,
253+
{
251254
/// Writes raw bits to the register.
252255
#[inline(always)]
253256
pub fn set(&mut self, bits: REG::Ux) -> &mut Self {
@@ -335,7 +338,8 @@ pub struct RangeFrom<const MIN: u64>;
335338
pub struct RangeTo<const MAX: u64>;
336339

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

340344
impl<'a, REG, const WI: u8, FI, Safety> FieldWriter<'a, REG, WI, FI, Safety>
341345
where
@@ -390,7 +394,8 @@ where
390394
}
391395
}
392396

393-
impl<'a, REG, const WI: u8, FI, const MIN: u64, const MAX: u64> FieldWriter<'a, REG, WI, FI, Range<MIN, MAX>>
397+
impl<'a, REG, const WI: u8, FI, const MIN: u64, const MAX: u64>
398+
FieldWriter<'a, REG, WI, FI, Range<MIN, MAX>>
394399
where
395400
REG: Writable + RegisterSpec,
396401
FI: FieldSpec,
@@ -478,7 +483,7 @@ macro_rules! bit_proxy {
478483
pub const fn width(&self) -> u8 {
479484
Self::WIDTH
480485
}
481-
486+
482487
/// Field offset
483488
#[inline(always)]
484489
pub const fn offset(&self) -> u8 {

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;

0 commit comments

Comments
 (0)