Skip to content

Commit 90e97c1

Browse files
bors[bot]burrbull
andauthored
Merge #675
675: use_cast r=therealprof a=burrbull Fixes #671 Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents 1f794ae + bf93c62 commit 90e97c1

File tree

3 files changed

+53
-45
lines changed

3 files changed

+53
-45
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: 34 additions & 40 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
@@ -268,11 +269,12 @@ impl<REG: RegisterSpec> R<REG> {
268269
impl<REG: RegisterSpec, FI> PartialEq<FI> for R<REG>
269270
where
270271
REG::Ux: PartialEq,
271-
FI: Copy + Into<REG::Ux>,
272+
FI: Copy,
273+
REG::Ux: From<FI>
272274
{
273275
#[inline(always)]
274276
fn eq(&self, other: &FI) -> bool {
275-
self.bits.eq(&(*other).into())
277+
self.bits.eq(&REG::Ux::from(*other))
276278
}
277279
}
278280

@@ -359,21 +361,23 @@ where
359361
impl<U, FI> PartialEq<FI> for FieldReader<U, FI>
360362
where
361363
U: PartialEq,
362-
FI: Copy + Into<U>,
364+
FI: Copy,
365+
U: From<FI>,
363366
{
364367
#[inline(always)]
365368
fn eq(&self, other: &FI) -> bool {
366-
self.bits.eq(&(*other).into())
369+
self.bits.eq(&U::from(*other))
367370
}
368371
}
369372

370373
impl<FI> PartialEq<FI> for BitReader<FI>
371374
where
372-
FI: Copy + Into<bool>,
375+
FI: Copy,
376+
bool: From<FI>,
373377
{
374378
#[inline(always)]
375379
fn eq(&self, other: &FI) -> bool {
376-
self.bits.eq(&(*other).into())
380+
self.bits.eq(&bool::from(*other))
377381
}
378382
}
379383

@@ -404,7 +408,7 @@ pub struct Unsafe;
404408
pub struct FieldWriterRaw<'a, U, REG, N, FI, Safety, const WI: u8, const O: u8>
405409
where
406410
REG: Writable + RegisterSpec<Ux = U>,
407-
FI: Into<N>,
411+
N: From<FI>,
408412
{
409413
pub(crate) w: &'a mut REG::Writer,
410414
_field: marker::PhantomData<(N, FI, Safety)>,
@@ -414,7 +418,7 @@ impl<'a, U, REG, N, FI, Safety, const WI: u8, const O: u8>
414418
FieldWriterRaw<'a, U, REG, N, FI, Safety, WI, O>
415419
where
416420
REG: Writable + RegisterSpec<Ux = U>,
417-
FI: Into<N>,
421+
N: From<FI>,
418422
{
419423
/// Creates a new instance of the writer
420424
#[allow(unused)]
@@ -431,7 +435,7 @@ where
431435
pub struct BitWriterRaw<'a, U, REG, FI, M, const O: u8>
432436
where
433437
REG: Writable + RegisterSpec<Ux = U>,
434-
FI: Into<bool>,
438+
bool: From<FI>,
435439
{
436440
pub(crate) w: &'a mut REG::Writer,
437441
_field: marker::PhantomData<(FI, M)>,
@@ -440,7 +444,7 @@ where
440444
impl<'a, U, REG, FI, M, const O: u8> BitWriterRaw<'a, U, REG, FI, M, O>
441445
where
442446
REG: Writable + RegisterSpec<Ux = U>,
443-
FI: Into<bool>,
447+
bool: From<FI>,
444448
{
445449
/// Creates a new instance of the writer
446450
#[allow(unused)]
@@ -463,7 +467,7 @@ pub type FieldWriterSafe<'a, U, REG, N, FI, const WI: u8, const O: u8> =
463467
impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriter<'a, U, REG, N, FI, WI, OF>
464468
where
465469
REG: Writable + RegisterSpec<Ux = U>,
466-
FI: Into<N>,
470+
N: From<FI>,
467471
{
468472
/// Field width
469473
pub const WIDTH: u8 = WI;
@@ -472,7 +476,7 @@ where
472476
impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriterSafe<'a, U, REG, N, FI, WI, OF>
473477
where
474478
REG: Writable + RegisterSpec<Ux = U>,
475-
FI: Into<N>,
479+
N: From<FI>,
476480
{
477481
/// Field width
478482
pub const WIDTH: u8 = WI;
@@ -489,7 +493,7 @@ macro_rules! bit_proxy {
489493
impl<'a, U, REG, FI, const OF: u8> $writer<'a, U, REG, FI, OF>
490494
where
491495
REG: Writable + RegisterSpec<Ux = U>,
492-
FI: Into<bool>,
496+
bool: From<FI>,
493497
{
494498
/// Field width
495499
pub const WIDTH: u8 = 1;
@@ -503,8 +507,7 @@ macro_rules! impl_bit_proxy {
503507
where
504508
REG: Writable + RegisterSpec<Ux = U>,
505509
U: RawReg,
506-
U: From<bool>,
507-
FI: Into<bool>,
510+
bool: From<FI>,
508511
{
509512
/// Writes bit to the field
510513
#[inline(always)]
@@ -516,7 +519,7 @@ macro_rules! impl_bit_proxy {
516519
/// Writes `variant` to the field
517520
#[inline(always)]
518521
pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
519-
self.bit(variant.into())
522+
self.bit(bool::from(variant))
520523
}
521524
}
522525
};
@@ -533,9 +536,8 @@ bit_proxy!(BitWriter0T, Bit0T);
533536
impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriter<'a, U, REG, N, FI, WI, OF>
534537
where
535538
REG: Writable + RegisterSpec<Ux = U>,
536-
U: RawReg,
537-
N: Into<U>,
538-
FI: Into<N>,
539+
U: RawReg + From<N>,
540+
N: From<FI>,
539541
{
540542
/// Writes raw bits to the field
541543
///
@@ -545,33 +547,32 @@ where
545547
#[inline(always)]
546548
pub unsafe fn bits(self, value: N) -> &'a mut REG::Writer {
547549
self.w.bits &= !(U::mask::<WI>() << { OF });
548-
self.w.bits |= (value.into() & U::mask::<WI>()) << { OF };
550+
self.w.bits |= (U::from(value) & U::mask::<WI>()) << { OF };
549551
self.w
550552
}
551553
/// Writes `variant` to the field
552554
#[inline(always)]
553555
pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
554-
unsafe { self.bits(variant.into()) }
556+
unsafe { self.bits(N::from(variant)) }
555557
}
556558
}
557559
impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriterSafe<'a, U, REG, N, FI, WI, OF>
558560
where
559561
REG: Writable + RegisterSpec<Ux = U>,
560-
U: RawReg,
561-
N: Into<U>,
562-
FI: Into<N>,
562+
U: RawReg + From<N>,
563+
N: From<FI>,
563564
{
564565
/// Writes raw bits to the field
565566
#[inline(always)]
566567
pub fn bits(self, value: N) -> &'a mut REG::Writer {
567568
self.w.bits &= !(U::mask::<WI>() << { OF });
568-
self.w.bits |= (value.into() & U::mask::<WI>()) << { OF };
569+
self.w.bits |= (U::from(value) & U::mask::<WI>()) << { OF };
569570
self.w
570571
}
571572
/// Writes `variant` to the field
572573
#[inline(always)]
573574
pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
574-
self.bits(variant.into())
575+
self.bits(N::from(variant))
575576
}
576577
}
577578

@@ -587,8 +588,7 @@ impl<'a, U, REG, FI, const OF: u8> BitWriter<'a, U, REG, FI, OF>
587588
where
588589
REG: Writable + RegisterSpec<Ux = U>,
589590
U: RawReg,
590-
U: From<bool>,
591-
FI: Into<bool>,
591+
bool: From<FI>,
592592
{
593593
/// Sets the field bit
594594
#[inline(always)]
@@ -606,8 +606,7 @@ impl<'a, U, REG, FI, const OF: u8> BitWriter1S<'a, U, REG, FI, OF>
606606
where
607607
REG: Writable + RegisterSpec<Ux = U>,
608608
U: RawReg,
609-
U: From<bool>,
610-
FI: Into<bool>,
609+
bool: From<FI>,
611610
{
612611
/// Sets the field bit
613612
#[inline(always)]
@@ -620,8 +619,7 @@ impl<'a, U, REG, FI, const OF: u8> BitWriter0C<'a, U, REG, FI, OF>
620619
where
621620
REG: Writable + RegisterSpec<Ux = U>,
622621
U: RawReg,
623-
U: From<bool>,
624-
FI: Into<bool>,
622+
bool: From<FI>,
625623
{
626624
/// Clears the field bit
627625
#[inline(always)]
@@ -634,8 +632,7 @@ impl<'a, U, REG, FI, const OF: u8> BitWriter1C<'a, U, REG, FI, OF>
634632
where
635633
REG: Writable + RegisterSpec<Ux = U>,
636634
U: RawReg,
637-
U: From<bool>,
638-
FI: Into<bool>,
635+
bool: From<FI>,
639636
{
640637
///Clears the field bit by passing one
641638
#[inline(always)]
@@ -648,8 +645,7 @@ impl<'a, U, REG, FI, const OF: u8> BitWriter0S<'a, U, REG, FI, OF>
648645
where
649646
REG: Writable + RegisterSpec<Ux = U>,
650647
U: RawReg,
651-
U: From<bool>,
652-
FI: Into<bool>,
648+
bool: From<FI>,
653649
{
654650
///Sets the field bit by passing zero
655651
#[inline(always)]
@@ -662,8 +658,7 @@ impl<'a, U, REG, FI, const OF: u8> BitWriter1T<'a, U, REG, FI, OF>
662658
where
663659
REG: Writable + RegisterSpec<Ux = U>,
664660
U: RawReg,
665-
U: From<bool>,
666-
FI: Into<bool>,
661+
bool: From<FI>,
667662
{
668663
///Toggle the field bit by passing one
669664
#[inline(always)]
@@ -676,8 +671,7 @@ impl<'a, U, REG, FI, const OF: u8> BitWriter0T<'a, U, REG, FI, OF>
676671
where
677672
REG: Writable + RegisterSpec<Ux = U>,
678673
U: RawReg,
679-
U: From<bool>,
680-
FI: Into<bool>,
674+
bool: From<FI>,
681675
{
682676
///Toggle the field bit by passing zero
683677
#[inline(always)]

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)