Skip to content

Commit 9191c13

Browse files
committed
use_cast
1 parent 1f794ae commit 9191c13

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

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](http://semver.org/).
77

88
## [Unreleased]
99

10+
- Don't cast fields with width 17-31
1011
- Make `generic.rs` generic
1112
- Change initial write value for registers with modifiedWriteValues
1213
- Update `clap` to 4.0, use `irx-config` instead of `clap_conf`

src/generate/generic.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use core::marker;
44
pub trait RawReg:
55
Copy
66
+ Default
7+
+ From<bool>
78
+ core::ops::BitOr<Output = Self>
89
+ core::ops::BitAnd<Output = Self>
910
+ core::ops::BitOrAssign
@@ -503,7 +504,6 @@ macro_rules! impl_bit_proxy {
503504
where
504505
REG: Writable + RegisterSpec<Ux = U>,
505506
U: RawReg,
506-
U: From<bool>,
507507
FI: Into<bool>,
508508
{
509509
/// Writes bit to the field
@@ -533,8 +533,7 @@ bit_proxy!(BitWriter0T, Bit0T);
533533
impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriter<'a, U, REG, N, FI, WI, OF>
534534
where
535535
REG: Writable + RegisterSpec<Ux = U>,
536-
U: RawReg,
537-
N: Into<U>,
536+
U: RawReg + From<N>,
538537
FI: Into<N>,
539538
{
540539
/// Writes raw bits to the field
@@ -545,7 +544,7 @@ where
545544
#[inline(always)]
546545
pub unsafe fn bits(self, value: N) -> &'a mut REG::Writer {
547546
self.w.bits &= !(U::mask::<WI>() << { OF });
548-
self.w.bits |= (value.into() & U::mask::<WI>()) << { OF };
547+
self.w.bits |= (U::from(value) & U::mask::<WI>()) << { OF };
549548
self.w
550549
}
551550
/// Writes `variant` to the field
@@ -557,15 +556,14 @@ where
557556
impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriterSafe<'a, U, REG, N, FI, WI, OF>
558557
where
559558
REG: Writable + RegisterSpec<Ux = U>,
560-
U: RawReg,
561-
N: Into<U>,
559+
U: RawReg + From<N>,
562560
FI: Into<N>,
563561
{
564562
/// Writes raw bits to the field
565563
#[inline(always)]
566564
pub fn bits(self, value: N) -> &'a mut REG::Writer {
567565
self.w.bits &= !(U::mask::<WI>() << { OF });
568-
self.w.bits |= (value.into() & U::mask::<WI>()) << { OF };
566+
self.w.bits |= (U::from(value) & U::mask::<WI>()) << { OF };
569567
self.w
570568
}
571569
/// Writes `variant` to the field
@@ -587,7 +585,6 @@ impl<'a, U, REG, FI, const OF: u8> BitWriter<'a, U, REG, FI, OF>
587585
where
588586
REG: Writable + RegisterSpec<Ux = U>,
589587
U: RawReg,
590-
U: From<bool>,
591588
FI: Into<bool>,
592589
{
593590
/// Sets the field bit
@@ -606,7 +603,6 @@ impl<'a, U, REG, FI, const OF: u8> BitWriter1S<'a, U, REG, FI, OF>
606603
where
607604
REG: Writable + RegisterSpec<Ux = U>,
608605
U: RawReg,
609-
U: From<bool>,
610606
FI: Into<bool>,
611607
{
612608
/// Sets the field bit
@@ -620,7 +616,6 @@ impl<'a, U, REG, FI, const OF: u8> BitWriter0C<'a, U, REG, FI, OF>
620616
where
621617
REG: Writable + RegisterSpec<Ux = U>,
622618
U: RawReg,
623-
U: From<bool>,
624619
FI: Into<bool>,
625620
{
626621
/// Clears the field bit
@@ -634,7 +629,6 @@ impl<'a, U, REG, FI, const OF: u8> BitWriter1C<'a, U, REG, FI, OF>
634629
where
635630
REG: Writable + RegisterSpec<Ux = U>,
636631
U: RawReg,
637-
U: From<bool>,
638632
FI: Into<bool>,
639633
{
640634
///Clears the field bit by passing one
@@ -648,7 +642,6 @@ impl<'a, U, REG, FI, const OF: u8> BitWriter0S<'a, U, REG, FI, OF>
648642
where
649643
REG: Writable + RegisterSpec<Ux = U>,
650644
U: RawReg,
651-
U: From<bool>,
652645
FI: Into<bool>,
653646
{
654647
///Sets the field bit by passing zero
@@ -662,7 +655,6 @@ impl<'a, U, REG, FI, const OF: u8> BitWriter1T<'a, U, REG, FI, OF>
662655
where
663656
REG: Writable + RegisterSpec<Ux = U>,
664657
U: RawReg,
665-
U: From<bool>,
666658
FI: Into<bool>,
667659
{
668660
///Toggle the field bit by passing one
@@ -676,7 +668,6 @@ impl<'a, U, REG, FI, const OF: u8> BitWriter0T<'a, U, REG, FI, OF>
676668
where
677669
REG: Writable + RegisterSpec<Ux = U>,
678670
U: RawReg,
679-
U: From<bool>,
680671
FI: Into<bool>,
681672
{
682673
///Toggle the field bit by passing zero

src/generate/register.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -436,10 +436,15 @@ pub fn fields(
436436
let rv = properties.reset_value.map(|rv| (rv >> offset) & mask);
437437
let fty = width.to_ty()?;
438438

439-
let use_mask = if let Some(size) = properties.size {
440-
size != width
439+
let use_mask;
440+
let use_cast;
441+
if let Some(size) = properties.size {
442+
let size = size.to_ty_width()?;
443+
use_cast = size != width.to_ty_width()?;
444+
use_mask = size != width;
441445
} else {
442-
true
446+
use_cast = true;
447+
use_mask = true;
443448
};
444449

445450
let mut lookup_results = Vec::new();
@@ -481,10 +486,14 @@ pub fn fields(
481486
quote! {
482487
((self.bits >> #offset) & #hexmask) #cast
483488
}
484-
} else if use_mask {
489+
} else if use_cast {
485490
quote! {
486491
(self.bits & #hexmask) #cast
487492
}
493+
} else if use_mask {
494+
quote! {
495+
self.bits & #hexmask
496+
}
488497
} else {
489498
quote! {
490499
self.bits
@@ -713,10 +722,14 @@ pub fn fields(
713722
quote! {
714723
((self.bits >> #sub_offset) & #hexmask) #cast
715724
}
716-
} else if use_mask {
725+
} else if use_cast {
717726
quote! {
718727
(self.bits & #hexmask) #cast
719728
}
729+
} else if use_mask {
730+
quote! {
731+
self.bits & #hexmask
732+
}
720733
} else {
721734
quote! {
722735
self.bits

0 commit comments

Comments
 (0)