Skip to content

Commit 30caef4

Browse files
bors[bot]burrbull
andauthored
Merge #720
720: default Field generics r=Emilgardis a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents e6a6d15 + 317fd0a commit 30caef4

File tree

3 files changed

+51
-32
lines changed

3 files changed

+51
-32
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](https://semver.org/).
77

88
## [Unreleased]
99

10+
- `bool` and `u8` as default generics for `BitReader/Writer` and `FieldReader/Writer`
1011
- Bump MSRV to 1.65
1112
- Optimize case change/sanitize
1213
- Fix dangling implicit derives

src/generate/generic.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,9 @@ impl<REG: RegisterSpec> W<REG> {
301301
}
302302

303303
#[doc(hidden)]
304-
pub struct FieldReaderRaw<U, T> {
304+
pub struct FieldReaderRaw<U, FI> {
305305
pub(crate) bits: U,
306-
_reg: marker::PhantomData<T>,
306+
_reg: marker::PhantomData<FI>,
307307
}
308308

309309
impl<U, FI> FieldReaderRaw<U, FI>
@@ -322,9 +322,9 @@ where
322322
}
323323

324324
#[doc(hidden)]
325-
pub struct BitReaderRaw<T> {
325+
pub struct BitReaderRaw<FI> {
326326
pub(crate) bits: bool,
327-
_reg: marker::PhantomData<T>,
327+
_reg: marker::PhantomData<FI>,
328328
}
329329

330330
impl<FI> BitReaderRaw<FI> {
@@ -342,10 +342,10 @@ impl<FI> BitReaderRaw<FI> {
342342
/// Field reader.
343343
///
344344
/// Result of the `read` methods of fields.
345-
pub type FieldReader<U, FI> = FieldReaderRaw<U, FI>;
345+
pub type FieldReader<U = u8, FI = u8> = FieldReaderRaw<U, FI>;
346346

347347
/// Bit-wise field reader
348-
pub type BitReader<FI> = BitReaderRaw<FI>;
348+
pub type BitReader<FI = bool> = BitReaderRaw<FI>;
349349

350350
impl<U, FI> FieldReader<U, FI>
351351
where
@@ -405,7 +405,7 @@ pub struct Safe;
405405
pub struct Unsafe;
406406

407407
#[doc(hidden)]
408-
pub struct FieldWriterRaw<'a, U, REG, N, FI, Safety, const WI: u8, const O: u8>
408+
pub struct FieldWriterRaw<'a, U, REG, const WI: u8, const O: u8, N, FI, Safety>
409409
where
410410
REG: Writable + RegisterSpec<Ux = U>,
411411
N: From<FI>,
@@ -414,8 +414,8 @@ where
414414
_field: marker::PhantomData<(N, FI, Safety)>,
415415
}
416416

417-
impl<'a, U, REG, N, FI, Safety, const WI: u8, const O: u8>
418-
FieldWriterRaw<'a, U, REG, N, FI, Safety, WI, O>
417+
impl<'a, U, REG, const WI: u8, const O: u8, N, FI, Safety>
418+
FieldWriterRaw<'a, U, REG, WI, O, N, FI, Safety>
419419
where
420420
REG: Writable + RegisterSpec<Ux = U>,
421421
N: From<FI>,
@@ -432,7 +432,7 @@ where
432432
}
433433

434434
#[doc(hidden)]
435-
pub struct BitWriterRaw<'a, U, REG, FI, M, const O: u8>
435+
pub struct BitWriterRaw<'a, U, REG, const O: u8, FI, M>
436436
where
437437
REG: Writable + RegisterSpec<Ux = U>,
438438
bool: From<FI>,
@@ -441,7 +441,7 @@ where
441441
_field: marker::PhantomData<(FI, M)>,
442442
}
443443

444-
impl<'a, U, REG, FI, M, const O: u8> BitWriterRaw<'a, U, REG, FI, M, O>
444+
impl<'a, U, REG, const O: u8, FI, M> BitWriterRaw<'a, U, REG, O, FI, M>
445445
where
446446
REG: Writable + RegisterSpec<Ux = U>,
447447
bool: From<FI>,
@@ -458,13 +458,13 @@ where
458458
}
459459

460460
/// Write field Proxy with unsafe `bits`
461-
pub type FieldWriter<'a, U, REG, N, FI, const WI: u8, const O: u8> =
462-
FieldWriterRaw<'a, U, REG, N, FI, Unsafe, WI, O>;
461+
pub type FieldWriter<'a, U, REG, const WI: u8, const O: u8, N = u8, FI = u8> =
462+
FieldWriterRaw<'a, U, REG, WI, O, N, FI, Unsafe>;
463463
/// Write field Proxy with safe `bits`
464-
pub type FieldWriterSafe<'a, U, REG, N, FI, const WI: u8, const O: u8> =
465-
FieldWriterRaw<'a, U, REG, N, FI, Safe, WI, O>;
464+
pub type FieldWriterSafe<'a, U, REG, const WI: u8, const O: u8, N = u8, FI = u8> =
465+
FieldWriterRaw<'a, U, REG, WI, O, N, FI, Safe>;
466466

467-
impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriter<'a, U, REG, N, FI, WI, OF>
467+
impl<'a, U, REG, const WI: u8, const OF: u8, N, FI> FieldWriter<'a, U, REG, WI, OF, N, FI>
468468
where
469469
REG: Writable + RegisterSpec<Ux = U>,
470470
N: From<FI>,
@@ -473,7 +473,7 @@ where
473473
pub const WIDTH: u8 = WI;
474474
}
475475

476-
impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriterSafe<'a, U, REG, N, FI, WI, OF>
476+
impl<'a, U, REG, const WI: u8, const OF: u8, N, FI> FieldWriterSafe<'a, U, REG, WI, OF, N, FI>
477477
where
478478
REG: Writable + RegisterSpec<Ux = U>,
479479
N: From<FI>,
@@ -488,9 +488,9 @@ macro_rules! bit_proxy {
488488
pub struct $mwv;
489489

490490
/// Bit-wise write field proxy
491-
pub type $writer<'a, U, REG, FI, const O: u8> = BitWriterRaw<'a, U, REG, FI, $mwv, O>;
491+
pub type $writer<'a, U, REG, const O: u8, FI = bool> = BitWriterRaw<'a, U, REG, O, FI, $mwv>;
492492

493-
impl<'a, U, REG, FI, const OF: u8> $writer<'a, U, REG, FI, OF>
493+
impl<'a, U, REG, const OF: u8, FI> $writer<'a, U, REG, OF, FI>
494494
where
495495
REG: Writable + RegisterSpec<Ux = U>,
496496
bool: From<FI>,
@@ -503,7 +503,7 @@ macro_rules! bit_proxy {
503503

504504
macro_rules! impl_bit_proxy {
505505
($writer:ident) => {
506-
impl<'a, U, REG, FI, const OF: u8> $writer<'a, U, REG, FI, OF>
506+
impl<'a, U, REG, const OF: u8, FI> $writer<'a, U, REG, OF, FI>
507507
where
508508
REG: Writable + RegisterSpec<Ux = U>,
509509
U: RawReg,
@@ -533,7 +533,7 @@ bit_proxy!(BitWriter0S, Bit0S);
533533
bit_proxy!(BitWriter1T, Bit1T);
534534
bit_proxy!(BitWriter0T, Bit0T);
535535

536-
impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriter<'a, U, REG, N, FI, WI, OF>
536+
impl<'a, U, REG, const WI: u8, const OF: u8, N, FI> FieldWriter<'a, U, REG, WI, OF, N, FI>
537537
where
538538
REG: Writable + RegisterSpec<Ux = U>,
539539
U: RawReg + From<N>,
@@ -556,7 +556,7 @@ where
556556
unsafe { self.bits(N::from(variant)) }
557557
}
558558
}
559-
impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriterSafe<'a, U, REG, N, FI, WI, OF>
559+
impl<'a, U, REG, const WI: u8, const OF: u8, N, FI> FieldWriterSafe<'a, U, REG, WI, OF, N, FI>
560560
where
561561
REG: Writable + RegisterSpec<Ux = U>,
562562
U: RawReg + From<N>,
@@ -584,7 +584,7 @@ impl_bit_proxy!(BitWriter0S);
584584
impl_bit_proxy!(BitWriter1T);
585585
impl_bit_proxy!(BitWriter0T);
586586

587-
impl<'a, U, REG, FI, const OF: u8> BitWriter<'a, U, REG, FI, OF>
587+
impl<'a, U, REG, const OF: u8, FI> BitWriter<'a, U, REG, OF, FI>
588588
where
589589
REG: Writable + RegisterSpec<Ux = U>,
590590
U: RawReg,
@@ -604,7 +604,7 @@ where
604604
}
605605
}
606606

607-
impl<'a, U, REG, FI, const OF: u8> BitWriter1S<'a, U, REG, FI, OF>
607+
impl<'a, U, REG, const OF: u8, FI> BitWriter1S<'a, U, REG, OF, FI>
608608
where
609609
REG: Writable + RegisterSpec<Ux = U>,
610610
U: RawReg,
@@ -618,7 +618,7 @@ where
618618
}
619619
}
620620

621-
impl<'a, U, REG, FI, const OF: u8> BitWriter0C<'a, U, REG, FI, OF>
621+
impl<'a, U, REG, const OF: u8, FI> BitWriter0C<'a, U, REG, OF, FI>
622622
where
623623
REG: Writable + RegisterSpec<Ux = U>,
624624
U: RawReg,
@@ -632,7 +632,7 @@ where
632632
}
633633
}
634634

635-
impl<'a, U, REG, FI, const OF: u8> BitWriter1C<'a, U, REG, FI, OF>
635+
impl<'a, U, REG, const OF: u8, FI> BitWriter1C<'a, U, REG, OF, FI>
636636
where
637637
REG: Writable + RegisterSpec<Ux = U>,
638638
U: RawReg,
@@ -646,7 +646,7 @@ where
646646
}
647647
}
648648

649-
impl<'a, U, REG, FI, const OF: u8> BitWriter0S<'a, U, REG, FI, OF>
649+
impl<'a, U, REG, const OF: u8, FI> BitWriter0S<'a, U, REG, OF, FI>
650650
where
651651
REG: Writable + RegisterSpec<Ux = U>,
652652
U: RawReg,
@@ -660,7 +660,7 @@ where
660660
}
661661
}
662662

663-
impl<'a, U, REG, FI, const OF: u8> BitWriter1T<'a, U, REG, FI, OF>
663+
impl<'a, U, REG, const OF: u8, FI> BitWriter1T<'a, U, REG, OF, FI>
664664
where
665665
REG: Writable + RegisterSpec<Ux = U>,
666666
U: RawReg,
@@ -674,7 +674,7 @@ where
674674
}
675675
}
676676

677-
impl<'a, U, REG, FI, const OF: u8> BitWriter0T<'a, U, REG, FI, OF>
677+
impl<'a, U, REG, const OF: u8, FI> BitWriter0T<'a, U, REG, OF, FI>
678678
where
679679
REG: Writable + RegisterSpec<Ux = U>,
680680
U: RawReg,

src/generate/register.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,15 @@ pub fn fields(
545545
// derive the read proxy structure if necessary.
546546
if should_derive_reader {
547547
let reader = if width == 1 {
548-
quote! { crate::BitReader<#value_read_ty> }
548+
if value_read_ty == "bool" {
549+
quote! { crate::BitReader }
550+
} else {
551+
quote! { crate::BitReader<#value_read_ty> }
552+
}
553+
} else if fty == "u8" && value_read_ty == "u8" {
554+
quote! { crate::FieldReader }
555+
} else if value_read_ty == "u8" {
556+
quote! { crate::FieldReader<#fty> }
549557
} else {
550558
quote! { crate::FieldReader<#fty, #value_read_ty> }
551559
};
@@ -868,7 +876,11 @@ pub fn fields(
868876
},
869877
span,
870878
);
871-
quote! { crate::#wproxy<'a, #rty, #regspec_ident, #value_write_ty, O> }
879+
if value_write_ty == "bool" {
880+
quote! { crate::#wproxy<'a, #rty, #regspec_ident, O> }
881+
} else {
882+
quote! { crate::#wproxy<'a, #rty, #regspec_ident, O, #value_write_ty> }
883+
}
872884
} else {
873885
let wproxy = Ident::new(
874886
if unsafety {
@@ -879,7 +891,13 @@ pub fn fields(
879891
span,
880892
);
881893
let width = &util::unsuffixed(width as _);
882-
quote! { crate::#wproxy<'a, #rty, #regspec_ident, #fty, #value_write_ty, #width, O> }
894+
if fty == "u8" && value_write_ty == "u8" {
895+
quote! { crate::#wproxy<'a, #rty, #regspec_ident, #width, O> }
896+
} else if value_write_ty == "u8" {
897+
quote! { crate::#wproxy<'a, #rty, #regspec_ident, #width, O, #fty> }
898+
} else {
899+
quote! { crate::#wproxy<'a, #rty, #regspec_ident, #width, O, #fty, #value_write_ty> }
900+
}
883901
};
884902
mod_items.extend(quote! {
885903
#[doc = #field_writer_brief]

0 commit comments

Comments
 (0)